Skip to content

Commit

Permalink
flatten zod errors (#789)
Browse files Browse the repository at this point in the history
* flatten zod errors

* fix error
  • Loading branch information
qiweiii authored Jul 10, 2024
1 parent 74a8042 commit 524a935
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/chopsticks/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 new Error('Bad argv', { cause: error.flatten().fieldErrors })
} else {
throw error
}
}
}

Expand Down

0 comments on commit 524a935

Please sign in to comment.