UNPKG

@getclave/lifi-sdk

Version:

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

115 lines (114 loc) 5.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const version_1 = require("../version"); const SDKError_1 = require("./SDKError"); const baseError_1 = require("./baseError"); 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)('SDKError', () => { (0, vitest_1.describe)('when the cause is a http error', () => { (0, vitest_1.it)('should present the causing errors stack trace for http errors', async () => { vitest_1.expect.assertions(1); const testFunction = async () => { try { const mockResponse = { status: 400, statusText: 'Bad Request', json: () => Promise.resolve(responseBody), }; const httpError = new httpError_1.HTTPError(mockResponse, url, options); await httpError.buildAdditionalDetails(); throw httpError; } catch (e) { throw new SDKError_1.SDKError(e); } }; try { await testFunction(); } catch (e) { (0, vitest_1.expect)(e.stack).toBe(e.cause.stack); } }); (0, vitest_1.it)('should feature the causing http error message as part of its own message', async () => { const mockResponse = { status: 400, statusText: 'Bad Request', json: () => Promise.resolve(responseBody), }; const httpError = new httpError_1.HTTPError(mockResponse, url, options); await httpError.buildAdditionalDetails(); const testFunction = () => { throw new SDKError_1.SDKError(httpError); }; (0, vitest_1.expect)(() => testFunction()).toThrowError(`[HTTPError] [ValidationError] Request failed with status code 400 Bad Request. Oops\nLI.FI SDK version: ${version_1.version}`); }); }); (0, vitest_1.describe)('when the cause is a base error', () => { (0, vitest_1.it)('should present the causing errors stack trace for base errors', () => { vitest_1.expect.assertions(1); const testFunction = () => { try { const baseError = new baseError_1.BaseError(constants_1.ErrorName.ValidationError, constants_1.LiFiErrorCode.ValidationError, 'problem validating'); throw baseError; } catch (e) { throw new SDKError_1.SDKError(e); } }; try { testFunction(); } catch (e) { (0, vitest_1.expect)(e.stack).toBe(e.cause.stack); } }); (0, vitest_1.it)('should present the causing errors stack trace for base errors own causing error', () => { vitest_1.expect.assertions(1); const causingError = () => { try { throw new Error('this was the root cause'); } catch (e) { throw new baseError_1.BaseError(constants_1.ErrorName.ValidationError, constants_1.LiFiErrorCode.ValidationError, 'problem validating', e); } }; const testFunction = () => { try { causingError(); } catch (e) { throw new SDKError_1.SDKError(e); } }; try { testFunction(); } catch (e) { (0, vitest_1.expect)(e.stack).toBe(e.cause.cause.stack); } }); (0, vitest_1.it)('should feature the causing base error message as part of its own message', () => { const testFunction = () => { throw new SDKError_1.SDKError(new baseError_1.BaseError(constants_1.ErrorName.UnknownError, constants_1.LiFiErrorCode.InternalError, 'There was an error')); }; (0, vitest_1.expect)(() => testFunction()).toThrowError(`[UnknownError] There was an error\nLI.FI SDK version: ${version_1.version}`); }); (0, vitest_1.it)('should use a fail back error message if one is not defined on the base error', () => { const testFunction = () => { throw new SDKError_1.SDKError(new baseError_1.BaseError(constants_1.ErrorName.BalanceError, constants_1.LiFiErrorCode.BalanceError, '')); }; (0, vitest_1.expect)(() => testFunction()).toThrowError(`Unknown error occurred\nLI.FI SDK version: ${version_1.version}`); }); (0, vitest_1.it)('should present the passed base error as the cause', () => { const baseError = new baseError_1.BaseError(constants_1.ErrorName.UnknownError, constants_1.LiFiErrorCode.InternalError, 'There was an error'); const sdkError = new SDKError_1.SDKError(baseError); (0, vitest_1.expect)(sdkError.cause).toBe(baseError); }); }); });