Skip to content

Commit

Permalink
Improve Zod error response message (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h authored Sep 25, 2024
1 parent 27df478 commit dc5f80e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { logger } from './src/logger.js';
import { makeUsageQuery } from "./src/usage.js";
import { APIErrorResponse } from "./src/utils.js";
import { usageOperationsToEndpointsMap, type EndpointReturnTypes, type UsageEndpoints, type ValidPathParams, type ValidUserParams } from "./src/types/api.js";
import { paths } from './src/types/zod.gen.js';
import { paths, usageTransfersQueryParamsSchema } from './src/types/zod.gen.js';

async function AntelopeTokenAPI() {
const app = new Hono();
Expand Down Expand Up @@ -110,7 +110,12 @@ async function AntelopeTokenAPI() {
} as ValidUserParams<typeof endpoint>
);
} else {
return APIErrorResponse(ctx, 400, "bad_query_input", { ...path_params.error, ...query_params.error });
// Merge path and query params errors into one `ZodError` instance
const e = new z.ZodError([]);
e.addIssues(path_params.error?.issues);
e.addIssues(query_params.error?.issues);

return APIErrorResponse(ctx, 400, "bad_query_input", e);
}
}
);
Expand Down
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
],
"dependencies": {
"@clickhouse/client-web": "latest",
"@hono/graphql-server": "^0.5.0",
"@kubb/cli": "^2.23.3",
"@kubb/core": "^2.23.3",
"@kubb/plugin-oas": "^2.23.3",
"@kubb/swagger-zod": "^2.23.3",
"@hono/graphql-server": "^0.5.1",
"@kubb/cli": "^2.27.0",
"@kubb/core": "^2.27.0",
"@kubb/plugin-oas": "^2.27.0",
"@kubb/swagger-zod": "^2.27.0",
"commander": "latest",
"dotenv": "latest",
"hono": "latest",
Expand Down Expand Up @@ -55,5 +55,9 @@
},
"prettier": {
"tabWidth": 4
}
},
"trustedDependencies": [
"es5-ext",
"esbuild"
]
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function APIErrorResponse(ctx: Context, status: ApiErrorSchema["status"],
if (typeof err === "string") {
message = err;
} else if (err instanceof ZodError) {
message = err.issues.map(issue => `[${issue.code}] ${issue.path.join('/')}: ${issue.message}`).join('\n');
message = err.issues.map(issue => `[${issue.code}] ${issue.path.join('/')}: ${issue.message}`).join(' | ');
} else if (err instanceof Error) {
message = err.message;
}
Expand Down

0 comments on commit dc5f80e

Please sign in to comment.