Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/responseToError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ export function responseToError(response: any, body: any, url?: string): Firebas
};
}

let message = "HTTP Error: " + statusCode + ", " + (body.error.message || body.error);
let errorMessage = body.error.message || body.error;
if (typeof errorMessage === "string") {
errorMessage = errorMessage.replace("Enable it by visiting", "\n\nEnable it by visiting");
errorMessage = errorMessage.replace(
"If you enabled this API recently",
"\n\nIf you enabled this API recently",
);
Comment on lines +43 to +47
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation using String.prototype.replace() with a string literal only replaces the first occurrence of the target text. While it is unlikely that these specific phrases appear multiple times in a single error message, using a global regular expression is more robust and ensures all occurrences are formatted as intended. Additionally, using a single regex with a capture group simplifies the logic and handles both phrases in one pass.

Suggested change
errorMessage = errorMessage.replace("Enable it by visiting", "\n\nEnable it by visiting");
errorMessage = errorMessage.replace(
"If you enabled this API recently",
"\n\nIf you enabled this API recently",
);
errorMessage = errorMessage.replace(/(Enable it by visiting|If you enabled this API recently)/g, "\n\n$1");

}

let message = "HTTP Error: " + statusCode + ", " + errorMessage;
if (url) {
message = "Request to " + url + " had " + message;
}
Expand Down