Skip to content

Commit 1bf8625

Browse files
authored
Merge 8a5f9e8 into bc3b405
2 parents bc3b405 + 8a5f9e8 commit 1bf8625

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

packages/auth/src/api/errors.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AuthErrorCode } from '../core/errors';
2222
*/
2323
export const enum ServerError {
2424
ADMIN_ONLY_OPERATION = 'ADMIN_ONLY_OPERATION',
25+
BLOCKING_FUNCTION_ERROR_RESPONSE = 'BLOCKING_FUNCTION_ERROR_RESPONSE',
2526
CAPTCHA_CHECK_FAILED = 'CAPTCHA_CHECK_FAILED',
2627
CORS_UNSUPPORTED = 'CORS_UNSUPPORTED',
2728
CREDENTIAL_MISMATCH = 'CREDENTIAL_MISMATCH',
@@ -199,5 +200,8 @@ export const SERVER_ERROR_MAP: Partial<ServerErrorMap<ServerError>> = {
199200
[ServerError.SECOND_FACTOR_EXISTS]:
200201
AuthErrorCode.SECOND_FACTOR_ALREADY_ENROLLED,
201202
[ServerError.SECOND_FACTOR_LIMIT_EXCEEDED]:
202-
AuthErrorCode.SECOND_FACTOR_LIMIT_EXCEEDED
203+
AuthErrorCode.SECOND_FACTOR_LIMIT_EXCEEDED,
204+
205+
// Blocking functions related errors.
206+
[ServerError.BLOCKING_FUNCTION_ERROR_RESPONSE]: AuthErrorCode.INTERNAL_ERROR,
203207
};

packages/auth/src/api/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,37 @@ describe('api/_performApiRequest', () => {
198198
expect(mock.calls[0].request).to.eql(request);
199199
});
200200

201+
it('should pass through server messages if applicable', async () => {
202+
mockEndpoint(
203+
Endpoint.SIGN_UP,
204+
{
205+
error: {
206+
code: 400,
207+
message: `${ServerError.BLOCKING_FUNCTION_ERROR_RESPONSE} : Text text text`,
208+
errors: [
209+
{
210+
message: 'Text text text'
211+
}
212+
]
213+
}
214+
},
215+
400
216+
);
217+
const promise = _performApiRequest<typeof request, typeof serverResponse>(
218+
auth,
219+
HttpMethod.POST,
220+
Endpoint.SIGN_UP,
221+
request
222+
);
223+
let error: FirebaseError;
224+
try {
225+
await promise;
226+
} catch (e) {
227+
error = e;
228+
}
229+
expect(error!.customData!.message).to.eql('Text text text');
230+
});
231+
201232
it('should handle unknown server errors', async () => {
202233
const mock = mockEndpoint(
203234
Endpoint.SIGN_UP,

packages/auth/src/api/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export async function _performFetchWithErrorHandling<V>(
152152
return json;
153153
} else {
154154
const errorMessage = response.ok ? json.errorMessage : json.error.message;
155-
const serverErrorCode = errorMessage.split(' : ')[0] as ServerError;
155+
const [serverErrorCode, serverErrorMessage] = errorMessage.split(' : ');
156156
if (serverErrorCode === ServerError.FEDERATED_USER_ID_ALREADY_LINKED) {
157157
throw _makeTaggedError(
158158
auth,
@@ -163,11 +163,15 @@ export async function _performFetchWithErrorHandling<V>(
163163
throw _makeTaggedError(auth, AuthErrorCode.EMAIL_EXISTS, json);
164164
}
165165
const authError =
166-
errorMap[serverErrorCode] ||
166+
errorMap[serverErrorCode as ServerError] ||
167167
((serverErrorCode
168168
.toLowerCase()
169169
.replace(/[_\s]+/g, '-') as unknown) as AuthErrorCode);
170-
_fail(auth, authError);
170+
if (serverErrorMessage) {
171+
_fail(auth, authError, {message: serverErrorMessage});
172+
} else {
173+
_fail(auth, authError);
174+
}
171175
}
172176
} catch (e) {
173177
if (e instanceof FirebaseError) {

0 commit comments

Comments
 (0)