hono icon indicating copy to clipboard operation
hono copied to clipboard

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

Open marceloverdijk opened this issue 1 year ago • 3 comments

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

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

marceloverdijk avatar Sep 03 '24 15:09 marceloverdijk

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 95.77%. Comparing base (3500404) to head (88df683). :warning: Report is 384 commits behind head on main.

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            

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Sep 03 '24 15:09 codecov[bot]

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.

yusukebe avatar Sep 05 '24 06:09 yusukebe

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)

marceloverdijk avatar Sep 05 '24 06:09 marceloverdijk