UNPKG

@lylajs/core

Version:

116 lines (115 loc) 3.88 kB
export var LYLA_ERROR; (function (LYLA_ERROR) { /** * Request encountered an error, fired by XHR `onerror` event. It doesn't mean * your network has error, for example CORS error also triggers NETWORK_ERROR. */ LYLA_ERROR["NETWORK"] = "NETWORK"; /** * Request is aborted. */ LYLA_ERROR["ABORTED"] = "ABORTED"; /** * Response text is not valid JSON. */ LYLA_ERROR["INVALID_JSON"] = "INVALID_JSON"; /** * Trying resolving `response.json` with `responseType='arraybuffer'` or * `responseType='blob'`. */ LYLA_ERROR["INVALID_CONVERSION"] = "INVALID_CONVERSION"; /** * Request timeout. */ LYLA_ERROR["TIMEOUT"] = "TIMEOUT"; /** * HTTP status error. */ LYLA_ERROR["HTTP"] = "HTTP"; /** * Request `options` is not valid. It's not a response error. */ LYLA_ERROR["BAD_REQUEST"] = "BAD_REQUEST"; /** * `onAfterResponse` hook throws error. */ LYLA_ERROR["BROKEN_ON_AFTER_RESPONSE"] = "BROKEN_ON_AFTER_RESPONSE"; /** * `onBeforeRequest` hook throws error. */ LYLA_ERROR["BROKEN_ON_BEFORE_REQUEST"] = "BROKEN_ON_BEFORE_REQUEST"; /** * `onInit` hook throws error. */ LYLA_ERROR["BROKEN_ON_INIT"] = "BROKEN_ON_INIT"; /** * `onResponseError` hook throws error. */ LYLA_ERROR["BROKEN_ON_RESPONSE_ERROR"] = "BROKEN_ON_RESPONSE_ERROR"; /** * `onNonResponseError` hook throws error. */ LYLA_ERROR["BROKEN_ON_NON_RESPONSE_ERROR"] = "BROKEN_ON_NON_RESPONSE_ERROR"; /** * `onHeadersReceived` hook throws error. */ LYLA_ERROR["BROKEN_ON_HEADERS_RECEIVED"] = "BROKEN_ON_HEADERS_RECEIVED"; /** * Lyla instance created with `withRetry` throws an unexpected error. This * error isn't created by `lyla` instance itself, but thrown by `onRejected` * or `onResolved` of `withRetry` or the process of creating retry request options * defined by user. * * The error won't be created by `lyla` instance that not created with `withRetry`. */ LYLA_ERROR["BROKEN_RETRY"] = "BROKEN_RETRY"; /** * A non-lyla error is return by `onRejected` or `onResolved`'s `reject` action. * Lyla error won't be wrapped in this error. * * The error won't be created by `lyla` instance that not created with `withRetry`. */ LYLA_ERROR["RETRY_REJECTED_BY_NON_LYLA_ERROR"] = "RETRY_REJECTED_BY_NON_LYLA_ERROR"; })(LYLA_ERROR || (LYLA_ERROR = {})); function createLylaError() { const lylaError = new Error(); lylaError.__lylaError = true; return lylaError; } export function defineLylaError(lylaErrorProps, stack) { const lylaError = createLylaError(); lylaError.name = `LylaError[${lylaErrorProps.type}]`; if (stack) { lylaError.stack += stack; } Object.assign(lylaError, lylaErrorProps); const spread = () => { const currentLylaError = lylaError; return { // Error props name: currentLylaError.name, message: currentLylaError.message, stack: currentLylaError.stack, // Other props type: currentLylaError.type, error: currentLylaError.error, detail: currentLylaError.detail, context: currentLylaError.context, response: currentLylaError.response, requestOptions: currentLylaError.requestOptions, // special prop __lylaError: true }; }; lylaError.spread = spread; return lylaError; } export function isLylaError(error) { return (typeof error === 'object' && !!error && '__lylaError' in error && !('isRetryError' in error)); } export function isLylaErrorWithRetry(error) { return typeof error === 'object' && !!error && '__lylaError' in error; }