Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@adonisjs/generic-exceptions
Advanced tools
Customized exceptions for AdonisJs
This repo contains some helpful classes to throw uniform exceptions through out the application. Ofcourse you can throw exceptions using new Error()
but using this package will help in throwing informative exceptions.
Install package using npm.
npm i @adonisjs/generic-exceptions
The package exports 4 different exceptions, defined as follows.
const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
const message = 'Model.create requires an object'
const status = 400
const code = 'E_INVALID_ARGUMENT'
throw new InvalidArgumentException(message, status, code)
The status
must be a valid HTTP status code and code
is a unique error code to recognize an exception. AdonisJs error codes starts with E_
, for example: E_MISSING_CONFIG
.
const GE = require('@adonisjs/generic-exceptions')
new GE.InvalidArgumentException()
new GE.RuntimeException()
new GE.HttpException()
new GE.LogicalException()
For commonly thrown exceptions, AdonisJs provides you a handful of static methods, so that you don't have to remember the error codes
for those exceptions.
const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
throw InvalidArgumentException.missingParameter('model.create', 'payload', '1st')
Output
// Missing parameter payload expected by model.create method as 1st parameter
As you can see it returns a properly formatted error message by using the input values. This way we can keep all the exception messages to have same language.
throw InvalidArgumentException.invalidParameter('username must be a string', originalValue)
Output
// username must be a string instead received null
Thrown when value for a given key inside configuration file is missing.
const { RuntimeException } = require('@adonisjs/generic-exceptions')
throw RuntimeException.missingConfig('mysql', 'config/database.js')
Output
mysql is not defined inside config/database.js file
Thrown when value for appKey
is not defined.
throw RuntimeException.missingAppKey('Redis')
Output
Make sure to define appKey inside config/app.js file before using Redis provider
Raised when config file and value exists, but is not complete.
const { RuntimeException } = require('@adonisjs/generic-exceptions')
throw RuntimeException.incompleteConfig(['mysql.username', 'mysql.database'], 'config/database.js')
Output
Make sure to define mysql.username, mysql.database inside config/database.js file.
Same as creating a new instance, but the default error code
is used automatically.
throw InvalidArgumentException.invoke('Custom message', 500)
throw RuntimeException.invoke('Custom message', 500)
FAQs
List of generic exceptions to be used in all other repos
The npm package @adonisjs/generic-exceptions receives a total of 13,622 weekly downloads. As such, @adonisjs/generic-exceptions popularity was classified as popular.
We found that @adonisjs/generic-exceptions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.