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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
11 changes: 9 additions & 2 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
40 changes: 40 additions & 0 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading