|
| 1 | +/** |
| 2 | + * Jwt error code structure. |
| 3 | + * |
| 4 | + * @param code - The error code. |
| 5 | + * @param message - The error message. |
| 6 | + * @constructor |
| 7 | + */ |
| 8 | +export class JwtError extends Error { |
| 9 | + constructor(readonly code: JwtErrorCode, readonly message: string) { |
| 10 | + super(message); |
| 11 | + (this as any).__proto__ = JwtError.prototype; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * JWT error codes. |
| 17 | + */ |
| 18 | +export enum JwtErrorCode { |
| 19 | + INVALID_ARGUMENT = "invalid-argument", |
| 20 | + INVALID_CREDENTIAL = "invalid-credential", |
| 21 | + TOKEN_EXPIRED = "token-expired", |
| 22 | + INVALID_SIGNATURE = "invalid-token", |
| 23 | + NO_MATCHING_KID = "no-matching-kid-error", |
| 24 | + NO_KID_IN_HEADER = "no-kid-error", |
| 25 | + KEY_FETCH_ERROR = "key-fetch-error", |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * App client error codes and their default messages. |
| 30 | + */ |
| 31 | +export class AppErrorCodes { |
| 32 | + public static INVALID_CREDENTIAL = "invalid-credential"; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Auth client error codes and their default messages. |
| 37 | + */ |
| 38 | +export class AuthClientErrorCode { |
| 39 | + public static INVALID_ARGUMENT = { |
| 40 | + code: "argument-error", |
| 41 | + message: "Invalid argument provided.", |
| 42 | + }; |
| 43 | + public static INVALID_CREDENTIAL = { |
| 44 | + code: "invalid-credential", |
| 45 | + message: "Invalid credential object provided.", |
| 46 | + }; |
| 47 | + public static ID_TOKEN_EXPIRED = { |
| 48 | + code: "id-token-expired", |
| 49 | + message: "The provided Firebase ID token is expired.", |
| 50 | + }; |
| 51 | + public static ID_TOKEN_REVOKED = { |
| 52 | + code: "id-token-revoked", |
| 53 | + message: "The Firebase ID token has been revoked.", |
| 54 | + }; |
| 55 | + public static INTERNAL_ERROR = { |
| 56 | + code: "internal-error", |
| 57 | + message: "An internal error has occurred.", |
| 58 | + }; |
| 59 | + public static USER_NOT_FOUND = { |
| 60 | + code: "user-not-found", |
| 61 | + message: |
| 62 | + "There is no user record corresponding to the provided identifier.", |
| 63 | + }; |
| 64 | + public static USER_DISABLED = { |
| 65 | + code: "user-disabled", |
| 66 | + message: "The user record is disabled.", |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * `FirebaseErrorInterface` is a subclass of the standard JavaScript `Error` object. In |
| 72 | + * addition to a message string and stack trace, it contains a string code. |
| 73 | + */ |
| 74 | +export interface FirebaseErrorInterface { |
| 75 | + /** |
| 76 | + * Error codes are strings using the following format: `"service/string-code"`. |
| 77 | + * Some examples include `"auth/invalid-uid"` and |
| 78 | + * `"messaging/invalid-recipient"`. |
| 79 | + * |
| 80 | + * While the message for a given error can change, the code will remain the same |
| 81 | + * between backward-compatible versions of the Firebase SDK. |
| 82 | + */ |
| 83 | + code: string; |
| 84 | + |
| 85 | + /** |
| 86 | + * An explanatory message for the error that just occurred. |
| 87 | + * |
| 88 | + * This message is designed to be helpful to you, the developer. Because |
| 89 | + * it generally does not convey meaningful information to end users, |
| 90 | + * this message should not be displayed in your application. |
| 91 | + */ |
| 92 | + message: string; |
| 93 | + |
| 94 | + /** |
| 95 | + * A string value containing the execution backtrace when the error originally |
| 96 | + * occurred. |
| 97 | + * |
| 98 | + * This information can be useful for troubleshooting the cause of the error with |
| 99 | + * {@link https://firebase.google.com/support | Firebase Support}. |
| 100 | + */ |
| 101 | + stack?: string; |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns a JSON-serializable object representation of this error. |
| 105 | + * |
| 106 | + * @returns A JSON-serializable representation of this object. |
| 107 | + */ |
| 108 | + toJSON(): object; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Firebase error code structure. This extends Error. |
| 113 | + * |
| 114 | + * @param errorInfo - The error information (code and message). |
| 115 | + * @constructor |
| 116 | + */ |
| 117 | +export class FirebaseError extends Error implements FirebaseErrorInterface { |
| 118 | + constructor(private errorInfo: ErrorInfo) { |
| 119 | + super(errorInfo.message); |
| 120 | + |
| 121 | + /* tslint:disable:max-line-length */ |
| 122 | + // Set the prototype explicitly. See the following link for more details: |
| 123 | + // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work |
| 124 | + /* tslint:enable:max-line-length */ |
| 125 | + (this as any).__proto__ = FirebaseError.prototype; |
| 126 | + } |
| 127 | + |
| 128 | + /** @returns The error code. */ |
| 129 | + public get code(): string { |
| 130 | + return this.errorInfo.code; |
| 131 | + } |
| 132 | + |
| 133 | + /** @returns The error message. */ |
| 134 | + public get message(): string { |
| 135 | + return this.errorInfo.message; |
| 136 | + } |
| 137 | + |
| 138 | + /** @returns The object representation of the error. */ |
| 139 | + public toJSON(): object { |
| 140 | + return { |
| 141 | + code: this.code, |
| 142 | + message: this.message, |
| 143 | + }; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Defines error info type. This includes a code and message string. |
| 149 | + */ |
| 150 | +export interface ErrorInfo { |
| 151 | + code: string; |
| 152 | + message: string; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * A FirebaseError with a prefix in front of the error code. |
| 157 | + * |
| 158 | + * @param codePrefix - The prefix to apply to the error code. |
| 159 | + * @param code - The error code. |
| 160 | + * @param message - The error message. |
| 161 | + * @constructor |
| 162 | + */ |
| 163 | +export class PrefixedFirebaseError extends FirebaseError { |
| 164 | + constructor(private codePrefix: string, code: string, message: string) { |
| 165 | + super({ |
| 166 | + code: `${codePrefix}/${code}`, |
| 167 | + message, |
| 168 | + }); |
| 169 | + |
| 170 | + /* tslint:disable:max-line-length */ |
| 171 | + // Set the prototype explicitly. See the following link for more details: |
| 172 | + // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work |
| 173 | + /* tslint:enable:max-line-length */ |
| 174 | + (this as any).__proto__ = PrefixedFirebaseError.prototype; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Allows the error type to be checked without needing to know implementation details |
| 179 | + * of the code prefixing. |
| 180 | + * |
| 181 | + * @param code - The non-prefixed error code to test against. |
| 182 | + * @returns True if the code matches, false otherwise. |
| 183 | + */ |
| 184 | + public hasCode(code: string): boolean { |
| 185 | + return `${this.codePrefix}/${code}` === this.code; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + * Firebase Auth error code structure. This extends PrefixedFirebaseError. |
| 191 | + * |
| 192 | + * @param info - The error code info. |
| 193 | + * @param [message] The error message. This will override the default |
| 194 | + * message if provided. |
| 195 | + * @constructor |
| 196 | + */ |
| 197 | +export class FirebaseAuthError extends PrefixedFirebaseError { |
| 198 | + constructor(info: ErrorInfo, message?: string) { |
| 199 | + // Override default message if custom message provided. |
| 200 | + super("auth", info.code, message || info.message); |
| 201 | + |
| 202 | + /* tslint:disable:max-line-length */ |
| 203 | + // Set the prototype explicitly. See the following link for more details: |
| 204 | + // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work |
| 205 | + /* tslint:enable:max-line-length */ |
| 206 | + (this as any).__proto__ = FirebaseAuthError.prototype; |
| 207 | + } |
| 208 | +} |
0 commit comments