Skip to content

Commit 4582eed

Browse files
committed
Migrated the remaining auth APIs to new error handling model
1 parent f0f7507 commit 4582eed

File tree

5 files changed

+210
-60
lines changed

5 files changed

+210
-60
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseUserManagerTest.cs

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,14 @@ public async Task GetUserByIdUserNotFound()
171171
};
172172
var auth = this.CreateFirebaseAuth(handler);
173173

174-
var exception = await Assert.ThrowsAsync<FirebaseException>(
174+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
175175
async () => await auth.GetUserAsync("user1"));
176+
177+
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
178+
Assert.Equal(AuthErrorCode.UserNotFound, exception.AuthErrorCode);
176179
Assert.Equal("Failed to get user with uid: user1", exception.Message);
180+
Assert.NotNull(exception.HttpResponse);
181+
Assert.Null(exception.InnerException);
177182
}
178183

179184
[Fact]
@@ -228,9 +233,14 @@ public async Task GetUserByEmailUserNotFound()
228233
};
229234
var auth = this.CreateFirebaseAuth(handler);
230235

231-
var exception = await Assert.ThrowsAsync<FirebaseException>(
236+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
232237
async () => await auth.GetUserByEmailAsync("[email protected]"));
238+
239+
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
240+
Assert.Equal(AuthErrorCode.UserNotFound, exception.AuthErrorCode);
233241
Assert.Equal("Failed to get user with email: [email protected]", exception.Message);
242+
Assert.NotNull(exception.HttpResponse);
243+
Assert.Null(exception.InnerException);
234244
}
235245

236246
[Fact]
@@ -285,9 +295,14 @@ public async Task GetUserByPhoneNumberUserNotFound()
285295
};
286296
var auth = this.CreateFirebaseAuth(handler);
287297

288-
var exception = await Assert.ThrowsAsync<FirebaseException>(
298+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
289299
async () => await auth.GetUserByPhoneNumberAsync("+1234567890"));
300+
301+
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
302+
Assert.Equal(AuthErrorCode.UserNotFound, exception.AuthErrorCode);
290303
Assert.Equal("Failed to get user with phone number: +1234567890", exception.Message);
304+
Assert.NotNull(exception.HttpResponse);
305+
Assert.Null(exception.InnerException);
291306
}
292307

293308
[Fact]
@@ -641,11 +656,21 @@ public async Task ListUsersHttpError()
641656
var handler = new MockMessageHandler()
642657
{
643658
StatusCode = HttpStatusCode.InternalServerError,
659+
Response = "{}",
644660
};
645661
var auth = this.CreateFirebaseAuth(handler);
646662

647663
var pagedEnumerable = auth.ListUsersAsync(null);
648-
await Assert.ThrowsAsync<FirebaseException>(async () => await pagedEnumerable.First());
664+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
665+
async () => await pagedEnumerable.First());
666+
667+
Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
668+
Assert.Null(exception.AuthErrorCode);
669+
Assert.Equal(
670+
"Unexpected HTTP response with status: 500 (InternalServerError)\n{}",
671+
exception.Message);
672+
Assert.NotNull(exception.HttpResponse);
673+
Assert.Null(exception.InnerException);
649674

650675
Assert.Single(handler.Requests);
651676
var query = this.ExtractQueryParams(handler.Requests[0]);
@@ -670,7 +695,17 @@ public async Task ListUsersIntermittentHttpError()
670695
}
671696

672697
handler.StatusCode = HttpStatusCode.InternalServerError;
673-
await Assert.ThrowsAsync<FirebaseException>(async () => await enumerator.MoveNext());
698+
handler.Response = "{}";
699+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
700+
async () => await enumerator.MoveNext());
701+
702+
Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
703+
Assert.Null(exception.AuthErrorCode);
704+
Assert.Equal(
705+
"Unexpected HTTP response with status: 500 (InternalServerError)\n{}",
706+
exception.Message);
707+
Assert.NotNull(exception.HttpResponse);
708+
Assert.Null(exception.InnerException);
674709

675710
Assert.Equal(2, handler.Requests.Count);
676711
var query = this.ExtractQueryParams(handler.Requests[0]);
@@ -907,9 +942,16 @@ public async Task CreateUserIncorrectResponse()
907942
Response = "{}",
908943
};
909944
var auth = this.CreateFirebaseAuth(handler);
910-
911945
var args = new UserRecordArgs();
912-
await Assert.ThrowsAsync<FirebaseException>(async () => await auth.CreateUserAsync(args));
946+
947+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
948+
async () => await auth.CreateUserAsync(args));
949+
950+
Assert.Equal(ErrorCode.Unknown, exception.ErrorCode);
951+
Assert.Equal(AuthErrorCode.UnexpectedResponse, exception.AuthErrorCode);
952+
Assert.Equal("Failed to create new user.", exception.Message);
953+
Assert.NotNull(exception.HttpResponse);
954+
Assert.Null(exception.InnerException);
913955
}
914956

