Skip to content

Commit 83abdcb

Browse files
committed
Add ErrorResponse to AuthException.
1 parent d7a6233 commit 83abdcb

File tree

9 files changed

+219
-144
lines changed

9 files changed

+219
-144
lines changed

Dropbox.Api/ApiException.cs

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ namespace Dropbox.Api
88
{
99
using System;
1010

11-
using Dropbox.Api.Babel;
12-
1311
/// <summary>
1412
/// The exception type that will be raised by an <see cref="ITransport" />
15-
/// implementation if there is an error processing the request.
13+
/// implementation if there is an error processing the request which is caused by
14+
/// failure in API route.
1615
/// </summary>
1716
/// <typeparam name="TError">The type of the error.</typeparam>
18-
public sealed class ApiException<TError> : Exception
17+
public sealed class ApiException<TError> : StructuredException<TError>
1918
{
2019
/// <summary>
2120
/// Initializes a new instance of the <see cref="ApiException{TError}"/> class.
@@ -52,93 +51,8 @@ public ApiException(TError errorResponse, string message)
5251
/// <param name="message">The message.</param>
5352
/// <param name="inner">The inner.</param>
5453
public ApiException(TError errorResponse, string message, Exception inner)
55-
: base(message, inner)
54+
: base(errorResponse, message, inner)
5655
{
57-
this.ErrorResponse = errorResponse;
58-
this.ErrorMessage = message;
59-
}
60-
61-
/// <summary>
62-
/// Gets the error response.
63-
/// </summary>
64-
/// <value>
65-
/// The error response.
66-
/// </value>
67-
public TError ErrorResponse { get; private set; }
68-
69-
/// <summary>
70-
/// Gets the exception message.
71-
/// </summary>
72-
public override string Message
73-
{
74-
get { return this.ErrorMessage; }
75-
}
76-
77-
/// <summary>
78-
/// Gets or sets the error message.
79-
/// </summary>
80-
private string ErrorMessage { get; set; }
81-
82-
/// <summary>
83-
/// Decode from given json using given decoder.
84-
/// </summary>
85-
/// <param name="json">The json.</param>
86-
/// <param name="errorDecoder">The error json.</param>
87-
/// <returns>The <see cref="ApiException{TError}"/></returns>
88-
internal static ApiException<TError> Decode(string json, IDecoder<TError> errorDecoder)
89-
{
90-
return JsonReader.Read(json, new ApiExceptionDecoder(errorDecoder));
91-
}
92-
93-
/// <summary>
94-
/// The exception decoder.
95-
/// </summary>
96-
private class ApiExceptionDecoder : StructDecoder<ApiException<TError>>
97-
{
98-
/// <summary>
99-
/// The error decoder.
100-
/// </summary>
101-
private readonly IDecoder<TError> errorDecoder;
102-
103-
/// <summary>
104-
/// Initializes a new instance of the <see cref="ApiExceptionDecoder"/> class.
105-
/// </summary>
106-
/// <param name="errorDecoder">The error decoder.</param>
107-
public ApiExceptionDecoder(IDecoder<TError> errorDecoder)
108-
{
109-
this.errorDecoder = errorDecoder;
110-
}
111-
112-
/// <summary>
113-
/// Create a struct instance.
114-
/// </summary>
115-
/// <returns>The struct instance.</returns>
116-
protected override ApiException<TError> Create()
117-
{
118-
return new ApiException<TError>();
119-
}
120-
121-
/// <summary>
122-
/// Set given field.
123-
/// </summary>
124-
/// <param name="value">The field value.</param>
125-
/// <param name="fieldName">The field name.</param>
126-
/// <param name="reader">The reader.</param>
127-
protected override void SetField(ApiException<TError> value, string fieldName, IJsonReader reader)
128-
{
129-
switch (fieldName)
130-
{
131-
case "error":
132-
value.ErrorResponse = this.errorDecoder.Decode(reader);
133-
break;
134-
case "error_summary":
135-
value.ErrorMessage = StringDecoder.Instance.Decode(reader);
136-
break;
137-
default:
138-
reader.Skip();
139-
break;
140-
}
141-
}
14256
}
14357
}
14458
}

Dropbox.Api/AuthException.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <auto-generated>
2+
// Auto-generated by BabelAPI, do not modify.
3+
// </auto-generated>
4+
5+
namespace Dropbox.Api
6+
{
7+
using sys = System;
8+
9+
using Dropbox.Api.Auth;
10+
11+
/// <summary>
12+
/// <para>An HTTP exception that is caused by the server reporting an authentication
13+
/// problem.</para>
14+
/// </summary>
15+
public sealed partial class AuthException : StructuredException<AuthError>
16+
{
17+
/// <summary>
18+
/// <para>Initializes a new instance of the <see cref="AuthException"/> class.</para>
19+
/// </summary>
20+
public AuthException()
21+
{
22+
}
23+
24+
/// <summary>
25+
/// <para>Decode from given json.</para>
26+
/// </summary>
27+
internal static AuthException Decode(string json)
28+
{
29+
return StructuredException<AuthError>.Decode<AuthException>(json, AuthError.Decoder);
30+
}
31+
}
32+
}

