import {isUndefined} from './other.ts'; import {EMPTY_STRING, TINYBASE} from './strings.ts'; export const ERROR_STORE_TYPE = 0; export const ERROR_CONTENT = 1; export const ERROR_CHART = 2; export const ERROR_SYNC_RESPONSE = 3; export const ERROR_MULTIPLEX_RESPONSE = 4; export const ERROR_MULTIPLEX_SOCKET = 5; export const ERROR_MULTIPLEX_CHANNEL = 6; export const ERROR_MULTIPLEX_CHANNEL_DUPLICATE = 7; export const ERROR_MULTIPLEX_DESTROYED = 8; export const ERROR_MULTIPLEX_LEGACY = 9; export const ERROR_LEGACY_MULTIPLEX = 10; export const ERROR_INDEXED_DB_STORE = 11; export const ERROR_INDEXED_DB_OPEN = 12; export const ERROR_HLC = 13; export const ERROR_SYNC_MESSAGE = 14; export const ERROR_SYNC_OVERFLOW = 15; export const ERROR_QUERY_SELECT_ALL_CYCLE = 16; export const ERROR_SYNC_UNAUTHORIZED = 17; export const ERROR_MERGEABLE = 18; export const errorNew = (code: number, details?: any): Error => new Error( TINYBASE + ':' + code + (isUndefined(details) ? EMPTY_STRING : /* istanbul ignore next */ ':' + details), ); export const errorThrow = (code: number, details?: any): never => { throw errorNew(code, details); }; export const tryReturn = ( tryF: () => Return, catchReturn?: Return, ): Return | undefined => { try { return tryF(); } catch { /*! istanbul ignore next */ return catchReturn; } }; export const tryCatch = async ( action: () => Return | Promise, onError?: (error: any) => void, ): Promise => { try { return await action(); } catch (error) { /*! istanbul ignore next */ onError?.(error); } }; export const tryCatchIgnore = ( action: () => Return | Promise, onIgnoredError?: (error: any) => void, ): Promise => tryCatch(action, (error) => void tryCatch(() => onIgnoredError?.(error))); export const tryCatchSync = ( action: () => Return, onError?: (error: any) => void, ): Return | void => { try { return action(); } catch (error) { onError?.(error); } }; export const tryFinally = ( action: () => Return, finallyAction: () => void, ): Return => { try { return action(); } finally { finallyAction(); } }; export const tryFinallyAsync = async ( action: () => Promise, finallyAction: () => void | Promise, ): Promise => { try { return await action(); } finally { await finallyAction(); } };