UNPKG

3.78 kBJavaScriptView Raw
1/*! firebase-admin v12.0.0 */
2"use strict";
3/*!
4 * Copyright 2019 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.getErrorCode = exports.createFirebaseError = void 0;
20const error_1 = require("../utils/error");
21const validator = require("../utils/validator");
22/**
23 * Creates a new FirebaseMessagingError by extracting the error code, message and other relevant
24 * details from an HTTP error response.
25 *
26 * @param err - The HttpError to convert into a Firebase error
27 * @returns A Firebase error that can be returned to the user.
28 */
29function createFirebaseError(err) {
30 if (err.response.isJson()) {
31 // For JSON responses, map the server response to a client-side error.
32 const json = err.response.data;
33 const errorCode = getErrorCode(json);
34 const errorMessage = getErrorMessage(json);
35 return error_1.FirebaseMessagingError.fromServerError(errorCode, errorMessage, json);
36 }
37 // Non-JSON response
38 let error;
39 switch (err.response.status) {
40 case 400:
41 error = error_1.MessagingClientErrorCode.INVALID_ARGUMENT;
42 break;
43 case 401:
44 case 403:
45 error = error_1.MessagingClientErrorCode.AUTHENTICATION_ERROR;
46 break;
47 case 500:
48 error = error_1.MessagingClientErrorCode.INTERNAL_ERROR;
49 break;
50 case 503:
51 error = error_1.MessagingClientErrorCode.SERVER_UNAVAILABLE;
52 break;
53 default:
54 // Treat non-JSON responses with unexpected status codes as unknown errors.
55 error = error_1.MessagingClientErrorCode.UNKNOWN_ERROR;
56 }
57 return new error_1.FirebaseMessagingError({
58 code: error.code,
59 message: `${error.message} Raw server response: "${err.response.text}". Status code: ` +
60 `${err.response.status}.`,
61 });
62}
63exports.createFirebaseError = createFirebaseError;
64/**
65 * @param response - The response to check for errors.
66 * @returns The error code if present; null otherwise.
67 */
68function getErrorCode(response) {
69 if (validator.isNonNullObject(response) && 'error' in response) {
70 const error = response.error;
71 if (validator.isString(error)) {
72 return error;
73 }
74 if (validator.isArray(error.details)) {
75 const fcmErrorType = 'type.googleapis.com/google.firebase.fcm.v1.FcmError';
76 for (const element of error.details) {
77 if (element['@type'] === fcmErrorType) {
78 return element.errorCode;
79 }
80 }
81 }
82 if ('status' in error) {
83 return error.status;
84 }
85 else {
86 return error.message;
87 }
88 }
89 return null;
90}
91exports.getErrorCode = getErrorCode;
92/**
93 * Extracts error message from the given response object.
94 *
95 * @param response - The response to check for errors.
96 * @returns The error message if present; null otherwise.
97 */
98function getErrorMessage(response) {
99 if (validator.isNonNullObject(response) &&
100 'error' in response &&
101 validator.isNonEmptyString(response.error.message)) {
102 return response.error.message;
103 }
104 return null;
105}