Skip to content

Commit

Permalink
add status library
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Dec 22, 2024
1 parent 6432548 commit 5176767
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 57 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
"file-saver": "^2.0.5",
"history": "^5.1.0",
"lodash": "^4.17.5",
"http-status-codes": "^2.3.0",
"moment": "^2.29.1",
"prop-types": "^15.5.10",
"query-string": "^6.1.0",
Expand Down
45 changes: 0 additions & 45 deletions src/constants/errorStatuses.js

This file was deleted.

1 change: 0 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export * from './inAppActions';
export * from '../utils/date';
export * from './files';
export * from './logsActions';
export * from './errorStatuses';
export * from './moduleNames';
20 changes: 13 additions & 7 deletions src/hooks/useErrorMessages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useIntl } from 'react-intl';
import { useShowCallout } from '@folio/stripes-acq-components';
import { getReasonPhrase } from 'http-status-codes';

import { ERRORS, ERROR_STATUSES } from '../constants';
import { ERRORS } from '../constants';
import { useSearchParams } from './useSearchParams';


Expand Down Expand Up @@ -43,15 +44,20 @@ export const useErrorMessages = () => {

const showExternalModuleError = (moduleName, error) => {
const status = error?.status ?? 500;
const message = error?.message;
const initialErrorMessage = error?.message;

const statusError = ERROR_STATUSES[status];
const messageError = ERROR_STATUSES[message]; // Some modules return the error message as a known status
let displayMessage = '';

// Determine message details based on priority: messageError > message > statusError
const messageDetails = messageError || message || statusError;
try {
displayMessage = getReasonPhrase(initialErrorMessage); // Some modules return the error message as a known status
} catch {
const statusErrorMessage = getReasonPhrase(status);

showError(`${moduleName} returns status code: ${status} - ${messageDetails}.`);
// Determine displayMessage details based on priority: initialErrorMessage > statusErrorMessage
displayMessage = initialErrorMessage || statusErrorMessage;
}

showError(`${moduleName} returns status code: ${status} - ${displayMessage}.`);
};

return {
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useErrorMessages.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { renderHook } from '@testing-library/react-hooks';
import { useIntl } from 'react-intl';
import { getReasonPhrase } from 'http-status-codes';

import { useShowCallout } from '@folio/stripes-acq-components';

import { useErrorMessages } from './useErrorMessages';
import { ERROR_STATUSES, ERRORS } from '../constants';
import { ERRORS } from '../constants';

jest.mock('react-intl', () => ({
useIntl: jest.fn(),
Expand Down Expand Up @@ -125,7 +128,7 @@ describe('useErrorMessages', () => {
expect(showCalloutMock)
.toHaveBeenCalledWith({
type: 'error',
message: `TestModule returns status code: 404 - ${ERROR_STATUSES[404]}.`,
message: `TestModule returns status code: 404 - ${getReasonPhrase(404)}.`,
});
});

Expand Down Expand Up @@ -166,7 +169,7 @@ describe('useErrorMessages', () => {
expect(showCalloutMock)
.toHaveBeenCalledWith({
type: 'error',
message: `TestModule returns status code: 418 - ${ERROR_STATUSES[418]}.`,
message: `TestModule returns status code: 418 - ${getReasonPhrase(418)}.`,
});
});

Expand All @@ -178,7 +181,7 @@ describe('useErrorMessages', () => {
expect(showCalloutMock)
.toHaveBeenCalledWith({
type: 'error',
message: `TestModule returns status code: 500 - ${ERROR_STATUSES[500]}.`,
message: `TestModule returns status code: 500 - ${getReasonPhrase(500)}.`,
});
});

Expand Down

0 comments on commit 5176767

Please sign in to comment.