Skip to content

Commit

Permalink
[bug fix] ErrorUtils: handle edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Apr 19, 2024
1 parent 0d5aeb4 commit 83f2032
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions utils/ErrorUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,15 @@ describe('ErrorUtils', () => {
)
).toEqual('Random message');
});

it('Return string if error is sent as a string', () => {
expect(errorToUserFriendly('Payment timed out', false)).toEqual(
'Payment timed out'
);
});

it('To throw generic error if error is undefined', () => {
expect(errorToUserFriendly(undefined, false)).toEqual('Error');
});
});
});
8 changes: 5 additions & 3 deletions utils/ErrorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const userFriendlyErrors: any = {
FAILURE_REASON_INCORRECT_PAYMENT_DETAILS:
'error.failureReasonIncorrectPaymentDetails',
FAILURE_REASON_INSUFFICIENT_BALANCE:
'error.failureReasonInsufficientBalance'
'error.failureReasonInsufficientBalance',
Error: 'general.error'
};

const errorToUserFriendly = (error: Error, localize = true) => {
let errorMessage: string = error.message;
let errorMessage: string = error?.message;
let errorObject: any;

try {
Expand All @@ -28,7 +29,8 @@ const errorToUserFriendly = (error: Error, localize = true) => {
errorObject?.error?.message ||
errorObject?.message ||
errorMessage ||
error;
error ||
'Error';

if (localize) {
const localeString = require('./LocaleUtils').localeString;
Expand Down

0 comments on commit 83f2032

Please sign in to comment.