H
Honoigmtink

What is the best way to handle the error?

What I want is to get the error from server to client using rpc + react query 1. Using trycatch 2. Returning c.json({ message: 'Invalid password' }, 401) 3. Throwing HTTPException(401, { message: 'Invalid password' })
No description
Nico
Nico25d ago
I’m not very familiar with react query but I can try my best Is the error your trying to get coming tonight the catch above?
igmtink
igmtink25d ago
I want to know how to handle errors inside the Hono server, so I can get the error within the React Query. What I used to handle the error is this: return c.json({ message: 'Invalid password' }, { status: 401 }) on the Hono server. The problem is the 'onError' option of React Query doesn't catch it, so I used try-catch to pass the error to 'onError'. Normally, when fetching using the standard Fetch API and there's an error, React Query automatically passes it to 'onError'. But when using RPC, it doesn't pass to 'onError', so I'm not sure if the way I handled the error in my API route is correct. It still returns the error message as data, so I used try-catch."
Nico
Nico25d ago
Fetch should only reject if the promise get rejected like a cors error. If there is body content even non non-200 status it will still pass You need to check the status code or response.ok. You may be thinking of Axios which does throw errors for non-200 statuses
igmtink
igmtink25d ago
here's my hono server, I don't know what to choose to handle the error
No description
igmtink
igmtink25d ago
but when I use throw new HTTPException it's not rejecting also, is it the same of return c.json({ message: 'Invalid password' }, { status: 401})?
Nico
Nico25d ago
It's not rejecting on the server?
igmtink
igmtink25d ago
this works for me, I thought trycatch is the same of !res.ok
No description
igmtink
igmtink25d ago
return Promise.reject(data) to pass the error to onError
Nico
Nico25d ago
No it will only fire if catch did not receive a response with fetch
igmtink
igmtink25d ago
I mean if the body don't have a content it will consider as reject? so I tried to use throw new HTTPException to reject the promise unlike return c.json({message}, {status: 401}) this has a content even it's error that's why I can't get the error from my server because I use trycatch because the error is returning as a data
Nico
Nico25d ago
No sorry I mean like no body content was received so like the server did not respond or it was blocked by cors Correct it still returns as data In libraries like Axios it will reject if there is another else but 2xx
igmtink
igmtink25d ago
I got used to Axios where all errors are caught in try-catch.
Nico
Nico25d ago
It's happened to me as well, I used to only use Axios
igmtink
igmtink25d ago
that's why I thought also in fetch api standard is also can catch the error using trycatch
Nico
Nico25d ago
@igmtink will you close the discussion on GitHub as well to clear it up
Want results from more Discord servers?
Add your server
More Posts
Running `bun run dev` on a newly created Cloudflare Pages example spits an error``` bun run dev $ vite (!) Could not auto-determine entry point from rollupOptions or html files andconnect my hono application endpoints with swagger uihow i can use swagger ui with my hono applicationHow to set "credentials: true" in client side using rpc?I'm using rpc with react query on the client side, this kind of fetching has already credentials: trCookies doesn't processWhen I try to run in my middleware the following ``` setCookie(c, 'test', 'res', { maxAge: 60 * 60 use Context to get vars from wrangler.tomlhow can fix this code. i want to use my openaiApiKey from the wrangler.toml when i create an openai Connect Hono Cloudflare with MongoDBHi. I am using Hono and deploying to Cloudflare. However I am unable to find a way to connect mongodCase insensitive query paramsWe are using hono-zod-openapi to replace an existing API. Sadly the old API (written in ASP.Net) appPassword hashingHow to hash password?How to setup Hono with Remix/Cloudflare for Vite dev server?From the documentation for Cloudflare Pages, there is an adapter and a plugin, but it is not clear hValidate response objectsUsing the zod validator middleware it is possible to validate params , query, request body like: ``Hono Oauth Provider with JWT issueHey everyone! I'm not sure if this is the right place for me to post about this but recently I wanteDeploy Hono / Bun APII know, I know this is such a newby question but I come from small express apps and other simple ‘clIs there a way to list all request headers ?I have this use case where I am not sure which header key is being sent, I would like to log all heaTyping Middleware JWT and othersCurrently I am trying to type the `c.get('jwtPayload')` that is documented [here](https://hono.dev/mHow to generate openapi.yaml spec with Zod OpenAPI Hono?Is it possible to generate the openapi.yaml spec with Zod OpenAPI Hono from the commandline using a Why use Hono for Next.js backend (route handlers)?I'm creating a full-stack web app and probably using Next.js API/route handler: https://nextjs.org/dContext not finalizedIm trying to build a S3 API and it returns this on every requests ```js Error: Context is not finalType checking with Hono + prisma + zod -> return json date (expect string)HI, i m usin openAPi I declared return params in my route ``` const getUserRoute = createRoute({ Deploying Hono App to Multiple RuntimesHow can I deploy my Hono app to multiple different runtimes simultaneously within the same project? Sourcemap Support for Sentry MiddlewareThere's some way to improve the DX of using Sentry with Hono? I'm receiving the issues on my Sentry