show http error text in console only and not in on the page itself w/ burnettk

This commit is contained in:
jasquat 2024-04-18 15:00:50 -04:00
parent 32141a238e
commit 1e9854f82a
No known key found for this signature in database
1 changed files with 35 additions and 3 deletions

View File

@ -44,6 +44,34 @@ export class UnexpectedResponseError extends Error {
}
}
const messageForHttpError = (statusCode: number, statusText: string) => {
let errorMessage = `HTTP Error ${statusCode}`;
if (statusText) {
errorMessage += `: ${statusText}`;
} else {
switch (statusCode) {
case 400:
errorMessage += ': Bad Request';
break;
case 401:
errorMessage += ': Unauthorized';
break;
case 403:
errorMessage += ': Forbidden';
break;
case 404:
errorMessage += ': Not Found';
break;
case 413:
errorMessage += ': Payload Too Large';
break;
default:
break;
}
}
return errorMessage;
};
const makeCallToBackend = ({
path,
successCallback,
@ -96,11 +124,15 @@ backendCallProps) => {
try {
jsonResult = JSON.parse(result.text);
} catch (error) {
if (error instanceof SyntaxError) {
throw new UnexpectedResponseError(
`Received unexpected response from server. Status: ${result.response.status}: ${result.response.statusText}. Body: ${result.text}`
const httpStatusMesage = messageForHttpError(
result.response.status,
result.response.statusText
);
const baseMessage = `Received unexpected response from server. Status: ${httpStatusMesage}.`;
if (error instanceof SyntaxError) {
throw new UnexpectedResponseError(baseMessage);
}
console.error(`${baseMessage} Body: ${result.text}`);
throw error;
}
if (result.response.status === 403) {