Skip to content

Instantly share code, notes, and snippets.

@rwillians
Created February 13, 2024 18:50
Show Gist options
  • Save rwillians/a58df88819cf1f7b32f8449e76804cd0 to your computer and use it in GitHub Desktop.
Save rwillians/a58df88819cf1f7b32f8449e76804cd0 to your computer and use it in GitHub Desktop.
express rescue
const rescue = require('express-rescue')
class UserNotFound extends Error {}
// ...
app.get('/users/:id', [
rescue(async (req, res) => {
throw new UserNotFound(/* ... */)
},
resuce.from(UserNotFound, (err, req, res) => {
res.status(404)
.json({ message: 'user not found' })
})
])
const rescue = require('express-rescue')
class UserNotFound extends Error {}
class HttpError extends Error{
constructor({ status, message }) { /* ... */ }
}
class NotFound extends HttpError {
constructor(message) { super({ status: 404, message }) }
}
// ...
app.get('/users/:id', [
rescue(async (req, res) => {
throw new UserNotFound(/* ... */)
},
resuce.from(UserNotFound, (err, req, res) => {
throw new NotFound('user not found')
})
])
app.use(rescue.from(HttpError, (err, req, res) => {
res.status(err.status)
.json({ message: err.message })
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment