Throwlhos
Throws error objects with status and code
throw Error('What the heck goes here?...')
You do not know exactly what to throw?! We've got you covered!


Installation
The latest version is available at: https://www.npmjs.com/package/throwlhos
Use your favorite package manager to install. For instance:
yarn add throwlhos
Basic Usage
import throwlhos from 'throwlhos'
import { Request, Response } from 'express'
class FooBarController {
async index(request: Request, response: Response) {
console.log(throwlhos.err_internalServerError())
}
}
Above code prompts the following output:
{
code: 500,
status: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error'
}
err_*
methods accept two optional parameters:
message?: string | null
errors?: any
Returned throwlhos
object type exaplained:
export type IThrowlhos = {
code: number
status: string
message: string
errors: any
}
-
code
: Number value of HTTP Status Code
-
status
: String value of HTTP Status Name
-
message
: Message given at first parameter or HTTP Status Name human-readable
if none or null is given.
-
errors
: Anything given as the second parameter. It's undefined
if no value is given.
Throwlhos as an express middleware
You can use throwlhos
as an express middleware:
app.use(throwlhos.middlware)
Since throwlhos
overwrites Express interface, you can find throwlhos
err_*
methods directly in express response!
Consider the following code which is an express middlware:
import throwlhos from 'throwlhos'
import { Request, Response } from 'express'
class FooBarController {
async index(request: Request, response: Response) {
const throwlhos = response.err_internalServerError()
return response.status(throwlhos.code).send(throwlhos)
}
}
Outputs:
HTTP/1.1 500 INTERNAL_SERVER_ERROR
X-Powered-By: Express
errors-Type: application/json; charset=utf-8
{
code: 500,
status: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error'
}
Usage with an express Error Handler (recommended)
If you want to quickly send an error response with your throwlhos
use it with responserror.
Responserror handler will catch-all errors from any express middleware router in-between by using next(err)
.
Full example:
import throwlhos from 'throwlhos'
import errorHandler from 'responserror'
const app = express()
const router = express.Router()
router.post('/resources', (_, response: Response, next: NextFunction) => {
try {
throw response.err_forbidden('Access denied.. Sorry')
} catch(err) {
return next(err)
}
})
app.use(throwlhos.middlware)
app.use(router)
app.use(errorHandler)
POST /resources
outputs:
HTTP/1.1 403 FORBIDDEN
X-Powered-By: Express
errors-Type: application/json; charset=utf-8
{
code: 403,
status: 'FORBIDDEN',
message: 'Access denied.. Sorry',
success: false
}
Available Methods
Throwlhos currently supports following methods:
err_multipleChoices
err_movedPermanently
err_movedTemporarily
err_seeOther
err_notModified
err_useProxy
err_temporaryRedirect
err_permanentRedirect
err_badRequest
err_unauthorized
err_paymentRequired
err_forbidden
err_notFound
err_methodNotAllowed
err_notAcceptable
err_proxyAuthenticationRequired
err_requestTimeout
err_conflict
err_gone
err_lengthRequired
err_preconditionFailed
err_requestTooLong
err_requestUriTooLong
err_unsupportedMediaType
err_requestedRangeNotSatisfiable
err_expectationFailed
err_imATeapot
err_insufficientSpaceOnResource
err_methodFailure
err_misdirectedRequest
err_unprocessableEntity
err_locked
err_failedDependency
err_preconditionRequired
err_tooManyRequests
err_requestHeaderFieldsTooLarge
err_unavailableForLegalReasons
err_internalServerError
err_notImplemented
err_badGateway
err_serviceUnavailable
err_gatewayTimeout
err_httpVersionNotSupported
err_insufficientStorage
err_networkAuthenticationRequired
err_custom
More
Special thanks to my fellow engineer Estanis as his words where my inspiration in the naming of the package.
Testing
Run the test suit with yarn test
.
Contributing
If you want to contribute in any of theses ways:
- Give your ideas or feedback
- Question something
- Point out a problem or issue
- Enhance the code or its documentation
- Help in any other way
You can (and should) open an issue or even a pull request.
Has this package been useful for you?
If so, you can contribute by giving it a Star ⭐ at the GitHub Repository!
Thanks for your interest in contributing to this repo!
Author
Luiz Felipe Zarco (felipezarco@hotmail.com)
License
This code is licensed under the MIT License. See the LICENSE.md file for more info.