0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

feat(context): added (optional) message argument to notFound() handler

This commit is contained in:
Marcel Overdijk 2024-09-03 17:52:26 +02:00
parent 350040470d
commit 88df68327f
4 changed files with 55 additions and 5 deletions

View File

@ -899,8 +899,8 @@ export class Context<
* })
* ```
*/
notFound = (): Response | Promise<Response> => {
notFound = (message?: string | object): Response | Promise<Response> => {
this.#notFoundHandler ??= () => new Response()
return this.#notFoundHandler(this)
return this.#notFoundHandler(this, message)
}
}

View File

@ -33,8 +33,15 @@ import { getPath, getPathNoStrict, mergePath } from './utils/url'
*/
export const COMPOSED_HANDLER = Symbol('composedHandler')
const notFoundHandler = (c: Context) => {
return c.text('404 Not Found', 404)
const notFoundHandler = (c: Context, message?: string | object) => {
if (!message) {
message = '404 Not Found'
}
const status = 404
if (typeof message === 'string') {
return c.text(message, status)
}
return c.json(message, 404)
}
const errorHandler = (err: Error | HTTPResponseError, c: Context) => {

View File

@ -1365,6 +1365,46 @@ describe('Not Found', () => {
expect(await res.text()).toBe('Custom NotFound')
})
})
describe('Not Found message as string', () => {
const app = new Hono()
app.get('/not-found', (c) => c.notFound('Custom not found message string'))
it('Custom 404 Not Found message as string', async () => {
const res = await app.request('http://localhost/not-found')
expect(res.status).toBe(404)
expect(await res.text()).toBe('Custom not found message string')
})
})
describe('Not Found message as object', () => {
const app = new Hono()
app.get('/not-found', (c) => c.notFound({ message: 'Custom not found message object' }))
it('Custom 404 Not Found message as object', async () => {
const res = await app.request('http://localhost/not-found')
expect(res.status).toBe(404)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(await res.text()).toBe('{"message":"Custom not found message object"}')
})
})
describe('Custom 404 Not Found handler with message', () => {
const app = new Hono()
app.notFound((c, message) => {
return c.text(message as string, 404)
})
app.get('/not-found', (c) => c.notFound('Custom not found handler and message'))
it('Custom 404 Not Found handler and message', async () => {
const res = await app.request('http://localhost/not-found')
expect(res.status).toBe(404)
expect(await res.text()).toBe('Custom not found handler and message')
})
})
})
describe('Redirect', () => {

View File

@ -89,7 +89,10 @@ export type H<
R extends HandlerResponse<any> = any
> = Handler<E, P, I, R> | MiddlewareHandler<E, P, I>
export type NotFoundHandler<E extends Env = any> = (c: Context<E>) => Response | Promise<Response>
export type NotFoundHandler<E extends Env = any> = (
c: Context<E>,
message?: string | object
) => Response | Promise<Response>
export interface HTTPResponseError extends Error {
getResponse: () => Response