Skip to content

Commit 3c3e415

Browse files
Correctly parses error codes with details messages in Firebase Auth. (#422)
* Correctly parses error codes with details messages in Firebase Auth. ``` { "error": { "errors": [ { "domain": "global", "reason": "invalid", "message": "INVALID_PHONE_NUMBER : Phone number is too short" } ], "code": 400, "message": "INVALID_PHONE_NUMBER : Phone number is too short" } } ``` The above will throw error code auth/invalid-phone-number and the message will be "Phone number is too short". * Adds CHANGELOG note for fix.
1 parent 25098bf commit 3c3e415

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- [fixed] Correctly parses error codes with details messages in Firebase Auth.
34
- [fixed] Fixed optional fields in UserRecord types to be optional.
45

56
# v6.4.0

src/utils/error.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,19 @@ export class FirebaseAuthError extends PrefixedFirebaseError {
148148
message?: string,
149149
rawServerResponse?: object,
150150
): FirebaseAuthError {
151+
// serverErrorCode could contain additional details:
152+
// ERROR_CODE : Detailed message which can also contain colons
153+
const colonSeparator = (serverErrorCode || '').indexOf(':');
154+
let customMessage = null;
155+
if (colonSeparator !== -1) {
156+
customMessage = serverErrorCode.substring(colonSeparator + 1).trim();
157+
serverErrorCode = serverErrorCode.substring(0, colonSeparator).trim();
158+
}
151159
// If not found, default to internal error.
152160
const clientCodeKey = AUTH_SERVER_TO_CLIENT_CODE[serverErrorCode] || 'INTERNAL_ERROR';
153161
const error: ErrorInfo = deepCopy((AuthClientErrorCode as any)[clientCodeKey]);
154-
error.message = message || error.message;
162+
// Server detailed message should have highest priority.
163+
error.message = customMessage || message || error.message;
155164

156165
if (clientCodeKey === 'INTERNAL_ERROR' && typeof rawServerResponse !== 'undefined') {
157166
try {
@@ -330,6 +339,10 @@ export class AuthClientErrorCode {
330339
code: 'claims-too-large',
331340
message: 'Developer claims maximum payload size exceeded.',
332341
};
342+
public static CONFIGURATION_NOT_FOUND = {
343+
code: 'configuration-not-found',
344+
message: 'There is no configuration corresponding to the provided identifier.',
345+
};
333346
public static ID_TOKEN_EXPIRED = {
334347
code: 'id-token-expired',
335348
message: 'The provided Firebase ID token is expired.',
@@ -680,8 +693,8 @@ export type ProjectManagementErrorCode =
680693
const AUTH_SERVER_TO_CLIENT_CODE: ServerToClientCode = {
681694
// Claims payload is too large.
682695
CLAIMS_TOO_LARGE: 'CLAIMS_TOO_LARGE',
683-
// Project not found.
684-
CONFIGURATION_NOT_FOUND: 'PROJECT_NOT_FOUND',
696+
// Configuration not found.
697+
CONFIGURATION_NOT_FOUND: 'CONFIGURATION_NOT_FOUND',
685698
// Provided credential has insufficient permissions.
686699
INSUFFICIENT_PERMISSION: 'INSUFFICIENT_PERMISSION',
687700
// ActionCodeSettings missing continue URL.

test/unit/utils/error.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ describe('FirebaseAuthError', () => {
8888
expect(error.code).to.be.equal('auth/internal-error');
8989
expect(error.message).to.be.equal('An internal error has occurred.');
9090
});
91+
92+
it('should initialize an error from an expected server with server detailed message', () => {
93+
// Error code should be separated from detailed message at first colon.
94+
const error = FirebaseAuthError.fromServerError('CONFIGURATION_NOT_FOUND : more details key: value');
95+
expect(error.code).to.be.equal('auth/configuration-not-found');
96+
expect(error.message).to.be.equal('more details key: value');
97+
});
9198
});
9299

93100
describe('with message specified', () => {
@@ -104,6 +111,14 @@ describe('FirebaseAuthError', () => {
104111
expect(error.code).to.be.equal('auth/internal-error');
105112
expect(error.message).to.be.equal('An unexpected error occurred.');
106113
});
114+
115+
it('should initialize an error from an expected server with server detailed message', () => {
116+
const error = FirebaseAuthError.fromServerError(
117+
'CONFIGURATION_NOT_FOUND : more details',
118+
'Ignored message');
119+
expect(error.code).to.be.equal('auth/configuration-not-found');
120+
expect(error.message).to.be.equal('more details');
121+
});
107122
});
108123

109124
describe('with raw server response specified', () => {

0 commit comments

Comments
 (0)