Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 15, 2025
1 parent 7b94232 commit a310810
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 141 deletions.
11 changes: 8 additions & 3 deletions packages/core/util/serialize-error/error-constructors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Let `serialize-error` know about your custom error constructors so that when `{n
Warning: The constructor must work without any arguments or this function will throw.
*/

type BaseErrorConstructor = new (message?: string, ...arguments_: unknown[]) => Error;
declare function addKnownErrorConstructor(constructor: BaseErrorConstructor): void;
type BaseErrorConstructor = new (
message?: string,
...arguments_: unknown[]
) => Error
declare function addKnownErrorConstructor(
constructor: BaseErrorConstructor,
): void

export {addKnownErrorConstructor};
export { addKnownErrorConstructor }
18 changes: 9 additions & 9 deletions packages/core/util/serialize-error/error-constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ const list = [
]
// Non-native Errors are used with `globalThis` because they might be missing. This filter drops them when undefined.
.filter(Boolean)
.map(
constructor => [constructor.name, constructor],
);
.map(constructor => [constructor.name, constructor])

export const errorConstructors = new Map(list);
export const errorConstructors = new Map(list)

export function addKnownErrorConstructor(constructor) {
const {name} = constructor;
const { name } = constructor
if (errorConstructors.has(name)) {
throw new Error(`The error constructor "${name}" is already known.`);
throw new Error(`The error constructor "${name}" is already known.`)
}

try {
// eslint-disable-next-line no-new -- It just needs to be verified
new constructor();
new constructor()
} catch (error) {
throw new Error(`The error constructor "${name}" is not compatible`, {cause: error});
throw new Error(`The error constructor "${name}" is not compatible`, {
cause: error,
})
}

errorConstructors.set(name, constructor);
errorConstructors.set(name, constructor)
}
50 changes: 28 additions & 22 deletions packages/core/util/serialize-error/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {type Primitive, type JsonObject} from 'type-fest';
import { type Primitive, type JsonObject } from 'type-fest'

export {addKnownErrorConstructor} from './error-constructors.js';
export { addKnownErrorConstructor } from './error-constructors.js'

export type ErrorObject = {
name?: string;
message?: string;
stack?: string;
cause?: unknown;
code?: string;
} & JsonObject;
name?: string
message?: string
stack?: string
cause?: unknown
code?: string
} & JsonObject

export type ErrorLike = {
[key: string]: unknown;
name: string;
message: string;
stack: string;
cause?: unknown;
code?: string;
};
[key: string]: unknown
name: string
message: string
stack: string
cause?: unknown
code?: string
}

export type Options = {
/**
Expand All @@ -39,15 +39,15 @@ export type Options = {
//=> {name: 'Error', message: '…', one: { two: {}}}
```
*/
readonly maxDepth?: number;
readonly maxDepth?: number

/**
Indicate whether to use a `.toJSON()` method if encountered in the object. This is useful when a custom error implements its own serialization logic via `.toJSON()` but you prefer to not use it.
@default true
*/
readonly useToJSON?: boolean;
};
readonly useToJSON?: boolean
}

/**
Serialize an `Error` object into a plain object.
Expand Down Expand Up @@ -105,11 +105,14 @@ serializeError(error);
// => {horn: 'x', name, message, stack}
```
*/
export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
export function serializeError<ErrorType>(
error: ErrorType,
options?: Options,
): ErrorType extends Primitive
? ErrorType
: unknown extends ErrorType
? unknown
: ErrorObject;
: ErrorObject

/**
Deserialize a plain object or any value into an `Error` object.
Expand Down Expand Up @@ -137,7 +140,10 @@ console.log(error);
// at <anonymous>:1:13
```
*/
export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error;
export function deserializeError(
errorObject: ErrorObject | unknown,
options?: Options,
): Error

/**
Predicate to determine whether a value looks like an error, even if it's not an instance of `Error`. It must have at least the `name`, `message`, and `stack` properties.
Expand Down Expand Up @@ -170,4 +176,4 @@ isErrorLike({
//=> false
```
*/
export function isErrorLike(value: unknown): value is ErrorLike;
export function isErrorLike(value: unknown): value is ErrorLike
Loading

0 comments on commit a310810

Please sign in to comment.