@@ -8,6 +8,7 @@ namespace Dropbox.Api
88{
99 using System ;
1010 using System . Collections . Generic ;
11+ using System . Net ;
1112 using System . Net . Http ;
1213 using System . Text ;
1314 using System . Threading . Tasks ;
@@ -378,6 +379,11 @@ public static async Task<OAuth2Response> ProcessCodeFlowAsync(string code, strin
378379 var raw = await response . Content . ReadAsStringAsync ( ) ;
379380 var json = JObject . Parse ( raw ) ;
380381
382+ if ( response . StatusCode != HttpStatusCode . OK )
383+ {
384+ throw new OAuth2Exception ( json [ "error" ] . ToString ( ) , json . Value < string > ( "error_description" ) ) ;
385+ }
386+
381387 return new OAuth2Response (
382388 json [ "access_token" ] . ToString ( ) ,
383389 json [ "uid" ] . ToString ( ) ,
@@ -528,4 +534,25 @@ internal OAuth2Response(string accessToken, string uid, string state, string tok
528534 /// </value>
529535 public string TokenType { get ; private set ; }
530536 }
537+
538+ /// <summary>
539+ /// Exception when error occurs during oauth2 flow.
540+ /// </summary>
541+ public sealed class OAuth2Exception : Exception
542+ {
543+ /// <summary>
544+ /// Initializes a new instance of the <see cref="OAuth2Exception"/> class.
545+ /// </summary>
546+ /// <param name="message">The message.</param>
547+ /// <param name="errorDescription">The error description</param>
548+ public OAuth2Exception ( string message , string errorDescription = null ) : base ( message )
549+ {
550+ this . ErrorDescription = errorDescription ;
551+ }
552+
553+ /// <summary>
554+ /// Gets the error description.
555+ /// </summary>
556+ public string ErrorDescription { get ; private set ; }
557+ }
531558}
0 commit comments