diff --git a/packages/date-time/src/constants.ts b/packages/date-time/src/constants.ts index 2b741882..8493dd1d 100644 --- a/packages/date-time/src/constants.ts +++ b/packages/date-time/src/constants.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + export const RFC_3339_DATETIME_REGEX = /^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60))(\.\d+)?((Z)|([+|-]([01]\d|2[0-3]):[0-5]\d))$/; diff --git a/packages/date-time/src/errors.ts b/packages/date-time/src/errors.ts index c685ddf8..2d785a54 100644 --- a/packages/date-time/src/errors.ts +++ b/packages/date-time/src/errors.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { ErrorCode } from "@storm-stack/errors"; export type DateTimeErrorCode = diff --git a/packages/date-time/src/index.ts b/packages/date-time/src/index.ts index 3bb41766..d83b9eb4 100644 --- a/packages/date-time/src/index.ts +++ b/packages/date-time/src/index.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + export * from "./constants"; export * from "./errors"; export * from "./storm-date"; diff --git a/packages/date-time/src/storm-date-time.ts b/packages/date-time/src/storm-date-time.ts index 155f85c1..89fbc765 100644 --- a/packages/date-time/src/storm-date-time.ts +++ b/packages/date-time/src/storm-date-time.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { Temporal } from "@js-temporal/polyfill"; import { type JsonValue, Serializable } from "@storm-stack/serialization"; import { @@ -305,7 +322,7 @@ export class StormDateTime extends Date { return value.isValid; } if (isInstant(value)) { - return !!value.epochMilliseconds; + return Boolean(value.epochMilliseconds); } let datetime: string | undefined; @@ -644,7 +661,12 @@ export class StormDateTime extends Date { ): number { this.#instant = this.#instant .toZonedDateTimeISO("UTC") - .with({ hour, minute, second, millisecond }) + .with({ + hour, + minute, + second, + millisecond + }) .toInstant(); this.#zonedDateTime = this.#instant.toZonedDateTime({ timeZone: this.timeZoneId, diff --git a/packages/date-time/src/storm-date.ts b/packages/date-time/src/storm-date.ts index d529440b..fcc2b0f3 100644 --- a/packages/date-time/src/storm-date.ts +++ b/packages/date-time/src/storm-date.ts @@ -1,4 +1,20 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { Temporal } from "@js-temporal/polyfill"; import { type JsonValue, Serializable } from "@storm-stack/serialization"; import { isBigInt, isDate, isNumber, isSetString } from "@storm-stack/types"; @@ -132,7 +148,7 @@ export class StormDate extends StormDateTime { return value.isValid; } if (isInstant(value)) { - return !!value.epochMilliseconds; + return Boolean(value.epochMilliseconds); } let datetime: string | undefined; diff --git a/packages/date-time/src/storm-time.ts b/packages/date-time/src/storm-time.ts index b938ed94..b9413368 100644 --- a/packages/date-time/src/storm-time.ts +++ b/packages/date-time/src/storm-time.ts @@ -1,11 +1,27 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { Temporal } from "@js-temporal/polyfill"; import { type JsonValue, Serializable } from "@storm-stack/serialization"; import { isBigInt, isDate, isNumber, isSetString } from "@storm-stack/types"; import { RFC_3339_TIME_REGEX } from "./constants"; import type { DateTimeInput, DateTimeOptions } from "./storm-date-time"; import { StormDateTime } from "./storm-date-time"; -import { isInstant } from "./utilities"; +import { isInstant } from "./utilities/is-instant"; /** * Serializes a StormTime into a string @@ -108,7 +124,7 @@ export class StormTime extends StormDateTime { return value.isValid; } if (isInstant(value)) { - return !!value.epochMilliseconds; + return Boolean(value.epochMilliseconds); } let datetime: string | undefined; diff --git a/packages/date-time/src/utilities/format-date-time-iso.ts b/packages/date-time/src/utilities/format-date-time-iso.ts index 38becc4d..b375635f 100644 --- a/packages/date-time/src/utilities/format-date-time-iso.ts +++ b/packages/date-time/src/utilities/format-date-time-iso.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import type { Temporal } from "@js-temporal/polyfill"; import { EMPTY_STRING } from "@storm-stack/types"; import type { StormDateTime } from "../storm-date-time"; diff --git a/packages/date-time/src/utilities/format-date-time.ts b/packages/date-time/src/utilities/format-date-time.ts index 048bd04f..aa0d7953 100644 --- a/packages/date-time/src/utilities/format-date-time.ts +++ b/packages/date-time/src/utilities/format-date-time.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import type { Temporal } from "@js-temporal/polyfill"; import { EMPTY_STRING } from "@storm-stack/types"; import { StormDateTime } from "../storm-date-time"; @@ -21,13 +38,13 @@ export const formatDateTime = ( return dateTime ? `${dateTime.zonedDateTime - .toString({ - smallestUnit, - roundingMode, - calendarName, - timeZoneName, - offset - }) - .replaceAll("T", " ")}` + .toString({ + smallestUnit, + roundingMode, + calendarName, + timeZoneName, + offset + }) + .replaceAll("T", " ")}` : EMPTY_STRING; }; diff --git a/packages/date-time/src/utilities/format-date.ts b/packages/date-time/src/utilities/format-date.ts index 3300df96..4078ed30 100644 --- a/packages/date-time/src/utilities/format-date.ts +++ b/packages/date-time/src/utilities/format-date.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import type { Temporal } from "@js-temporal/polyfill"; import { StormDate } from "../storm-date"; import type { StormDateTime } from "../storm-date-time"; diff --git a/packages/date-time/src/utilities/format-since.ts b/packages/date-time/src/utilities/format-since.ts index b419c4ce..461d6d62 100644 --- a/packages/date-time/src/utilities/format-since.ts +++ b/packages/date-time/src/utilities/format-since.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import type { Temporal } from "@js-temporal/polyfill"; import { StormError } from "@storm-stack/errors"; import { DateTimeErrorCode } from "../errors"; diff --git a/packages/date-time/src/utilities/format-time.ts b/packages/date-time/src/utilities/format-time.ts index cfa24aca..3baa773f 100644 --- a/packages/date-time/src/utilities/format-time.ts +++ b/packages/date-time/src/utilities/format-time.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import type { Temporal } from "@js-temporal/polyfill"; import type { StormDateTime } from "../storm-date-time"; import { StormTime } from "../storm-time"; diff --git a/packages/date-time/src/utilities/index.ts b/packages/date-time/src/utilities/index.ts index e0135a9b..38d0bcd4 100644 --- a/packages/date-time/src/utilities/index.ts +++ b/packages/date-time/src/utilities/index.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + export * from "./format-date"; export * from "./format-date-time"; export * from "./format-date-time-iso"; diff --git a/packages/date-time/src/utilities/is-date-time.ts b/packages/date-time/src/utilities/is-date-time.ts index 35904ae8..765799b0 100644 --- a/packages/date-time/src/utilities/is-date-time.ts +++ b/packages/date-time/src/utilities/is-date-time.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { StormDateTime } from "../storm-date-time"; /** diff --git a/packages/date-time/src/utilities/is-instant.ts b/packages/date-time/src/utilities/is-instant.ts index f9ec040f..1f5f4ddb 100644 --- a/packages/date-time/src/utilities/is-instant.ts +++ b/packages/date-time/src/utilities/is-instant.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { Temporal } from "@js-temporal/polyfill"; import { isFunction, isSet } from "@storm-stack/types"; diff --git a/packages/date-time/src/utilities/validate-date-time.ts b/packages/date-time/src/utilities/validate-date-time.ts index 42a54926..7e886dde 100644 --- a/packages/date-time/src/utilities/validate-date-time.ts +++ b/packages/date-time/src/utilities/validate-date-time.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { isBigInt, isDate, isNumber } from "@storm-stack/types"; import { RFC_3339_DATETIME_REGEX } from "../constants"; import type { DateTimeInput, DateTimeOptions } from "../storm-date-time"; @@ -20,7 +37,7 @@ export function validateDateTime( return value.isValid; } if (isInstant(value)) { - return !!value.epochMilliseconds; + return Boolean(value.epochMilliseconds); } let datetime: string | undefined; @@ -54,14 +71,14 @@ export function validateDateTime( // Split the date-time-string up into the string-date and time-string part. // and check whether these parts are RFC 3339 compliant. - return !!( + return Boolean( validateDate( datetime.slice(0, Math.max(0, datetime.indexOf("T"))), options ) && - validateTime( - datetime.slice(Math.max(0, datetime.indexOf("T") + 1)), - options - ) + validateTime( + datetime.slice(Math.max(0, datetime.indexOf("T") + 1)), + options + ) ); } diff --git a/packages/date-time/src/utilities/validate-date.ts b/packages/date-time/src/utilities/validate-date.ts index ca7e5b1f..05b6b665 100644 --- a/packages/date-time/src/utilities/validate-date.ts +++ b/packages/date-time/src/utilities/validate-date.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { isBigInt, isDate, isNumber } from "@storm-stack/types"; import { RFC_3339_DATE_REGEX } from "../constants"; import { @@ -16,7 +33,7 @@ export function validateDate( return value.isValid; } if (isInstant(value)) { - return !!value.epochMilliseconds; + return Boolean(value.epochMilliseconds); } let datetime: string | undefined; diff --git a/packages/date-time/src/utilities/validate-time.ts b/packages/date-time/src/utilities/validate-time.ts index 9a12da9d..2f8df8d1 100644 --- a/packages/date-time/src/utilities/validate-time.ts +++ b/packages/date-time/src/utilities/validate-time.ts @@ -1,3 +1,20 @@ +/*------------------------------------------------------------------- + + ⚡ Storm Software - Storm Stack + + This code was released as part of the Storm Stack project. Storm Stack + is maintained by Storm Software under the Apache-2.0 License, and is + free for commercial and private use. For more information, please visit + our licensing page. + + Website: https://stormsoftware.com + Repository: https://github.com/storm-software/storm-stack + Documentation: https://stormsoftware.com/projects/storm-stack/docs + Contact: https://stormsoftware.com/contact + License: https://stormsoftware.com/projects/storm-stack/license + + -------------------------------------------------------------------*/ + import { isBigInt, isDate, isNumber } from "@storm-stack/types"; import { RFC_3339_TIME_REGEX } from "../constants"; import type { DateTimeInput, DateTimeOptions } from "../storm-date-time"; @@ -12,7 +29,7 @@ export function validateTime( return value.isValid; } if (isInstant(value)) { - return !!value.epochMilliseconds; + return Boolean(value.epochMilliseconds); } let datetime: string | undefined; diff --git a/packages/errors/src/storm-error.ts b/packages/errors/src/storm-error.ts index 48203b47..d73a592c 100644 --- a/packages/errors/src/storm-error.ts +++ b/packages/errors/src/storm-error.ts @@ -16,9 +16,16 @@ -------------------------------------------------------------------*/ import { Serializable } from "@storm-stack/serialization"; -import { EMPTY_STRING, NEWLINE_STRING, isFunction } from "@storm-stack/types"; -import { getCauseFromUnknown } from "./utilities/get-cause-from-unknown"; -import { isStormError } from "./utilities/is-storm-error"; +import { + EMPTY_STRING, + type Indexable, + NEWLINE_STRING, + isError, + isFunction, + isObject, + isSetString +} from "@storm-stack/types"; +import { ErrorCode } from "./errors"; export interface StormErrorOptions { name?: string; @@ -28,6 +35,111 @@ export interface StormErrorOptions { data?: any; } +/** + * Creates a new StormError instance + * + * @param cause - The cause of the error + * @returns The newly created StormError + */ +export function createStormError({ + code, + name, + message, + cause, + stack, + data +}: StormErrorOptions & { code?: TCode }): StormError { + if (isStormError(cause)) { + return cause; + } + + if (cause instanceof Error && cause.name === "StormError") { + return cause as StormError; + } + + const stormError = new StormError( + (code ?? ErrorCode.internal_server_error) as TCode, + { + name, + message, + cause, + stack, + data + } + ); + + // Inherit stack from error + if (cause instanceof Error && cause.stack) { + stormError.stack = cause.stack; + } + + return stormError; +} + +/** + * Gets the cause of an unknown error and returns it as a StormError + * + * @param cause - The cause of the error in an unknown type + * @returns The cause of the error in a StormError object or undefined + */ +export function getCauseFromUnknown(cause: unknown): StormError { + if (isStormError(cause)) { + return cause; + } + if (isError(cause)) { + return createStormError({ + code: ErrorCode.internal_server_error, + name: cause.name, + message: cause.message, + cause, + stack: cause.stack + }); + } + const type = typeof cause; + if (type === "undefined" || type === "function" || cause === null) { + return new StormError(ErrorCode.internal_server_error, { + cause + }); + } + + // Primitive types just get wrapped in an error + if (type !== "object") { + return new StormError(ErrorCode.internal_server_error, { + message: String(cause) + }); + } + + // If it's an object, we'll create a synthetic error + if (isObject(cause)) { + const err = new StormError(ErrorCode.unknown_cause, {}); + + for (const key of Object.keys(cause)) { + (err as Indexable)[key] = (cause as Indexable)[key]; + } + + return err; + } + + return new StormError(ErrorCode.internal_server_error, { cause }); +} + +/** + * Type-check to determine if `obj` is a `StormError` object + * + * @param value - the object to check + * @returns The function isStormError is returning a boolean value. + */ +export function isStormError( + value: unknown +): value is StormError { + return ( + isError(value) && + isSetString((value as unknown as StormError)?.code) && + isSetString((value as unknown as StormError)?.message) && + isSetString((value as unknown as StormError)?.stack) + ); +} + /** * A wrapper around the base JavaScript Error class to be used by Storm Software * diff --git a/packages/errors/src/utilities/create-storm-error.ts b/packages/errors/src/utilities/create-storm-error.ts deleted file mode 100644 index 4795e015..00000000 --- a/packages/errors/src/utilities/create-storm-error.ts +++ /dev/null @@ -1,61 +0,0 @@ -/*------------------------------------------------------------------- - - ⚡ Storm Software - Storm Stack - - This code was released as part of the Storm Stack project. Storm Stack - is maintained by Storm Software under the Apache-2.0 License, and is - free for commercial and private use. For more information, please visit - our licensing page. - - Website: https://stormsoftware.com - Repository: https://github.com/storm-software/storm-stack - Documentation: https://stormsoftware.com/projects/storm-stack/docs - Contact: https://stormsoftware.com/contact - License: https://stormsoftware.com/projects/storm-stack/license - - -------------------------------------------------------------------*/ - -import { ErrorCode } from "../errors"; -import { StormError, type StormErrorOptions } from "../storm-error"; -import { isStormError } from "./is-storm-error"; - -/** - * Creates a new StormError instance - * - * @param cause - The cause of the error - * @returns The newly created StormError - */ -export function createStormError({ - code, - name, - message, - cause, - stack, - data -}: StormErrorOptions & { code?: TCode }): StormError { - if (isStormError(cause)) { - return cause; - } - - if (cause instanceof Error && cause.name === "StormError") { - return cause as StormError; - } - - const stormError = new StormError( - (code ?? ErrorCode.internal_server_error) as TCode, - { - name, - message, - cause, - stack, - data - } - ); - - // Inherit stack from error - if (cause instanceof Error && cause.stack) { - stormError.stack = cause.stack; - } - - return stormError; -} diff --git a/packages/errors/src/utilities/get-cause-from-unknown.ts b/packages/errors/src/utilities/get-cause-from-unknown.ts deleted file mode 100644 index d9ef378e..00000000 --- a/packages/errors/src/utilities/get-cause-from-unknown.ts +++ /dev/null @@ -1,69 +0,0 @@ -/*------------------------------------------------------------------- - - ⚡ Storm Software - Storm Stack - - This code was released as part of the Storm Stack project. Storm Stack - is maintained by Storm Software under the Apache-2.0 License, and is - free for commercial and private use. For more information, please visit - our licensing page. - - Website: https://stormsoftware.com - Repository: https://github.com/storm-software/storm-stack - Documentation: https://stormsoftware.com/projects/storm-stack/docs - Contact: https://stormsoftware.com/contact - License: https://stormsoftware.com/projects/storm-stack/license - - -------------------------------------------------------------------*/ - -import { type Indexable, isError, isObject } from "@storm-stack/types"; -import { ErrorCode } from "../errors"; -import { StormError } from "../storm-error"; -import { createStormError } from "./create-storm-error"; -import { isStormError } from "./is-storm-error"; - -/** - * Gets the cause of an unknown error and returns it as a StormError - * - * @param cause - The cause of the error in an unknown type - * @returns The cause of the error in a StormError object or undefined - */ -export function getCauseFromUnknown(cause: unknown): StormError { - if (isStormError(cause)) { - return cause; - } - if (isError(cause)) { - return createStormError({ - code: ErrorCode.internal_server_error, - name: cause.name, - message: cause.message, - cause, - stack: cause.stack - }); - } - const type = typeof cause; - if (type === "undefined" || type === "function" || cause === null) { - return new StormError(ErrorCode.internal_server_error, { - cause - }); - } - - // Primitive types just get wrapped in an error - if (type !== "object") { - return new StormError(ErrorCode.internal_server_error, { - message: String(cause) - }); - } - - // If it's an object, we'll create a synthetic error - if (isObject(cause)) { - const err = new StormError(ErrorCode.unknown_cause, {}); - - for (const key of Object.keys(cause)) { - (err as Indexable)[key] = (cause as Indexable)[key]; - } - - return err; - } - - return new StormError(ErrorCode.internal_server_error, { cause }); -} diff --git a/packages/errors/src/utilities/index.ts b/packages/errors/src/utilities/index.ts index 68715971..0ebf06cd 100644 --- a/packages/errors/src/utilities/index.ts +++ b/packages/errors/src/utilities/index.ts @@ -15,7 +15,4 @@ -------------------------------------------------------------------*/ -export * from "./create-storm-error"; -export * from "./get-cause-from-unknown"; -export * from "./is-storm-error"; export * from "./serialization"; diff --git a/packages/errors/src/utilities/is-storm-error.ts b/packages/errors/src/utilities/is-storm-error.ts deleted file mode 100644 index 8d00e0cc..00000000 --- a/packages/errors/src/utilities/is-storm-error.ts +++ /dev/null @@ -1,36 +0,0 @@ -/*------------------------------------------------------------------- - - ⚡ Storm Software - Storm Stack - - This code was released as part of the Storm Stack project. Storm Stack - is maintained by Storm Software under the Apache-2.0 License, and is - free for commercial and private use. For more information, please visit - our licensing page. - - Website: https://stormsoftware.com - Repository: https://github.com/storm-software/storm-stack - Documentation: https://stormsoftware.com/projects/storm-stack/docs - Contact: https://stormsoftware.com/contact - License: https://stormsoftware.com/projects/storm-stack/license - - -------------------------------------------------------------------*/ - -import { isError, isSetString } from "@storm-stack/types"; -import type { StormError } from "../storm-error"; - -/** - * Type-check to determine if `obj` is a `StormError` object - * - * @param value - the object to check - * @returns The function isStormError is returning a boolean value. - */ -export function isStormError( - value: unknown -): value is StormError { - return ( - isError(value) && - isSetString((value as unknown as StormError)?.code) && - isSetString((value as unknown as StormError)?.message) && - isSetString((value as unknown as StormError)?.stack) - ); -}