Replies: 2 comments
-
|
What you want to do is interesting.
Currently, it does not support |
Beta Was this translation helpful? Give feedback.
-
|
For workaround, I ended up create custom type like this import { ClientResponse } from "hono/client";
import { ResponseFormat } from "hono/types";
import { ClientErrorStatusCode, ServerErrorStatusCode } from "hono/utils/http-status";
interface ErrorData {
message: string,
errors: Record<string,string[]>
} // adjust it with your backend error response
export type ErrorResponse = ClientResponse<ErrorData, ClientErrorStatusCode|ServerErrorStatusCode, "json">;and this is the example how to use it export function handleResponse<R extends ClientResponse<unknown, number, ResponseFormat>>(res: R){
if(res.ok) return res.json();
const err = res as unknown as ErrorResponse;
if(err.status == 422){
throw new ValidationError(err);
}
throw new ServerError(err);
}In my example, the error response is well formatted from the backend |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using custom errors to simplify and standardize formatting of responses returned to the client. An example is the validation error below.
I can then throw a validation error and not worry about specific status codes, and later also add specific validation messages that specify which fields were invalid in a POST request for example, and display those at appropriate places in a client side form.
This is used with a custom
onErrorhandler:When used with hono/client, the response I receive is typed as successful response and does not allow for a
bodytype adjusted to the error version.Typescript will show an error for the code above, saying
Object literal may only specify known properties, and 'message' does not exist in type ....Is there a way I could customize the
ClientResponsetype based on the status code, so that it understands there is a successful and a failed response? It's really ugly having to writebody as xyall over the code.Thank you
Beta Was this translation helpful? Give feedback.
All reactions