1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.getErrorCode = exports.createFirebaseError = void 0;
|
20 | const error_1 = require("../utils/error");
|
21 | const validator = require("../utils/validator");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function createFirebaseError(err) {
|
30 | if (err.response.isJson()) {
|
31 |
|
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 |
|
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 |
|
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 | }
|
63 | exports.createFirebaseError = createFirebaseError;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function 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 | }
|
91 | exports.getErrorCode = getErrorCode;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function 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 | }
|