MCPcopy Create free account
hub / github.com/firebase/firebase-tools / responseToError

Function responseToError

src/responseToError.ts:5–64  ·  view source on GitHub ↗
(response: any, body: any, url?: string)

Source from the content-addressed store, hash-verified

3import { FirebaseError } from "./error";
4
5export function responseToError(response: any, body: any, url?: string): FirebaseError | undefined {
6 const statusCode: number = (response.statusCode || response.status) as number;
7 if (statusCode < 400) {
8 return;
9 }
10 if (typeof body === "string") {
11 if (statusCode === 404) {
12 body = {
13 error: {
14 message: "Not Found",
15 },
16 };
17 } else {
18 body = {
19 error: {
20 message: body,
21 },
22 };
23 }
24 }
25
26 if (typeof body !== "object") {
27 try {
28 body = JSON.parse(body);
29 } catch (e) {
30 body = {};
31 }
32 }
33
34 if (!body.error) {
35 const errMessage = statusCode === 404 ? "Not Found" : "Unknown Error";
36 body.error = {
37 message: errMessage,
38 };
39 }
40
41 let message = "HTTP Error: " + statusCode + ", " + (body.error.message || body.error);
42 if (url) {
43 message = "Request to " + url + " had " + message;
44 }
45
46 let exitCode;
47 if (statusCode >= 500) {
48 // 5xx errors are unexpected
49 exitCode = 2;
50 } else {
51 // 4xx errors happen sometimes
52 exitCode = 1;
53 }
54
55 _.unset(response, "request.headers");
56 return new FirebaseError(message, {
57 context: {
58 body: body,
59 response: response,
60 },
61 exit: exitCode,
62 status: statusCode,

Callers 4

doRequestMethod · 0.90
profilerFunction · 0.90
database-get.tsFile · 0.90

Calls 2

parseMethod · 0.80
unsetMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…