UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

137 lines (136 loc) 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._assert = _assert; exports._assertEquals = _assertEquals; exports._assertDeepEquals = _assertDeepEquals; exports._assertIsError = _assertIsError; exports._assertErrorClassOrRethrow = _assertErrorClassOrRethrow; exports._assertIsErrorObject = _assertIsErrorObject; exports._assertIsBackendErrorResponseObject = _assertIsBackendErrorResponseObject; exports._assertIsString = _assertIsString; exports._assertIsNumber = _assertIsNumber; exports._assertTypeOf = _assertTypeOf; exports.asUnixTimestamp = asUnixTimestamp; exports.asUnixTimestamp2000 = asUnixTimestamp2000; const deepEquals_1 = require("../object/deepEquals"); const stringify_1 = require("../string/stringify"); const zod_shared_schemas_1 = require("../zod/zod.shared.schemas"); const error_util_1 = require("./error.util"); /** * Evaluates the `condition` (casts it to Boolean). * Expects it to be truthy, otherwise throws AppError. * * Should be used NOT for "expected" / user-facing errors, but * vice-versa - for completely unexpected and 100% buggy "should never happen" cases. * * It'll result in http 500 on the server (cause that's the right code for "unexpected" errors). * Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib). * * API is similar to Node's assert(), except: * 1. Throws js-lib's AppError * 2. Has a default message, if not provided * * Since 2024-07-10 it no longer sets `userFriendly: true` by default. */ function _assert(condition, // will be evaluated as Boolean message, errorData) { if (!condition) { throw new error_util_1.AssertionError(message || 'condition failed', { ...errorData, }); } } /** * Like _assert(), but prints more helpful error message. * API is similar to Node's assert.equals(). * * Does SHALLOW, but strict equality (===), use _assertDeepEquals() for deep equality. */ function _assertEquals(actual, expected, message, errorData) { if (actual !== expected) { const msg = message || ['not equal', `expected: ${(0, stringify_1._stringify)(expected)}`, `got : ${(0, stringify_1._stringify)(actual)}`] .filter(Boolean) .join('\n'); throw new error_util_1.AssertionError(msg, { ...errorData, }); } } /** * Like _assert(), but prints more helpful error message. * API is similar to Node's assert.deepEquals(). * * Does DEEP equality via _deepEquals() */ function _assertDeepEquals(actual, expected, message, errorData) { if (!(0, deepEquals_1._deepEquals)(actual, expected)) { const msg = message || ['not deeply equal', `expected: ${(0, stringify_1._stringify)(expected)}`, `got : ${(0, stringify_1._stringify)(actual)}`] .filter(Boolean) .join('\n'); throw new error_util_1.AssertionError(msg, { ...errorData, }); } } function _assertIsError(err, errorClass = Error) { if (!(err instanceof errorClass)) { throw new error_util_1.AssertionError(`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}`); } } /** * Asserts that passed object is indeed an Error of defined ErrorClass. * If yes - returns peacefully (with TypeScript assertion). * In not - throws (re-throws) that error up. */ function _assertErrorClassOrRethrow(err, errorClass) { if (!(err instanceof errorClass)) { // re-throw throw err; } } function _assertIsErrorObject(obj) { if (!(0, error_util_1._isErrorObject)(obj)) { throw new error_util_1.AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}`); } } function _assertIsBackendErrorResponseObject(obj) { if (!(0, error_util_1._isBackendErrorResponseObject)(obj)) { throw new error_util_1.AssertionError(`Expected to be BackendErrorResponseObject, actual typeof: ${typeof obj}`); } } function _assertIsString(v, message) { _assertTypeOf(v, 'string', message); } function _assertIsNumber(v, message) { _assertTypeOf(v, 'number', message); } function _assertTypeOf(v, expectedType, message) { // biome-ignore lint/suspicious/useValidTypeof: ok if (typeof v !== expectedType) { const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`; throw new error_util_1.AssertionError(msg); } } /** * Casts an arbitrary number as UnixTimestamp. * Right now does not perform any validation (unlike `asUnixTimestamp2000`), * but only type casting. */ function asUnixTimestamp(n) { return n; } /** * Casts an arbitrary number as UnixTimestamp2000. * Throws if the number is not inside 2000-01-01 and 2500-01-01 time interval, * which would indicate a bug. */ function asUnixTimestamp2000(n) { if (!n || n < zod_shared_schemas_1.TS_2000 || n > zod_shared_schemas_1.TS_2500) { throw new error_util_1.AssertionError(`Number is not a valid UnixTimestamp2000: ${n}`, { fingerprint: 'asUnixTimestamp2000', }); } return n; }