Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mock API validation errors look more like real API errors #228

Closed
david-crespo opened this issue Jan 24, 2024 · 0 comments · Fixed by #259
Closed

Make mock API validation errors look more like real API errors #228

david-crespo opened this issue Jan 24, 2024 · 0 comments · Fixed by #259

Comments

@david-crespo
Copy link
Collaborator

david-crespo commented Jan 24, 2024

One thing we could do, it just occurred to me, is make them look more like real API errors so that the UI would handle them naturally. As in, where right now we just return the Zod error directly as JSON, we could stick it somewhere in an object that looks like { "error_code": "InvalidRequest", "message": "..." }. The Zod error would probably look like garbage stringified, but maybe we put a short version of it in message (with "Zod" in there to be clear it's a mock API error) and then log the full thing to the console.

https://github.com/oxidecomputer/console/pull/1854/files#r1425571596

Right now, when there's a validation failure in the mock API, we return the list of Zod "issues" more or less directly as the response body.

const { params, paramsErr } = paramSchema
? validateParams(paramSchema, req, pathParams)
: { params: {}, paramsErr: undefined };
if (paramsErr) return json(paramsErr, { status: 400 });

const { issues } = result.error;
const status = issues.some((e) => e.path[0] === "path") ? 404 : 400;
return { paramsErr: json(issues, { status }) };

This is a bit silly because it means that our code in the console to display messages from API 400s chokes and displays "Unknown server error" instead of a nicer message that we can assert about in the E2Es. As I said in the quote above, I propose we jam these errors into an object that more closely resembles an API error response, which looks like this:

{
  error_code: "InvalidRequest",
  message: "Invalid IP address",
  request_id: "xxx",
}

We could also stick the full list of Zod issues in the response under another key — I don't think anything would break as a result. The point is to have a nicer message to display in the console for dev and testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant