feat(http/unstable): HttpError#7132
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7132 +/- ##
==========================================
- Coverage 94.61% 94.61% -0.01%
==========================================
Files 634 635 +1
Lines 51801 51845 +44
Branches 9329 9342 +13
==========================================
+ Hits 49011 49052 +41
- Misses 2216 2218 +2
- Partials 574 575 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
What should happen here? import { HttpError } from '@std/http/unstable-error'
const { addr: { hostname, port } } = Deno.serve(() => new Response(null, {
status: 555,
}))
const res = await fetch(`http://${hostname}:${port}/`)
console.error(new HttpError(res.status))Current behaviour:
Also:
IMO current behaviour is fine as you can just cast |
|
On second thought, setting the |
fibibot
left a comment
There was a problem hiding this comment.
- JSDoc on the
initproperty (lines 119-121) claimsinit.statusandinit.statusTextalways reflect the standard HTTP values for the given status code. Neither is true after the recent commit:init = { status, ...options?.init }never assignsstatusText, andoptions.init.statuswins over the constructor'sstatus(spread comes last). Either re-add the auto-population, or rewrite the paragraph to describe the actual contract (init.statusdefaults tostatusand is overridable;init.statusTextis whatever the user passes).
- nit: while you're in there, the constructor doc block (lines 145-156) duplicates the class-level description verbatim. Constructor JSDoc can be a one-liner or just dropped — TypeDoc/LSP already shows the class doc on
new HttpError(...). - nit: subtests are named
"initialises with correct defaults"/"initialises with custom properties". The repo convention issymbolName() does X when Y— e.g."new HttpError() defaults message to STATUS_TEXT[status]".
This PR brings back the
HttpErrorclass, which I regret removing in #3736 (sorry Kitson 🥲). It's a very versatile class that can be called anywhere in the call stack and handled in a unified way in the "master" request handler. I've been using this in my own projects and love it.This class is already implemented in Oak (similarly) and Fresh (a more minimal version).
I think it'd also be worth updating handlers within
@std/httpto throw errors instead of returning responses, allowing the dev to handle their own errors.Written by me. Checked by Claude Code.