@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
113 lines (112 loc) • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const constants_1 = require("./constants");
const httpError_1 = require("./httpError");
const url = 'http://some.where';
const options = { method: 'POST' };
const responseBody = { message: 'Oops' };
(0, vitest_1.describe)('HTTPError', () => {
vitest_1.it.each([
[
'when status code is 400',
options,
400,
'Bad Request',
{
initialMessage: 'Request failed with status code 400 Bad Request',
type: constants_1.ErrorName.ValidationError,
code: constants_1.LiFiErrorCode.ValidationError,
jsonFunc: () => Promise.resolve(responseBody),
responseBody,
builtMessage: '[ValidationError] Request failed with status code 400 Bad Request. Oops',
},
],
[
'when status code is 404',
options,
404,
'Not Found',
{
initialMessage: 'Request failed with status code 404 Not Found',
type: constants_1.ErrorName.NotFoundError,
code: constants_1.LiFiErrorCode.NotFound,
jsonFunc: () => Promise.resolve(responseBody),
responseBody,
builtMessage: '[NotFoundError] Request failed with status code 404 Not Found. Oops',
},
],
[
'when status code is 409',
options,
409,
'Conflict',
{
initialMessage: 'Request failed with status code 409 Conflict\nThe slippage is larger than the defined threshold. Please request a new route to get a fresh quote.',
type: constants_1.ErrorName.SlippageError,
code: constants_1.LiFiErrorCode.SlippageError,
jsonFunc: () => Promise.resolve(responseBody),
responseBody,
builtMessage: '[SlippageError] Request failed with status code 409 Conflict\nThe slippage is larger than the defined threshold. Please request a new route to get a fresh quote. Oops',
},
],
[
'when status code is 500',
options,
500,
'Internal Server Error',
{
initialMessage: 'Request failed with status code 500 Internal Server Error',
type: constants_1.ErrorName.ServerError,
code: constants_1.LiFiErrorCode.InternalError,
jsonFunc: () => Promise.resolve(responseBody),
responseBody,
builtMessage: '[ServerError] Request failed with status code 500 Internal Server Error. Oops',
},
],
[
'when status code is undefined',
options,
undefined,
'',
{
initialMessage: 'Request failed with an unknown error',
type: constants_1.ErrorName.ServerError,
code: constants_1.LiFiErrorCode.InternalError,
jsonFunc: () => Promise.resolve(responseBody),
responseBody,
builtMessage: '[ServerError] Request failed with an unknown error. Oops',
},
],
[
'when there is a problem processing the body',
options,
400,
'Bad Request',
{
initialMessage: 'Request failed with status code 400 Bad Request',
type: constants_1.ErrorName.ValidationError,
code: constants_1.LiFiErrorCode.ValidationError,
jsonFunc: () => Promise.reject(new Error('fail')),
responseBody: undefined,
builtMessage: '[ValidationError] Request failed with status code 400 Bad Request',
},
],
])('should present correctly %s', async (_, requestOptions, statusCode, statusText, expected) => {
const mockResponse = {
status: statusCode,
statusText,
json: expected.jsonFunc,
};
const error = new httpError_1.HTTPError(mockResponse, url, requestOptions);
(0, vitest_1.expect)(error.status).toEqual(statusCode);
(0, vitest_1.expect)(error.message).toEqual(expected.initialMessage);
(0, vitest_1.expect)(error.url).toEqual(url);
(0, vitest_1.expect)(error.fetchOptions).toEqual(requestOptions);
(0, vitest_1.expect)(error.type).toEqual(expected.type);
(0, vitest_1.expect)(error.code).toEqual(expected.code);
await error.buildAdditionalDetails();
(0, vitest_1.expect)(error.responseBody).toEqual(expected.responseBody);
(0, vitest_1.expect)(error.message).toEqual(expected.builtMessage);
});
});