Skip to content

Commit 96df388

Browse files
authored
ID-1051: Error handling improvements
1 parent 87f83b8 commit 96df388

File tree

18 files changed

+69
-31
lines changed

18 files changed

+69
-31
lines changed

packages/passport/sdk-sample-app/src/components/imx/Order.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ function Order({ showModal, setShowModal }: ModalProps) {
108108
tokenId: asset.token_id,
109109
tokenAddress: asset.token_address,
110110
},
111+
fees: [{
112+
address: '0x8e70719571e87a328696ad099a7d9f6adc120892',
113+
fee_percentage: 1,
114+
}],
111115
};
112116
try {
113117
await imxProvider.createOrder(request);

packages/passport/sdk-sample-app/src/components/imx/Trade.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,21 @@ function Trade({ showModal: showTradeModal, setShowModal: setShowTradeModal }: M
5454
const request: GetSignableTradeRequest = {
5555
order_id: id,
5656
user,
57+
fees: [{
58+
address: '0x8e70719571e87a328696ad099a7d9f6adc120892',
59+
fee_percentage: 1,
60+
}],
5761
};
5862
const createTradeResponse = await imxProvider?.createTrade(request);
5963
if (createTradeResponse) {
6064
setLoadingTrade(false);
6165
setTradeIndex(null);
62-
addMessage(`Successfully purchased with Order ID ${id}`);
66+
addMessage('CreateTrade', `Successfully purchased with Order ID ${id}`);
6367
handleCloseTrade();
6468
}
6569
} catch (err) {
6670
if (err instanceof Error) {
67-
addMessage(err.toString());
71+
addMessage('CreateTrade', err);
6872
handleCloseTrade();
6973
}
7074
}

packages/passport/sdk-sample-app/src/components/zkevm/Request.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function Request({ showModal, setShowModal }: ModalProps) {
147147
try {
148148
const result = await zkEvmProvider?.request(request);
149149
setLoadingRequest(false);
150-
addMessage(selectedEthMethod?.name, result);
150+
addMessage(request.method, result);
151151
handleClose();
152152
} catch (err) {
153153
addMessage('Request', err);

packages/passport/sdk-sample-app/src/context/PassportProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function PassportProvider({
129129
setZkEvmProvider(undefined);
130130
} catch (err) {
131131
if (err instanceof Error) {
132-
addMessage(err.toString());
132+
addMessage('Logout', err);
133133
}
134134
console.error(err);
135135
} finally {

packages/passport/sdk-sample-app/src/context/StatusProvider.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {
22
createContext, useCallback, useContext, useMemo, useState,
33
} from 'react';
4+
import { PassportError } from '@imtbl/passport';
45

56
const MessageContext = createContext<{
67
messages: string[],
@@ -21,12 +22,17 @@ export function StatusProvider({
2122
const [isLoading, setIsLoading] = useState(false);
2223

2324
const addMessage = useCallback((operation: string, ...args: any[]) => {
24-
const messageString = args.map((arg) => {
25-
if (arg instanceof Error) {
26-
return arg.toString();
27-
}
28-
return JSON.stringify(arg, null, 2);
29-
}).join(': ');
25+
let messageString: string;
26+
if (args[0] instanceof PassportError) {
27+
messageString = `${args[0].type}: ${args[0].message}`;
28+
} else {
29+
messageString = args.map((arg) => {
30+
if (arg instanceof Error) {
31+
return arg.toString();
32+
}
33+
return JSON.stringify(arg, null, 2);
34+
}).join(': ');
35+
}
3036
setMessages((prevMessages) => [...prevMessages, `${operation}: ${messageString}`]);
3137
}, []);
3238

packages/passport/sdk/src/authManager.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ describe('AuthManager', () => {
225225

226226
await expect(() => authManager.login()).rejects.toThrow(
227227
new PassportError(
228-
`${PassportErrorType.AUTHENTICATION_ERROR}: ${mockErrorMsg}`,
228+
mockErrorMsg,
229229
PassportErrorType.AUTHENTICATION_ERROR,
230230
),
231231
);
@@ -308,7 +308,7 @@ describe('AuthManager', () => {
308308

309309
await expect(() => manager.logout()).rejects.toThrow(
310310
new PassportError(
311-
`${PassportErrorType.LOGOUT_ERROR}: ${mockErrorMsg}`,
311+
mockErrorMsg,
312312
PassportErrorType.LOGOUT_ERROR,
313313
),
314314
);

packages/passport/sdk/src/confirmation/confirmation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('confirmation', () => {
8181
confirmationScreen.loading();
8282

8383
expect(res.confirmed).toEqual(false);
84-
expect(mockNewWindow.location.href).toEqual('https://passport.sandbox.immutable.com/transaction-confirmation/transaction.html?transactionId=transactionId123&imxEtherAddress=0x1234&chainType=starkex');
84+
expect(mockNewWindow.location.href).toEqual('https://passport.sandbox.immutable.com/transaction-confirmation/transaction?transactionId=transactionId123&imxEtherAddress=0x1234&chainType=starkex');
8585
});
8686
});
8787

packages/passport/sdk/src/confirmation/confirmation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class ConfirmationScreen {
6262
let href = '';
6363
if (chainType === TransactionApprovalRequestChainTypeEnum.Starkex) {
6464
// eslint-disable-next-line max-len
65-
href = `${this.config.passportDomain}/transaction-confirmation/transaction.html?transactionId=${transactionId}&imxEtherAddress=${imxEtherAddress}&chainType=starkex`;
65+
href = `${this.config.passportDomain}/transaction-confirmation/transaction?transactionId=${transactionId}&imxEtherAddress=${imxEtherAddress}&chainType=starkex`;
6666
} else {
6767
// eslint-disable-next-line max-len
6868
href = `${this.config.passportDomain}/transaction-confirmation/zkevm?transactionId=${transactionId}&imxEtherAddress=${imxEtherAddress}&chainType=evm&chainId=${chainId}`;
@@ -116,7 +116,7 @@ export default class ConfirmationScreen {
116116
}
117117

118118
this.confirmationWindow = openPopupCenter({
119-
url: `${this.config.passportDomain}/transaction-confirmation/loading.html`,
119+
url: `${this.config.passportDomain}/transaction-confirmation/loading`,
120120
title: CONFIRMATION_WINDOW_TITLE,
121121
width: popupOptions?.width || CONFIRMATION_WINDOW_WIDTH,
122122
height: popupOptions?.height || CONFIRMATION_WINDOW_HEIGHT,

packages/passport/sdk/src/errors/passportError.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('passportError', () => {
2626
withPassportError(errorFunction, PassportErrorType.AUTHENTICATION_ERROR),
2727
).rejects.toThrow(
2828
new PassportError(
29-
'AUTHENTICATION_ERROR: SOMETHINGWRONG',
29+
'SOMETHINGWRONG',
3030
PassportErrorType.AUTHENTICATION_ERROR,
3131
),
3232
);

packages/passport/sdk/src/errors/passportError.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { isAxiosError } from 'axios';
2+
import { APIError } from '@imtbl/core-sdk';
3+
14
export enum PassportErrorType {
25
AUTHENTICATION_ERROR = 'AUTHENTICATION_ERROR',
36
INVALID_CONFIGURATION = 'INVALID_CONFIGURATION',
@@ -15,6 +18,10 @@ export enum PassportErrorType {
1518
OPERATION_NOT_SUPPORTED_ERROR = 'OPERATION_NOT_SUPPORTED_ERROR',
1619
}
1720

21+
function isAPIError(error: any): error is APIError {
22+
return 'code' in error && 'message' in error;
23+
}
24+
1825
export class PassportError extends Error {
1926
public type: PassportErrorType;
2027

@@ -31,7 +38,14 @@ export const withPassportError = async <T>(
3138
try {
3239
return await fn();
3340
} catch (error) {
34-
const errorMessage = `${customErrorType}: ${(error as Error).message}` || 'UnknownError';
41+
let errorMessage: string;
42+
43+
if (isAxiosError(error) && error.response?.data && isAPIError(error.response.data)) {
44+
errorMessage = error.response.data.message;
45+
} else {
46+
errorMessage = (error as Error).message;
47+
}
48+
3549
throw new PassportError(errorMessage, customErrorType);
3650
}
3751
};

0 commit comments

Comments
 (0)