Skip to content

Commit

Permalink
feat: WT-2076 - expose root raw error (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrearampin authored Jan 22, 2024
1 parent ed8730d commit 1c3ed35
Show file tree
Hide file tree
Showing 28 changed files with 116 additions and 115 deletions.
5 changes: 3 additions & 2 deletions packages/checkout/sdk/src/availability/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ export const availabilityService = (

try {
response = await axios.post(`${postEndpoint()}/v1/availability/checkout/swap`);
} catch (error: any) {
} catch (err: any) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
response = error.response;
response = err.response;

// If 403 then the service is geo-blocked
if (response.status === 403) return false;

throw new CheckoutError(
`Error fetching from api: ${response.status} ${response.statusText}`,
CheckoutErrorType.API_ERROR,
{ error: err },
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/checkout/sdk/src/balances/balances.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ describe('balances', () => {

expect(message).toEqual(testCase.expectedErrorMessage);
expect(type).toEqual(CheckoutErrorType.GET_INDEXER_BALANCE_ERROR);
expect(data).toEqual({
expect(data.error).toEqual({
code: HttpStatusCode.Forbidden,
message: testCase.errorMessage,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/checkout/sdk/src/balances/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const getIndexerBalance = async (
throw new CheckoutError(
err.message || 'InternalServerError | getTokensByWalletAddress',
CheckoutErrorType.GET_INDEXER_BALANCE_ERROR,
err,
{ error: err },
);
}
}
Expand All @@ -172,7 +172,7 @@ export const getIndexerBalance = async (
throw new CheckoutError(
err.message || 'InternalServerError | getNativeTokenByWalletAddress',
CheckoutErrorType.GET_INDEXER_BALANCE_ERROR,
err,
{ error: err },
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/checkout/sdk/src/config/remoteConfigFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ export class RemoteConfigFetcher {
if (response.data && typeof response.data !== 'object') {
try {
responseData = JSON.parse(response.data);
} catch (jsonError) {
throw new CheckoutError('Invalid configuration', CheckoutErrorType.API_ERROR);
} catch (err: any) {
throw new CheckoutError(
'Invalid configuration',
CheckoutErrorType.API_ERROR,
{ error: err },
);
}
}

Expand All @@ -66,6 +70,7 @@ export class RemoteConfigFetcher {
throw new CheckoutError(
`Error: ${err.message}`,
CheckoutErrorType.API_ERROR,
{ error: err },
);
}

Expand All @@ -87,6 +92,7 @@ export class RemoteConfigFetcher {
throw new CheckoutError(
`Error: ${err.message}`,
CheckoutErrorType.API_ERROR,
{ error: err },
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/checkout/sdk/src/connect/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function checkIsWalletConnected(
'Check wallet connection request failed',
CheckoutErrorType.PROVIDER_REQUEST_FAILED_ERROR,
{
error: err,
rpcMethod: WalletAction.CHECK_CONNECTION,
},
);
Expand Down
6 changes: 4 additions & 2 deletions packages/checkout/sdk/src/errors/checkoutError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ export class CheckoutError extends Error {

public type: CheckoutErrorType;

public data?: { [key: string]: string };
public data?: { [key: string]: any };

constructor(
message: string,
type: CheckoutErrorType,
data?: { [key: string]: string },
data?: {
[key: string]: any
},
) {
super(message);
this.message = message;
Expand Down
1 change: 1 addition & 0 deletions packages/checkout/sdk/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export async function switchWalletNetwork(
throw new CheckoutError(
'User cancelled add network request',
CheckoutErrorType.USER_REJECTED_REQUEST_ERROR,
{ error: err },
);
}
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/checkout/sdk/src/provider/validateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function validateProvider(
throw new CheckoutError(
'Unable to detect the web3Provider network',
CheckoutErrorType.WEB3_PROVIDER_ERROR,
{ error: err },
);
}

Expand Down
10 changes: 7 additions & 3 deletions packages/checkout/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Checkout {
new CheckoutError(
'Failed to load widgets script',
CheckoutErrorType.WIDGETS_SCRIPT_LOAD_ERROR,
{ message: err.message },
{ error: err },
),
);
}
Expand Down Expand Up @@ -429,8 +429,12 @@ export class Checkout {
let itemRequirements = [];
try {
itemRequirements = await getItemRequirementsFromRequirements(web3Provider, params.itemRequirements);
} catch (error) {
throw new CheckoutError('Failed to map item requirements', CheckoutErrorType.ITEM_REQUIREMENTS_ERROR);
} catch (err: any) {
throw new CheckoutError(
'Failed to map item requirements',
CheckoutErrorType.ITEM_REQUIREMENTS_ERROR,
{ error: err },
);
}

return await smartCheckout.smartCheckout(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ describe('signActions', () => {

expect(message).toEqual('An error occurred while executing the approval transaction');
expect(type).toEqual(CheckoutErrorType.EXECUTE_APPROVAL_TRANSACTION_ERROR);
expect(data).toEqual({
message: 'approval error',
});
expect(data.error).toBeDefined();
});
});

Expand Down Expand Up @@ -207,9 +205,7 @@ describe('signActions', () => {

expect(message).toEqual('An error occurred while executing the fulfillment transaction');
expect(type).toEqual(CheckoutErrorType.EXECUTE_FULFILLMENT_TRANSACTION_ERROR);
expect(data).toEqual({
message: 'fulfillment error',
});
expect(data.error).toBeDefined();
});
});

Expand Down Expand Up @@ -281,9 +277,7 @@ describe('signActions', () => {

expect(message).toEqual('An error occurred while signing the message');
expect(type).toEqual(CheckoutErrorType.SIGN_MESSAGE_ERROR);
expect(data).toEqual({
message: 'sign message error',
});
expect(data.error).toBeDefined();
});
});
});
12 changes: 3 additions & 9 deletions packages/checkout/sdk/src/smartCheckout/actions/signActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export const signApprovalTransactions = async (
throw new CheckoutError(
'An error occurred while executing the approval transaction',
CheckoutErrorType.EXECUTE_APPROVAL_TRANSACTION_ERROR,
{
message: err.message,
},
{ error: err },
);
}

Expand Down Expand Up @@ -60,9 +58,7 @@ export const signFulfillmentTransactions = async (
throw new CheckoutError(
'An error occurred while executing the fulfillment transaction',
CheckoutErrorType.EXECUTE_FULFILLMENT_TRANSACTION_ERROR,
{
message: err.message,
},
{ error: err },
);
}

Expand Down Expand Up @@ -101,9 +97,7 @@ export const signMessage = async (
throw new CheckoutError(
'An error occurred while signing the message',
CheckoutErrorType.SIGN_MESSAGE_ERROR,
{
message: err.message,
},
{ error: err },
);
}
};
21 changes: 9 additions & 12 deletions packages/checkout/sdk/src/smartCheckout/allowance/erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ describe('allowance', () => {
allowance: allowanceMock,
});

let message = '';
let type = '';
let data = {};
let message;
let type;
let data;

try {
await getERC20Allowance(
Expand All @@ -55,9 +55,7 @@ describe('allowance', () => {

expect(message).toEqual('Failed to get the allowance for ERC20');
expect(type).toEqual(CheckoutErrorType.GET_ERC20_ALLOWANCE_ERROR);
expect(data).toEqual({
contractAddress: 'OxERC20',
});
expect(data.error).toBeDefined();
expect(allowanceMock).toBeCalledWith('0xADDRESS', '0xSEAPORT');
});
});
Expand Down Expand Up @@ -90,9 +88,9 @@ describe('allowance', () => {
},
});

let message = '';
let type = '';
let data = {};
let message;
let type;
let data;

try {
await getERC20ApprovalTransaction(
Expand All @@ -110,9 +108,8 @@ describe('allowance', () => {

expect(message).toEqual('Failed to get the approval transaction for ERC20');
expect(type).toEqual(CheckoutErrorType.GET_ERC20_ALLOWANCE_ERROR);
expect(data).toEqual({
contractAddress: 'OxERC20',
});
expect(data.error).toBeDefined();
expect(data.contractAddress).toEqual('OxERC20');
expect(approveMock).toBeCalledWith('0xSEAPORT', BigNumber.from(1));
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/checkout/sdk/src/smartCheckout/allowance/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getERC20Allowance = async (
throw new CheckoutError(
'Failed to get the allowance for ERC20',
CheckoutErrorType.GET_ERC20_ALLOWANCE_ERROR,
{ contractAddress },
{ contractAddress, error: err },
);
}
};
Expand All @@ -46,11 +46,11 @@ export const getERC20ApprovalTransaction = async (
const approveTransaction = await contract.populateTransaction.approve(spenderAddress, amount);
if (approveTransaction) approveTransaction.from = ownerAddress;
return approveTransaction;
} catch {
} catch (err: any) {
throw new CheckoutError(
'Failed to get the approval transaction for ERC20',
CheckoutErrorType.GET_ERC20_ALLOWANCE_ERROR,
{ contractAddress },
{ contractAddress, error: err },
);
}
};
Expand Down
46 changes: 21 additions & 25 deletions packages/checkout/sdk/src/smartCheckout/allowance/erc721.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ describe('erc721', () => {
getApproved: getApprovedMock,
});

let message = '';
let type = '';
let data = {};
let message;
let type;
let data;

try {
await getERC721ApprovedAddress(
Expand All @@ -59,10 +59,9 @@ describe('erc721', () => {

expect(message).toEqual('Failed to get approved address for ERC721');
expect(type).toEqual(CheckoutErrorType.GET_ERC721_ALLOWANCE_ERROR);
expect(data).toEqual({
id: '0',
contractAddress: '0xERC721',
});
expect(data.error).toBeDefined();
expect(data.id).toEqual('0');
expect(data.contractAddress).toEqual('0xERC721');
expect(getApprovedMock).toBeCalledWith(0);
});
});
Expand Down Expand Up @@ -95,9 +94,9 @@ describe('erc721', () => {
},
});

let message = '';
let type = '';
let data = {};
let message;
let type;
let data;

try {
await getApproveTransaction(
Expand All @@ -115,12 +114,11 @@ describe('erc721', () => {

expect(message).toEqual('Failed to get the approval transaction for ERC721');
expect(type).toEqual(CheckoutErrorType.GET_ERC721_ALLOWANCE_ERROR);
expect(data).toEqual({
id: '0',
contractAddress: '0xERC721',
spenderAddress: '0xSEAPORT',
ownerAddress: '0xADDRESS',
});
expect(data.error).toBeDefined();
expect(data.id).toEqual('0');
expect(data.contractAddress).toEqual('0xERC721');
expect(data.spenderAddress).toEqual('0xSEAPORT');
expect(data.ownerAddress).toEqual('0xADDRESS');
expect(approveMock).toBeCalledWith('0xSEAPORT', 0);
});
});
Expand Down Expand Up @@ -148,10 +146,9 @@ describe('erc721', () => {
isApprovedForAll: isApprovedForAllMock,
});

let message = '';
let type = '';
let data = {};

let message;
let type;
let data;
try {
await getERC721ApprovedForAll(
mockProvider,
Expand All @@ -167,11 +164,10 @@ describe('erc721', () => {

expect(message).toEqual('Failed to check approval for all ERC721s of collection');
expect(type).toEqual(CheckoutErrorType.GET_ERC721_ALLOWANCE_ERROR);
expect(data).toEqual({
ownerAddress: '0xADDRESS',
contractAddress: '0xERC721',
spenderAddress: '0xSEAPORT',
});
expect(data.error).toBeDefined();
expect(data.ownerAddress).toEqual('0xADDRESS');
expect(data.contractAddress).toEqual('0xERC721');
expect(data.spenderAddress).toEqual('0xSEAPORT');
expect(isApprovedForAllMock).toBeCalledWith('0xADDRESS', '0xSEAPORT');
});
});
Expand Down
Loading

0 comments on commit 1c3ed35

Please sign in to comment.