Skip to content

Commit f8498f2

Browse files
author
Dominik Földi
committed
Simplify error handling by removing internal HttpErrorResponse and server to SDK error message mapping
1 parent 7d98076 commit f8498f2

File tree

2 files changed

+9
-92
lines changed

2 files changed

+9
-92
lines changed

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseUserManager.cs

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using FirebaseAdmin.Auth.Internal;
1615
using Google.Apis.Auth.OAuth2;
1716
using Google.Apis.Http;
18-
using Google.Apis.Json;
1917
using Newtonsoft.Json.Linq;
2018
using System;
21-
using System.Collections.Generic;
22-
using System.Collections.ObjectModel;
2319
using System.Net.Http;
2420
using System.Threading.Tasks;
2521

@@ -31,33 +27,10 @@ namespace FirebaseAdmin.Auth
3127
/// Google Identity Toolkit</a> via its REST API. This class does not hold any mutable state,
3228
/// and is thread safe.
3329
/// </summary>
34-
internal class FirebaseUserManager: IDisposable
30+
internal class FirebaseUserManager : IDisposable
3531
{
3632
private const string INTERNAL_ERROR = "internal-error";
3733

38-
// Map of server-side error codes to SDK error codes.
39-
// SDK error codes defined at: https://firebase.google.com/docs/auth/admin/errors
40-
private static readonly IReadOnlyDictionary<string, string> _errorCodes =
41-
new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
42-
{
43-
{ "CLAIMS_TOO_LARGE", "claims-too-large" },
44-
{ "CONFIGURATION_NOT_FOUND", "project-not-found" },
45-
{ "INSUFFICIENT_PERMISSION", "insufficient-permission" },
46-
{ "DUPLICATE_EMAIL", "email-already-exists" },
47-
{ "DUPLICATE_LOCAL_ID", "uid-already-exists" },
48-
{ "EMAIL_EXISTS", "email-already-exists" },
49-
{ "INVALID_CLAIMS", "invalid-claims" },
50-
{ "INVALID_EMAIL", "invalid-email" },
51-
{ "INVALID_PAGE_SELECTION", "invalid-page-token" },
52-
{ "INVALID_PHONE_NUMBER", "invalid-phone-number" },
53-
{ "PHONE_NUMBER_EXISTS", "phone-number-already-exists" },
54-
{ "PROJECT_NOT_FOUND", "project-not-found" },
55-
{ "USER_NOT_FOUND", "user-not-found" },
56-
{ "WEAK_PASSWORD", "invalid-password" },
57-
{ "UNAUTHORIZED_DOMAIN", "unauthorized-continue-uri" },
58-
{ "INVALID_DYNAMIC_LINK_DOMAIN", "invalid-dynamic-link-domain" }
59-
});
60-
6134
private const string ID_TOOLKIT_URL = "https://identitytoolkit.googleapis.com/v1/projects/{0}";
6235

6336
private readonly ConfigurableHttpClient _httpClient;
@@ -93,48 +66,24 @@ private async Task<JObject> PostAsync(string path, UserRecord user)
9366
try
9467
{
9568
response = await _httpClient.PostJsonAsync(requestUri, user, default);
69+
var json = await response.Content.ReadAsStringAsync();
9670

9771
if (response.IsSuccessStatusCode)
9872
{
99-
return JObject.Parse(await response.Content.ReadAsStringAsync());
73+
return JObject.Parse(json);
10074
}
10175
else
10276
{
103-
await HandleHttpError(response);
104-
}
105-
}
106-
catch (Exception)
107-
{
108-
throw new FirebaseException(INTERNAL_ERROR);
109-
}
110-
finally
111-
{
112-
if (response != null)
113-
{
114-
response.Dispose();
77+
var error = "Response status code does not indicate success: "
78+
+ $"{(int)response.StatusCode} ({response.StatusCode})"
79+
+ $"{Environment.NewLine}{json}";
80+
throw new FirebaseException(error);
11581
}
11682
}
117-
118-
return null;
119-
}
120-
121-
private async Task HandleHttpError(HttpResponseMessage response)
122-
{
123-
try
83+
catch (HttpRequestException e)
12484
{
125-
var errorResponse =
126-
NewtonsoftJsonSerializer.Instance.Deserialize<HttpErrorResponse>(await response.Content.ReadAsStringAsync());
127-
if (_errorCodes.TryGetValue(errorResponse.ErrorCode, out var code))
128-
{
129-
throw new FirebaseException(code);
130-
};
131-
}
132-
catch (Exception)
133-
{
134-
// Ignored
85+
throw new FirebaseException("Error while calling Firebase Auth service", e);
13586
}
136-
137-
throw new FirebaseException(INTERNAL_ERROR);
13887
}
13988

14089
internal static FirebaseUserManager Create(FirebaseApp app)

FirebaseAdmin/FirebaseAdmin/Auth/Internal/HttpErrorResponse.cs

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

0 commit comments

Comments
 (0)