Dropbox.Api/Dropbox.Api.Doc.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="Babel\JsonReader.cs" />
6464
<Compile Include="Babel\JsonWriter.cs" />
6565
<Compile Include="ApiException.cs" />
66+
<Compile Include="StructuredException.cs" />
6667
<Compile Include="Babel\Util.cs" />
6768
<Compile Include="DropboxCertHelper.cs" />
6869
<Compile Include="DropboxClient.common.cs" />
@@ -77,9 +78,9 @@
7778
<Compile Include="Async\PollEmptyResult.cs" />
7879
<Compile Include="Async\PollError.cs" />
7980
<Compile Include="Async\PollResultBase.cs" />
81+
<Compile Include="AuthException.cs" />
8082
<Compile Include="Auth\AuthError.cs" />
8183
<Compile Include="DropboxClient.cs" />
82-
<Compile Include="DropboxRequestHandlerFactory.cs" />
8384
<Compile Include="DropboxTeamClient.cs" />
8485
<Compile Include="Files\CommitInfo.cs" />
8586
<Compile Include="Files\CreateFolderArg.cs" />

Dropbox.Api/Dropbox.Api.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<Compile Include="Babel\JsonReader.cs" />
8484
<Compile Include="Babel\JsonWriter.cs" />
8585
<Compile Include="ApiException.cs" />
86+
<Compile Include="StructuredException.cs" />
8687
<Compile Include="Babel\Util.cs" />
8788
<Compile Include="DropboxCertHelper.cs" />
8889
<Compile Include="DropboxClient.common.cs" />
@@ -97,9 +98,9 @@
9798
<Compile Include="Async\PollEmptyResult.cs" />
9899
<Compile Include="Async\PollError.cs" />
99100
<Compile Include="Async\PollResultBase.cs" />
101+
<Compile Include="AuthException.cs" />
100102
<Compile Include="Auth\AuthError.cs" />
101103
<Compile Include="DropboxClient.cs" />
102-
<Compile Include="DropboxRequestHandlerFactory.cs" />
103104
<Compile Include="DropboxTeamClient.cs" />
104105
<Compile Include="Files\CommitInfo.cs" />
105106
<Compile Include="Files\CreateFolderArg.cs" />

Dropbox.Api/Dropbox.Api.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>$id$</id>
5-
<version>2.3.2</version>
5+
<version>2.3.3</version>
66
<title>Dropbox v2 API Beta</title>
77
<authors>Dropbox Inc</authors>
88
<owners>Dropbox Inc</owners>
@@ -18,7 +18,7 @@ What's new:
1818
-Include Dropbox Business endpoints.
1919

2020
Bug fixes:
21-
-Now throw ApiException&lt;AuthError&gt; instead of AuthException</releaseNotes>
21+
-AuthException now has a deserialized ErrorResponse field.</releaseNotes>
2222
<copyright>Copyright (c) Dropbox Inc. 2015</copyright>
2323
<tags>Dropbox Api</tags>
2424
</metadata>

Dropbox.Api/DropboxClient.common.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public DropboxClient(
4848
/// a team access token, actions will be performed on this this user's Dropbox.</param>
4949
internal DropboxClient(DropboxRequestHandlerOptions options, string selectUser = null)
5050
{
51-
this.InitializeRoutes(DropboxRequestHandlerFactory.Create(options, selectUser));
51+
this.InitializeRoutes(new DropboxRequestHandler(options, selectUser));
5252
}
5353

5454
/// <summary>
@@ -94,7 +94,7 @@ public DropboxTeamClient(
9494
}
9595

9696
this.options = new DropboxRequestHandlerOptions(oauth2AccessToken, maxRetriesOnError, userAgent, httpClient: httpClient);
97-
this.InitializeRoutes(DropboxRequestHandlerFactory.Create(this.options));
97+
this.InitializeRoutes(new DropboxRequestHandler(this.options));
9898
}
9999

