show http error text in console only and not in on the page itself w/ burnettk
This commit is contained in:
parent
32141a238e
commit
1e9854f82a
|
@ -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 = ({
|
const makeCallToBackend = ({
|
||||||
path,
|
path,
|
||||||
successCallback,
|
successCallback,
|
||||||
|
@ -96,11 +124,15 @@ backendCallProps) => {
|
||||||
try {
|
try {
|
||||||
jsonResult = JSON.parse(result.text);
|
jsonResult = JSON.parse(result.text);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
const httpStatusMesage = messageForHttpError(
|
||||||
|
result.response.status,
|
||||||
|
result.response.statusText
|
||||||
|
);
|
||||||
|
const baseMessage = `Received unexpected response from server. Status: ${httpStatusMesage}.`;
|
||||||
if (error instanceof SyntaxError) {
|
if (error instanceof SyntaxError) {
|
||||||
throw new UnexpectedResponseError(
|
throw new UnexpectedResponseError(baseMessage);
|
||||||
`Received unexpected response from server. Status: ${result.response.status}: ${result.response.statusText}. Body: ${result.text}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
console.error(`${baseMessage} Body: ${result.text}`);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
if (result.response.status === 403) {
|
if (result.response.status === 403) {
|
||||||
|
|
Loading…
Reference in New Issue