firebase-admin
Version:
Firebase admin SDK for Node.js
93 lines (92 loc) • 3.04 kB
JavaScript
/*! firebase-admin v14.0.0 */
"use strict";
/*!
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirebaseError = void 0;
exports.toHttpResponse = toHttpResponse;
/**
* Maps a RequestResponse to a clean HttpResponse, preserving raw text if not JSON.
*
* @param resp - The RequestResponse to map.
* @returns A clean HttpResponse object.
* @internal
*/
function toHttpResponse(resp) {
return {
status: resp.status,
headers: resp.headers,
data: resp.isJson() ? resp.data : resp.text,
};
}
/**
* Firebase error code structure. This extends Error.
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class FirebaseError extends Error {
/**
* @param errorInfo - The error information (code and message).
*/
constructor(errorInfo) {
super(errorInfo.message);
this.code = errorInfo.code;
if (errorInfo.cause !== undefined) {
this.cause = errorInfo.cause;
}
if (errorInfo.httpResponse !== undefined) {
this.httpResponse = errorInfo.httpResponse;
}
}
/** {@inheritDoc FirebaseError.hasCode} */
hasCode(code) {
if (this.code === code) {
return true;
}
return this.codePrefix != null && this.code === `${this.codePrefix}/${code}`;
}
/** @returns The object representation of the error. */
toJSON() {
const json = {
code: this.code,
message: this.message,
};
if (this.httpResponse) {
json.httpResponse = {
status: this.httpResponse.status,
headers: this.httpResponse.headers,
data: this.httpResponse.data,
};
}
if (this.cause) {
json.cause = {
name: this.cause.name || 'Error',
message: this.cause.message || String(this.cause),
stack: this.cause.stack
};
if ('errors' in this.cause && Array.isArray(this.cause.errors)) {
json.cause.errors = this.cause.errors.map((e) => {
if (e instanceof Error) {
return { name: e.name, message: e.message, stack: e.stack };
}
return String(e);
});
}
}
return json;
}
}
exports.FirebaseError = FirebaseError;