new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Hono · all subjects

httpexception

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

HTTPException constructor options

HTTPException is constructed with a status code and an options object. The options object can contain either a 'message' property for text responses, or a 'res' property for custom Response objects. Both can optionally include a 'cause' property for storing arbitrary data. The status code passed to the constructor is the one used in responses, even if the Response object passed to the res option has a different status.

HTTPException.getResponse() method

HTTPException includes a getResponse() method that returns a new Response object created from the error's status code and either the error message or the custom response that was provided when the exception was thrown. The getResponse() method is not aware of Context, so any headers set in Context must be manually applied to the returned Response if needed.

Handling HTTPException with app.onError

HTTPExceptions can be handled in the error handler registered with app.onError(). Use instanceof HTTPException to check if the caught error is an HTTPException, then call getResponse() to retrieve the appropriate response to return.

HTTPException custom message example

This example shows how to throw an HTTPException with a custom message: import { HTTPException } from 'hono/http-exception' throw new HTTPException(401, { message: 'Unauthorized' })

HTTPException custom response example

This example shows how to throw an HTTPException with a custom Response object: import { HTTPException } from 'hono/http-exception' const errorResponse = new Response('Unauthorized', { status: 401, // this gets ignored headers: { Authenticate: 'error="invalid_token"', }, }) throw new HTTPException(401, { res: errorResponse })

HTTPException with cause example

This example shows how to throw an HTTPException with a cause property to attach error context: import { Hono, Context } from 'hono' import { HTTPException } from 'hono/http-exception' const app = new Hono() app.post('/login', async (c) => { try { await authorize(c) } catch (cause) { throw new HTTPException(401, { message, cause }) } return c.redirect('/') })

app.onError handler for HTTPException example

This example shows how to handle HTTPExceptions in an error handler: import { Hono } from 'hono' import { HTTPException } from 'hono/http-exception' const app = new Hono() app.onError((err, c) => { if (err instanceof HTTPException) { return err.getResponse() } console.error(err) return c.text('Internal Server Error', 500) })

HTTPException is custom Hono Error

HTTPException is a custom Hono Error class used for fatal errors that simplifies returning error responses.

HTTPException getResponse() caveat with Context headers

When using HTTPException.getResponse(), the method is not aware of any headers that have been set on the Context object. If you need to preserve headers from Context in the error response, you must manually apply them to the Response returned by getResponse().

Give your agent this brain