From 97314bc51e97058b493807db620062141ca76e58 Mon Sep 17 00:00:00 2001 From: igalklebanov Date: Sun, 14 Jul 2024 03:12:44 +0300 Subject: [PATCH] fix seed run error handling. --- src/commands/seed/run.mts | 7 +++++++ src/kysely/process-migration-result-set.mts | 11 +++-------- src/utils/error.mts | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 src/utils/error.mts diff --git a/src/commands/seed/run.mts b/src/commands/seed/run.mts index 1a6ecf1..d2a7fd2 100644 --- a/src/commands/seed/run.mts +++ b/src/commands/seed/run.mts @@ -1,9 +1,11 @@ import type { ArgsDef, CommandDef } from 'citty' import { consola } from 'consola' import { colorize } from 'consola/utils' +import { process } from 'std-env' import { CommonArgs } from '../../arguments/common.mjs' import { usingSeeder } from '../../seeds/using-seeder.mjs' import { createSubcommand } from '../../utils/create-subcommand.mjs' +import { exitWithError, handleAggregateError } from '../../utils/error.mjs' const args = { ...CommonArgs, @@ -62,6 +64,11 @@ const BaseRunCommand = { }`, ) } + + if (error) { + handleAggregateError(error) + exitWithError(error) + } }, } satisfies CommandDef diff --git a/src/kysely/process-migration-result-set.mts b/src/kysely/process-migration-result-set.mts index d614f49..8164b86 100644 --- a/src/kysely/process-migration-result-set.mts +++ b/src/kysely/process-migration-result-set.mts @@ -2,6 +2,7 @@ import { consola } from 'consola' import { colorize } from 'consola/utils' import type { MigrationResultSet, Migrator } from 'kysely' import { process } from 'std-env' +import { exitWithError, handleAggregateError } from '../utils/error.mjs' import { getMigrations } from './get-migrations.mjs' export async function processMigrationResultSet( @@ -22,14 +23,8 @@ export async function processMigrationResultSet( }`, ) - if (error instanceof AggregateError) { - for (const subError of error.errors) { - consola.error(subError) - } - } - - process.exit?.(1) - throw error + handleAggregateError(error) + exitWithError(error) } if (!results?.length) { diff --git a/src/utils/error.mts b/src/utils/error.mts new file mode 100644 index 0000000..23c226e --- /dev/null +++ b/src/utils/error.mts @@ -0,0 +1,15 @@ +import { consola } from 'consola' +import { process } from 'std-env' + +export function handleAggregateError(error: unknown): void { + if (error instanceof AggregateError) { + for (const subError of error.errors) { + consola.error(subError) + } + } +} + +export function exitWithError(error: unknown): never { + process.exit?.(1) + throw error +}