From fcd5df6b702706bc42d9101ea6535e4602aa42d4 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 10 Jul 2024 12:07:02 +0800 Subject: [PATCH 1/2] flatten zod errors --- packages/chopsticks/src/cli.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/chopsticks/src/cli.ts b/packages/chopsticks/src/cli.ts index c26f01ba..3c037e94 100644 --- a/packages/chopsticks/src/cli.ts +++ b/packages/chopsticks/src/cli.ts @@ -1,5 +1,6 @@ import { config as dotenvConfig } from 'dotenv' import { hideBin } from 'yargs/helpers' +import { z } from 'zod' import _ from 'lodash' import yargs from 'yargs' import type { MiddlewareFunction } from 'yargs' @@ -12,14 +13,22 @@ import { setupWithServer } from './index.js' dotenvConfig() const processArgv: MiddlewareFunction<{ config?: string; port?: number; unsafeRpcMethods?: string }> = async (argv) => { - if (argv.unsafeRpcMethods) { - await loadRpcMethodsByScripts(argv.unsafeRpcMethods) - } - if (argv.config) { - Object.assign(argv, _.defaults(argv, await fetchConfig(argv.config))) - } - if (environment.PORT) { - argv.port = Number(environment.PORT) + try { + if (argv.unsafeRpcMethods) { + await loadRpcMethodsByScripts(argv.unsafeRpcMethods) + } + if (argv.config) { + Object.assign(argv, _.defaults(argv, await fetchConfig(argv.config))) + } + if (environment.PORT) { + argv.port = Number(environment.PORT) + } + } catch (error) { + if (error instanceof z.ZodError) { + throw error.flatten() + } else { + throw error + } } } From 028bbab035630b3593b995ecce9b71d7d7db72b6 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 10 Jul 2024 12:11:19 +0800 Subject: [PATCH 2/2] fix error --- packages/chopsticks/src/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/chopsticks/src/cli.ts b/packages/chopsticks/src/cli.ts index 3c037e94..0c470b7c 100644 --- a/packages/chopsticks/src/cli.ts +++ b/packages/chopsticks/src/cli.ts @@ -25,7 +25,7 @@ const processArgv: MiddlewareFunction<{ config?: string; port?: number; unsafeRp } } catch (error) { if (error instanceof z.ZodError) { - throw error.flatten() + throw new Error('Bad argv', { cause: error.flatten().fieldErrors }) } else { throw error }