Skip to content

Commit

Permalink
Merge pull request #2274 from kaloudis/error-utils-pascal-case
Browse files Browse the repository at this point in the history
ErrorUtils: handle PascalCase error messages from LSPS1
  • Loading branch information
kaloudis authored Jul 8, 2024
2 parents 298720b + f912f35 commit d3cc12a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
12 changes: 8 additions & 4 deletions stores/LSPStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default class LSPStore {
} catch (e) {}
} else {
this.error = true;
this.error_msg = data.message;
this.error_msg = errorToUserFriendly(data.message);
// handle LSP geoblocking :(
if (
this.error_msg.includes(
Expand Down Expand Up @@ -381,7 +381,9 @@ export default class LSPStore {
if (data.error) {
this.error = true;
this.loading = false;
this.error_msg = data?.error?.data?.message;
this.error_msg = data?.error?.data?.message
? errorToUserFriendly(data?.error?.data?.message)
: '';
} else {
this.createOrderResponse = data;
this.loading = false;
Expand All @@ -390,7 +392,9 @@ export default class LSPStore {
if (data.error) {
this.error = true;
this.loading = false;
this.error_msg = data?.error?.message;
this.error_msg = data?.error?.message
? errorToUserFriendly(data?.error?.message)
: '';
} else {
this.getOrderResponse = data;
}
Expand Down Expand Up @@ -510,7 +514,7 @@ export default class LSPStore {
const responseData = JSON.parse(response.data);
if (responseData.error) {
this.error = true;
this.error_msg = responseData.message;
this.error_msg = errorToUserFriendly(responseData.message);
this.loading = false;
} else {
this.createOrderResponse = responseData;
Expand Down
9 changes: 9 additions & 0 deletions utils/ErrorUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,14 @@ describe('ErrorUtils', () => {
'Payment timed out'
);
});

it('Handles PascalCased LSP error messages', () => {
expect(
errorToUserFriendly(
'ChannelExpiryBlocksTooHighInCreateOrderRequest',
false
)
).toEqual('Channel expiry blocks too high in create order request');
});
});
});
24 changes: 17 additions & 7 deletions utils/ErrorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const userFriendlyErrors: any = {
Error: 'general.error'
};

const pascalCase = /^[A-Z](([a-z0-9]+[A-Z]?)*)$/;

const errorToUserFriendly = (error: Error, localize = true) => {
let errorMessage: string = error?.message;
let errorObject: any;
Expand All @@ -25,26 +27,34 @@ const errorToUserFriendly = (error: Error, localize = true) => {
// ignore - using original error message
}

const userFriendlyErrorMessage =
let errorMsg =
errorObject?.error?.message ||
errorObject?.message ||
errorMessage ||
error;

// Handle LSP spec error message formatting
if (pascalCase.test(errorMsg)) {
// remove capital demarcation with spaces, move all to lowercase
errorMsg = errorMsg
.split(/(?=[A-Z])/)
.join(' ')
.toLowerCase();
// capitalize first letter
errorMsg = errorMsg.charAt(0).toUpperCase() + errorMsg.slice(1);
}

if (localize) {
const localeString = require('./LocaleUtils').localeString;
return (
localeString(userFriendlyErrors[userFriendlyErrorMessage])?.replace(
localeString(userFriendlyErrors[errorMsg])?.replace(
'Zeus',
'ZEUS'
) || userFriendlyErrorMessage
) || errorMsg
);
} else {
const EN = require('../locales/en.json');
return (
EN[userFriendlyErrors[userFriendlyErrorMessage]] ||
userFriendlyErrorMessage
);
return EN[userFriendlyErrors[errorMsg]] || errorMsg;
}
};

Expand Down

0 comments on commit d3cc12a

Please sign in to comment.