915957
[Fact]
@@ -1309,7 +1351,14 @@ public async Task UpdateUserIncorrectResponseObject()
13091351
Uid = "user1",
13101352
};
13111353

1312-
await Assert.ThrowsAsync<FirebaseException>(async () => await auth.UpdateUserAsync(args));
1354+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
1355+
async () => await auth.UpdateUserAsync(args));
1356+
1357+
Assert.Equal(ErrorCode.Unknown, exception.ErrorCode);
1358+
Assert.Equal(AuthErrorCode.UnexpectedResponse, exception.AuthErrorCode);
1359+
Assert.Equal("Failed to update user: user1", exception.Message);
1360+
Assert.NotNull(exception.HttpResponse);
1361+
Assert.Null(exception.InnerException);
13131362
}
13141363

13151364
[Fact]
@@ -1325,7 +1374,14 @@ public async Task UpdateUserIncorrectResponseUid()
13251374
Uid = "user1",
13261375
};
13271376

1328-
await Assert.ThrowsAsync<FirebaseException>(async () => await auth.UpdateUserAsync(args));
1377+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
1378+
async () => await auth.UpdateUserAsync(args));
1379+
1380+
Assert.Equal(ErrorCode.Unknown, exception.ErrorCode);
1381+
Assert.Equal(AuthErrorCode.UnexpectedResponse, exception.AuthErrorCode);
1382+
Assert.Equal("Failed to update user: user1", exception.Message);
1383+
Assert.NotNull(exception.HttpResponse);
1384+
Assert.Null(exception.InnerException);
13291385
}
13301386

13311387
[Fact]
@@ -1334,14 +1390,24 @@ public async Task UpdateUserHttpError()
13341390
var handler = new MockMessageHandler()
13351391
{
13361392
StatusCode = HttpStatusCode.InternalServerError,
1393+
Response = "{}",
13371394
};
13381395
var auth = this.CreateFirebaseAuth(handler);
13391396
var args = new UserRecordArgs()
13401397
{
13411398
Uid = "user1",
13421399
};
13431400

1344-
await Assert.ThrowsAsync<FirebaseException>(async () => await auth.UpdateUserAsync(args));
1401+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
1402+
async () => await auth.UpdateUserAsync(args));
1403+
1404+
Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
1405+
Assert.Null(exception.AuthErrorCode);
1406+
Assert.Equal(
1407+
"Unexpected HTTP response with status: 500 (InternalServerError)\n{}",
1408+
exception.Message);
1409+
Assert.NotNull(exception.HttpResponse);
1410+
Assert.Null(exception.InnerException);
13451411
}
13461412

13471413
[Fact]
@@ -1361,12 +1427,22 @@ public async Task DeleteUserNotFound()
13611427
{
13621428
var handler = new MockMessageHandler()
13631429
{
1364-
StatusCode = HttpStatusCode.NotFound,
1430+
StatusCode = HttpStatusCode.InternalServerError,
1431+
Response = @"{
1432+
""error"": {""message"": ""USER_NOT_FOUND""}
1433+
}",
13651434
};
13661435
var auth = this.CreateFirebaseAuth(handler);
13671436

1368-
await Assert.ThrowsAsync<FirebaseException>(
1437+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
13691438
async () => await auth.DeleteUserAsync("user1"));
1439+
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
1440+
Assert.Equal(AuthErrorCode.UserNotFound, exception.AuthErrorCode);
1441+
Assert.Equal(
1442+
"No user record found for the given identifier (USER_NOT_FOUND).",
1443+
exception.Message);
1444+
Assert.NotNull(exception.HttpResponse);
1445+
Assert.Null(exception.InnerException);
13701446
}
13711447

13721448
private FirebaseAuth CreateFirebaseAuth(HttpMessageHandler handler)

FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorCode.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,15 @@ public enum AuthErrorCode
2323
/// The user with the provided uid already exists.
2424
/// </summary>
2525
UidAlreadyExists,
26+
27+
/// <summary>
28+
/// Backend API responded with an unexpected message.
29+
/// </summary>
30+
UnexpectedResponse,
31+
32+
/// <summary>
33+
/// No user record found for the given identifier.
34+
/// </summary>
35+
UserNotFound,
2636
}
2737
}

FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorHandler.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ internal sealed class AuthErrorHandler : HttpErrorHandler
3535
AuthErrorCode.UidAlreadyExists,
3636
"The user with the provided uid already exists")
3737
},
38+
{
39+
"USER_NOT_FOUND",
40+
new ErrorInfo(
41+
ErrorCode.NotFound,
42+
AuthErrorCode.UserNotFound,
43+
"No user record found for the given identifier")
44+
},
3845
};
3946

4047
protected sealed override FirebaseExceptionArgs CreateExceptionArgs(

0 commit comments

Comments
 (0)