UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

56 lines (55 loc) 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const SDKError_1 = require("../SDKError"); const baseError_1 = require("../baseError"); const constants_1 = require("../constants"); const httpError_1 = require("../httpError"); const baseErrorRootCause_1 = require("./baseErrorRootCause"); const getErrorChain = () => { const NonLiFiErrorChain = new Error('non lifi error'); NonLiFiErrorChain.cause = new Error('root cause'); return new SDKError_1.SDKError(new baseError_1.BaseError(constants_1.ErrorName.ValidationError, constants_1.LiFiErrorCode.ValidationError, 'something happened', NonLiFiErrorChain)); }; (0, vitest_1.describe)('getRootCauseBaseError', () => { (0, vitest_1.it)('should return the top level error when there is no root cause', () => { const error = new Error('top level'); (0, vitest_1.expect)((0, baseErrorRootCause_1.getRootCauseBaseError)(error).message).toEqual('top level'); }); (0, vitest_1.it)('should return the lowest BaseError in the cause chain', () => { const errorChain = getErrorChain(); (0, vitest_1.expect)((0, baseErrorRootCause_1.getRootCauseBaseError)(errorChain).message).toEqual('something happened'); }); }); (0, vitest_1.describe)('getRootCauseBaseErrorMessage', () => { (0, vitest_1.describe)('when root cause is HTTP Error', () => { (0, vitest_1.it)('should return the HTTP response message if present', async () => { const mockResponse = { status: 400, statusText: 'Bad Request', json: () => Promise.resolve({ message: 'something went wrong on the server' }), }; const httpError = new httpError_1.HTTPError(mockResponse, 'http://some.where', {}); await httpError.buildAdditionalDetails(); const errorChain = new SDKError_1.SDKError(httpError); (0, vitest_1.expect)((0, baseErrorRootCause_1.getRootCauseBaseErrorMessage)(errorChain)).toEqual('something went wrong on the server'); }); (0, vitest_1.it)('should return the HTTP error message if response message not present', async () => { const mockResponse = { status: 400, statusText: 'Bad Request', json: () => Promise.resolve({}), }; const httpError = new httpError_1.HTTPError(mockResponse, 'http://some.where', {}); await httpError.buildAdditionalDetails(); const errorChain = new SDKError_1.SDKError(httpError); (0, vitest_1.expect)((0, baseErrorRootCause_1.getRootCauseBaseErrorMessage)(errorChain)).toEqual('[ValidationError] Request failed with status code 400 Bad Request'); }); }); (0, vitest_1.describe)('when root cause is base Error', () => { (0, vitest_1.it)('should return the BaseError message', () => { const errorChain = getErrorChain(); (0, vitest_1.expect)((0, baseErrorRootCause_1.getRootCauseBaseErrorMessage)(errorChain)).toEqual('something happened'); }); }); });