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

Conversation

marceloverdijk
Copy link
Contributor

This PR adds an optional message argument (either string or object) to the notFound() handler.

E.g. this allows users to do:

app.get('/notfound', (c) => {
  return c.notFound('Customer not found.')
})

or even:

app.get('/notfound', (c) => {
  return c.notFound({ message: 'Customer not found.' })
})

The latter option (when an object is provided) will automatically result in a json response.

Of course the default behavior is still applied:

app.get('/notfound', (c) => {
  return c.notFound()
})

will just return in a text response containing 404 Not Found.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Sep 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.77%. Comparing base (3500404) to head (88df683).

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #3369    +/-   ##
========================================
  Coverage   95.77%   95.77%            
========================================
  Files         152      152            
  Lines        9187     9194     +7     
  Branches     2818     2679   -139     
========================================
+ Hits         8799     8806     +7     
  Misses        388      388            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member

yusukebe commented Sep 5, 2024

Hi @marceloverdijk

I don't like this feature. Because it should have more code for detecting objects like using typeof. The hono-base.ts should be small. And, the behavior may be unexpected if the user sets the custom not found handler with app.notFound(). For example, message arg in c.notFound(message) will not affect the custom handler.

@marceloverdijk
Copy link
Contributor Author

marceloverdijk commented Sep 5, 2024

OK np, do you have other suggestions?

Or is it recommended users should simply use c.text('my message', 404) or c.json({ .. }, 404) when a custom text / json response is desired for a 404 / Not Found

I like the semantics of c.notFound(..) (but desire a custom response message)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants