feat(plugin-tools): improve error serialization and let each storage plugin serialize errors themselves

This commit is contained in:
Joakim Carlstein 2023-12-12 15:32:58 +01:00
parent 09181f284d
commit a79f8e8e37
6 changed files with 29 additions and 10 deletions

View file

@ -11,12 +11,16 @@ import {
} from './types.js';
export const serializeError = (error: Error): SerializedError => {
return {
const properties: Record<string, unknown> = {
name: error.name,
message: error.message,
stack: error.stack,
cause: error.cause instanceof Error ? serializeError(error.cause) : error.cause,
};
for (const key of Object.getOwnPropertyNames(error)) {
const value = error[key as keyof Error];
properties[key] = value instanceof Error ? serializeError(value) : value;
}
return properties as SerializedError;
};
export const isGeneratorPlugin = (plugin: any): plugin is GeneratorPlugin => {

View file

@ -5,7 +5,8 @@ export type StringOrModule<T> = string | T | (() => Awaitable<T>) | (() => Await
export type MigrationStatus = 'failed' | 'done' | 'pending';
export type SerializedError = {
name: string;
[key: string]: unknown;
name?: string;
message: string;
stack?: string;
cause?: unknown;
@ -73,7 +74,7 @@ export type Storage = {
* @param migration The name of the migration that should be marked as failed.
* @param error The error that caused the migration to fail.
*/
onError(migration: MigrationMetadataFinished, error: SerializedError): Promise<void>;
onError(migration: MigrationMetadataFinished, error: Error): Promise<void>;
/**
* Called when the command is finished or aborted (e.g. by a SIGTERM or SIGINT signal).
*