![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
responserror
Advanced tools
Express Error Handler to Node.js apps
Responserror is an Error Handler middleware for express.
The latest version is available at: https://www.npmjs.com/package/responserror
Use your favorite package manager to install. For instance:
yarn add responserror
Then import it and initialize a new instance:
import Responserror from 'responserror'
const { errorHandler } = new Responserror()
Then, you will want to add the errorHandler
as your last express middleware:
app.use(authRouter, userRouter, errorHandler)
And you're good to go!
import express from 'express'
import Responserror from 'responserror'
const app = express()
const router = express.Router()
const { errorHandler } = new Responserror()
router.post('/users', (_, response: Response, next: NextFunction) => {
try {
throw {
code: 504,
}
} catch(err) {
return next(err)
}
})
app.use(router, errorHandler)
Outputs HTTP Status 504 and following JSON response:
{
code: 504,
status: 'GATEWAY_TIMEOUT',
message: 'Gateway Timeout',
success: false
}
code: if given responserror
will try to find its status
and message
automatically.
status: if given responserror
will try to find its code
and message
automatically.
success: will always be false
, unless specified otherwise.
message: will be filled automatically if given code
or status
are valid and no message
value is given.
errors: anything can be sent here, responserror
will not try to fill this.
Note: if message
is given a value, that will override the automatic value responserror
would give. This applies to all other properties.
All properties are optional as shown in the first example.
message
and errors
as well:router.post('/users', (_, response: Response, next: NextFunction) => {
try {
throw {
code: 400,
message: 'Sorry!! Your request is invalid! =/',
errors: [
{ name: 'clientFullName', message: 'The full name needs to contain more than one word!' }
]
}
} catch(err) {
return next(err)
}
})
Outputs HTTP Status 400 and following JSON response:
{
code: 400,
status: 'BAD_REQUEST',
message: 'Sorry!! Your request is invalid! =/',
errors: [
{ name: 'clientFullName', message: 'The full name needs to contain more than one word!' }
]
}
Use method pre
to execute functions before sending the response:
const app = express()
app.use(responser)
const responserror = new Responserror({ promptErrors: true })
const errorHandler = responserror.errorHandler
responserror.pre(() => {
console.info('This will execute before the error response is given!')
})
const router = express.Router()
app.use(router, errorHandler)
While pre
will execute as the first thing responserror
will do, method pos
will execute as the last thing responserror will do before sending the response.
100 // Continue
101 // Switching Protocols
102 // Processing
200 // OK
201 // Created
202 // Accepted
203 // Non Authoritative Information
204 // No Content
205 // Reset Content
206 // Partial Content
207 // Multi-Status
300 // Multiple Choices
301 // Moved Permanently
302 // Moved Temporarily
303 // See Other
304 // Not Modified
305 // Use Proxy
307 // Temporary Redirect
308 // Permanent Redirect
400 // Bad Request
401 // Unauthorized
402 // Payment Required
403 // Forbidden
404 // Not Found
405 // Method Not Allowed
406 // Not Acceptable
407 // Proxy Authentication Required
408 // Request Timeout
409 // Conflict
410 // Gone
411 // Length Required
412 // Precondition Failed
413 // Request Entity Too Large
414 // Request-URI Too Long
415 // Unsupported Media Type
416 // Requested Range Not Satisfiable
417 // Expectation Failed
418 // I'm a teapot
419 // Insufficient Space on Resource
420 // Method Failure
422 // Unprocessable Entity
423 // Locked
424 // Failed Dependency
428 // Precondition Required
429 // Too Many Requests
431 // Request Header Fields Too Large
451 // Unavailable For Legal Reasons
500 // Internal Server Error
501 // Not Implemented
502 // Bad Gateway
503 // Service Unavailable
504 // Gateway Timeout
505 // HTTP Version Not Supported
507 // Insufficient Storage
511 // Network Authentication Required
Check the updated list of http status codes for all status and codes available.
If you want to have all HTTP responses at the tip of your finger (including sucessful ones), be sure to check out responser npm package. Differently from responserror
which is the "catch-all" error handler for express, responser
is used to directly send responses, wherever middleware/controller you are. Both responser
and responserror
can be used together.
If you are using responser module in the same express instance, responserror
will invoke send_* methods instead of its own.
Run the test suit with yarn test
.
If you want to contribute in any of theses ways:
You can (and should) open an issue or even a pull request!
Thanks for your interest in contributing to this repo!
Luiz Felipe Zarco (felipezarco@hotmail.com)
This code is licensed under the MIT License. See the LICENSE.md file for more info.
FAQs
Error Handler Middlware for Express
We found that responserror demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.