always log body, and improve messaging

This commit is contained in:
burnettk 2024-04-18 16:24:39 -04:00
parent 1e9854f82a
commit 0bfc4b7f20
No known key found for this signature in database
2 changed files with 34 additions and 7 deletions

View File

@ -0,0 +1,13 @@
import HttpService from './HttpService';
test('it can use given http status message in messageForHttpError', () => {
expect(HttpService.messageForHttpError(400, 'Bad Request')).toEqual(
'HTTP Error 400: Bad Request'
);
});
test('it can figure out http status message if not given in messageForHttpError', () => {
expect(HttpService.messageForHttpError(400, '')).toEqual(
'HTTP Error 400: Bad Request'
);
});

View File

@ -49,25 +49,38 @@ const messageForHttpError = (statusCode: number, statusText: string) => {
if (statusText) {
errorMessage += `: ${statusText}`;
} else {
let httpTextForCode = '';
switch (statusCode) {
case 400:
errorMessage += ': Bad Request';
httpTextForCode = 'Bad Request';
break;
case 401:
errorMessage += ': Unauthorized';
httpTextForCode = 'Unauthorized';
break;
case 403:
errorMessage += ': Forbidden';
httpTextForCode = 'Forbidden';
break;
case 404:
errorMessage += ': Not Found';
httpTextForCode = 'Not Found';
break;
case 413:
errorMessage += ': Payload Too Large';
httpTextForCode = 'Payload Too Large';
break;
case 500:
httpTextForCode = 'Internal Server Error';
break;
case 502:
httpTextForCode = 'Bad Gateway';
break;
case 503:
httpTextForCode = 'Service Unavailable';
break;
default:
break;
}
if (httpTextForCode) {
errorMessage += `: ${httpTextForCode}`;
}
}
return errorMessage;
};
@ -128,11 +141,11 @@ backendCallProps) => {
result.response.status,
result.response.statusText
);
const baseMessage = `Received unexpected response from server. Status: ${httpStatusMesage}.`;
const baseMessage = `Received unexpected response from server. ${httpStatusMesage}.`;
console.error(`${baseMessage} Body: ${result.text}`);
if (error instanceof SyntaxError) {
throw new UnexpectedResponseError(baseMessage);
}
console.error(`${baseMessage} Body: ${result.text}`);
throw error;
}
if (result.response.status === 403) {
@ -180,6 +193,7 @@ backendCallProps) => {
const HttpService = {
HttpMethods,
makeCallToBackend,
messageForHttpError,
};
export default HttpService;