100100
/// <summary>
@@ -163,18 +163,37 @@ public BadInputException(string message, Uri uri = null)
163163
/// <summary>
164164
/// An HTTP exception that is caused by the server reporting an authentication problem.
165165
/// </summary>
166-
[Obsolete("This exception is no longer thrown. Please catch ApiException<AuthError> instead.")]
167-
public class AuthException : HttpException
166+
public partial class AuthException
168167
{
169168
/// <summary>
170169
/// Initializes a new instance of the <see cref="AuthException"/> class.
171170
/// </summary>
172171
/// <param name="message">The message that describes the error.</param>
173172
/// <param name="uri">The request URI</param>
174-
public AuthException(string message, Uri uri = null)
175-
: base(401, message, uri)
173+
[Obsolete("This constructor will be removed soon.")]
174+
public AuthException(string message, Uri uri = null) : base(null, message)
176175
{
176+
this.StatusCode = 401;
177+
this.RequestUri = uri;
177178
}
179+
180+
/// <summary>
181+
/// Gets the HTTP status code that prompted this exception
182+
/// </summary>
183+
/// <value>
184+
/// The status code.
185+
/// </value>
186+
[Obsolete("This field will be removed soon.")]
187+
public int StatusCode { get; private set; }
188+
189+
/// <summary>
190+
/// Gets the URI for the request that prompted this exception.
191+
/// </summary>
192+
/// <value>
193+
/// The request URI.
194+
/// </value>
195+
[Obsolete("This field will be removed soon.")]
196+
public Uri RequestUri { get; private set; }
178197
}
179198

180199
/// <summary>

Dropbox.Api/DropboxRequestHandler.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ namespace Dropbox.Api
2424
/// <summary>
2525
/// The object used to to make requests to the Dropbox API.
2626
/// </summary>
27-
/// <typeparam name="TAuthError">The type for auth error.</typeparam>
28-
internal sealed class DropboxRequestHandler<TAuthError> : ITransport
27+
internal sealed class DropboxRequestHandler: ITransport
2928
{
3029
/// <summary>
3130
/// The API version
@@ -52,34 +51,21 @@ internal sealed class DropboxRequestHandler<TAuthError> : ITransport
5251
/// </summary>
5352
private readonly DropboxRequestHandlerOptions options;
5453

55-
/// <summary>
56-
/// The auth error decoder.
57-
/// </summary>
58-
private readonly IDecoder<TAuthError> authErrorDecoder;
59-
6054
/// <summary>
6155
/// Initializes a new instance of the <see cref="T:Dropbox.Api.DropboxRequestHandler"/> class.
6256
/// </summary>
6357
/// <param name="options">The configuration options for dropbox client.</param>
64-
/// <param name="authErrorDecoder">The auth error decoder.</param>
6558
/// <param name="selectUser">The member id of the selected user.</param>
6659
public DropboxRequestHandler(
6760
DropboxRequestHandlerOptions options,
68-
IDecoder<TAuthError> authErrorDecoder,
6961
string selectUser = null)
7062
{
7163
if (options == null)
7264
{
7365
throw new ArgumentNullException("options");
7466
}
7567

76-
if (authErrorDecoder == null)
77-
{
78-
throw new ArgumentNullException("authErrorDecoder");
79-
}
80-
8168
this.options = options;
82-
this.authErrorDecoder = authErrorDecoder;
8369
this.selectUser = selectUser;
8470
}
8571

@@ -138,7 +124,7 @@ async Task<TResponse> ITransport.SendRpcRequestAsync<TRequest, TResponse, TError
138124

139125
if (res.IsError)
140126
{
141-
throw ApiException<TError>.Decode(res.ObjectResult, errorDecoder);
127+
throw StructuredException<TError>.Decode<ApiException<TError>>(res.ObjectResult, errorDecoder);
142128
}
143129

144130
return JsonReader.Read(res.ObjectResult, resposneDecoder);
@@ -175,7 +161,7 @@ async Task<TResponse> ITransport.SendUploadRequestAsync<TRequest, TResponse, TEr
175161

176162
if (res.IsError)
177163
{
178-
throw ApiException<TError>.Decode(res.ObjectResult, errorDecoder);
164+
throw StructuredException<TError>.Decode<ApiException<TError>>(res.ObjectResult, errorDecoder);
179165
}
180166

181167
return JsonReader.Read(res.ObjectResult, resposneDecoder);
@@ -210,7 +196,7 @@ async Task<IDownloadResponse<TResponse>> ITransport.SendDownloadRequestAsync<TRe
210196

211197
if (res.IsError)
212198
{
213-
throw ApiException<TError>.Decode(res.ObjectResult, errorDecoder);
199+
throw StructuredException<TError>.Decode<ApiException<TError>>(res.ObjectResult, errorDecoder);
214200
}
215201

216202
var response = JsonReader.Read(res.ObjectResult, resposneDecoder);
@@ -414,7 +400,7 @@ private async Task<Result> RequestJsonString(
414400
else if (response.StatusCode == HttpStatusCode.Unauthorized)
415401
{
416402
var reason = await response.Content.ReadAsStringAsync();
417-
throw ApiException<TAuthError>.Decode(reason, this.authErrorDecoder);
403+
throw AuthException.Decode(reason);
418404
}
419405
else if ((int)response.StatusCode == 429)
420406
{

Dropbox.Api/DropboxRequestHandlerFactory.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)