Skip to content

Commit

Permalink
[Auth] Pass through server error messages to the error object when av…
Browse files Browse the repository at this point in the history
…ailable (#5423)
  • Loading branch information
sam-gc committed Sep 1, 2021
1 parent bc3b405 commit 08ec55d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-mangos-refuse.md
@@ -0,0 +1,5 @@
---
"@firebase/auth": patch
---

Fix bug where custom errors from blocking functions were being dropped.
6 changes: 5 additions & 1 deletion packages/auth/src/api/errors.ts
Expand Up @@ -22,6 +22,7 @@ import { AuthErrorCode } from '../core/errors';
*/
export const enum ServerError {
ADMIN_ONLY_OPERATION = 'ADMIN_ONLY_OPERATION',
BLOCKING_FUNCTION_ERROR_RESPONSE = 'BLOCKING_FUNCTION_ERROR_RESPONSE',
CAPTCHA_CHECK_FAILED = 'CAPTCHA_CHECK_FAILED',
CORS_UNSUPPORTED = 'CORS_UNSUPPORTED',
CREDENTIAL_MISMATCH = 'CREDENTIAL_MISMATCH',
Expand Down Expand Up @@ -199,5 +200,8 @@ export const SERVER_ERROR_MAP: Partial<ServerErrorMap<ServerError>> = {
[ServerError.SECOND_FACTOR_EXISTS]:
AuthErrorCode.SECOND_FACTOR_ALREADY_ENROLLED,
[ServerError.SECOND_FACTOR_LIMIT_EXCEEDED]:
AuthErrorCode.SECOND_FACTOR_LIMIT_EXCEEDED
AuthErrorCode.SECOND_FACTOR_LIMIT_EXCEEDED,

// Blocking functions related errors.
[ServerError.BLOCKING_FUNCTION_ERROR_RESPONSE]: AuthErrorCode.INTERNAL_ERROR,
};
31 changes: 31 additions & 0 deletions packages/auth/src/api/index.test.ts
Expand Up @@ -198,6 +198,37 @@ describe('api/_performApiRequest', () => {
expect(mock.calls[0].request).to.eql(request);
});

it('should pass through server messages if applicable', async () => {
mockEndpoint(
Endpoint.SIGN_UP,
{
error: {
code: 400,
message: `${ServerError.BLOCKING_FUNCTION_ERROR_RESPONSE} : Text text text`,
errors: [
{
message: 'Text text text'
}
]
}
},
400
);
const promise = _performApiRequest<typeof request, typeof serverResponse>(
auth,
HttpMethod.POST,
Endpoint.SIGN_UP,
request
);
let error: FirebaseError;
try {
await promise;
} catch (e) {
error = e;
}
expect(error!.customData!.message).to.eql('Text text text');
});

it('should handle unknown server errors', async () => {
const mock = mockEndpoint(
Endpoint.SIGN_UP,
Expand Down
10 changes: 7 additions & 3 deletions packages/auth/src/api/index.ts
Expand Up @@ -152,7 +152,7 @@ export async function _performFetchWithErrorHandling<V>(
return json;
} else {
const errorMessage = response.ok ? json.errorMessage : json.error.message;
const serverErrorCode = errorMessage.split(' : ')[0] as ServerError;
const [serverErrorCode, serverErrorMessage] = errorMessage.split(' : ');
if (serverErrorCode === ServerError.FEDERATED_USER_ID_ALREADY_LINKED) {
throw _makeTaggedError(
auth,
Expand All @@ -163,11 +163,15 @@ export async function _performFetchWithErrorHandling<V>(
throw _makeTaggedError(auth, AuthErrorCode.EMAIL_EXISTS, json);
}
const authError =
errorMap[serverErrorCode] ||
errorMap[serverErrorCode as ServerError] ||
((serverErrorCode
.toLowerCase()
.replace(/[_\s]+/g, '-') as unknown) as AuthErrorCode);
_fail(auth, authError);
if (serverErrorMessage) {
_fail(auth, authError, {message: serverErrorMessage});
} else {
_fail(auth, authError);
}
}
} catch (e) {
if (e instanceof FirebaseError) {
Expand Down

0 comments on commit 08ec55d

Please sign in to comment.