Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@adonisjs/generic-exceptions

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adonisjs/generic-exceptions - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

10

CHANGELOG.md

@@ -0,1 +1,11 @@

<a name="2.0.0"></a>
# [2.0.0](https://github.com/adonisjs/adonis-generic-exceptions/compare/v1.0.0...v2.0.0) (2018-01-16)
### Features
* add link to repo & update readme ([a1da015](https://github.com/adonisjs/adonis-generic-exceptions/commit/a1da015))
<a name="1.0.0"></a>

@@ -2,0 +12,0 @@ # 1.0.0 (2017-08-01)

2

index.js

@@ -12,5 +12,5 @@ 'use strict'

const { HttpException, LogicalException } = require('node-exceptions')
const InvalidArgumentException = require('./src/InvalidArgumentException')
const RuntimeException = require('./src/RuntimeException')
const { HttpException, LogicalException } = require('node-exceptions')

@@ -17,0 +17,0 @@ module.exports = {

{
"name": "@adonisjs/generic-exceptions",
"version": "1.0.0",
"version": "2.0.0",
"description": "List of generic exceptions to be used in all other repos",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/adonisjs/adonis-generic-exceptions.git"
},
"scripts": {

@@ -17,11 +21,11 @@ "test": "echo \"Error: no test specified\" && exit 1"

"dependencies": {
"node-exceptions": "^2.0.2",
"upcast": "^1.0.4"
"node-exceptions": "^3.0.0",
"upcast": "^2.1.1"
},
"devDependencies": {
"japa": "^1.0.3",
"japa": "^1.0.6",
"japa-cli": "^1.0.1",
"standard": "^10.0.2",
"standard": "^10.0.3",
"tern": "^0.21.0"
}
}
# AdonisJs Generic Exceptions 🚀
> Understandable custom exceptions for Adonisjs
> Customized exceptions for AdonisJs
[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Appveyor][appveyor-image]][appveyor-url]
[![Coveralls][coveralls-image]][coveralls-url]
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.
This repo contains a list of exceptions all modules makes use of to throw informative exceptions.
## Setup
Install package using npm.
<img src="http://res.cloudinary.com/adonisjs/image/upload/q_100/v1497112678/adonis-purple_pzkmzt.svg" width="200px" align="right" hspace="30px" vspace="100px">
```js
npm i @adonisjs/generic-exceptions
```
## Tests
Tests are written using [japa](http://github.com/thetutlage/japa). Run the following commands to run tests.
## Usage
The package exports 4 different exceptions, defined as follows.
```bash
npm run test:local
```js
const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
# report coverage
npm run test
const message = 'Model.create requires an object'
const status = 400
const code = 'E_INVALID_ARGUMENT'
# on windows
npm run test:win
throw new InvalidArgumentException(message, status, code)
```
## Release History
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`.
Checkout [CHANGELOG.md](CHANGELOG.md) file for release history.
## Available exceptions
## Meta
```js
const GE = require('@adonisjs/generic-exceptions')
AdonisJs – [@adonisframework](https://twitter.com/adonisframework) – virk@adonisjs.com
new GE.InvalidArgumentException()
new GE.RuntimeException()
new GE.HttpException()
new GE.LogicalException()
```
Checkout [LICENSE.txt](LICENSE.txt) for license information
## Static methods
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.
Harminder Virk (Aman) - [https://github.com/thetutlage](https://github.com/thetutlage)
#### missingParamter(method, parameterName, position)
```js
const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/adonis-generic-exceptions/master.svg?style=flat-square
throw InvalidArgumentException.missingParameter('model.create', 'payload', '1st')
```
[appveyor-url]: https://ci.appveyor.com/project/thetutlage/adonis-generic-exceptions
Output
[npm-image]: https://img.shields.io/npm/v/@adonisjs/generic-exceptions.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@adonisjs/generic-exceptions
```
// Missing parameter payload expected by model.create method as 1st parameter
```
[travis-image]: https://img.shields.io/travis/poppinss/adonis-generic-exceptions/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/poppinss/adonis-generic-exceptions
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.
[coveralls-image]: https://img.shields.io/coveralls/poppinss/adonis-generic-exceptions/develop.svg?style=flat-square
#### invalidParameter(errorMessage, [originalValue])
```js
throw InvalidArgumentException.invalidParameter('username must be a string', originalValue)
```
[coveralls-url]: https://coveralls.io/github/poppinss/adonis-generic-exceptions
Output
```
// username must be a string instead received null
```
#### missingConfig(key, file)
Thrown when value for a given key inside configuration file is missing.
```js
const { RuntimeException } = require('@adonisjs/generic-exceptions')
throw RuntimeException.missingConfig('mysql', 'config/database.js')
```
Output
```
mysql is not defined inside config/database.js file
```
#### missingAppKey(providerName)
Thrown when value for `appKey` is not defined.
```js
throw RuntimeException.missingAppKey('Redis')
```
Output
```
Make sure to define appKey inside config/app.js file before using Redis provider
```
#### incompleteConfig(missingKeys, file)
Raised when config file and value exists, but is not complete.
```js
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.
```
#### invoke(message, status, [code])
Same as creating a new instance, but the default error `code` is used automatically.
```js
throw InvalidArgumentException.invoke('Custom message', 500)
throw RuntimeException.invoke('Custom message', 500)
```

@@ -22,2 +22,6 @@ 'use strict'

class InvalidArgumentException extends NE.InvalidArgumentException {
static get repo () {
return 'adonisjs/errors'
}
/**

@@ -36,4 +40,4 @@ * Throw an exception when there is a missing parameter

static missingParameter (method, parameterName, position) {
const message = `Missing parameter ${parameterName} expected by ${method} as ${position} parameter`
return new this(message, 500, 'E_MISSING_PARAMETER')
const message = `Missing parameter ${parameterName} expected by ${method} method as ${position} parameter`
return new this(message, 500, 'E_MISSING_PARAMETER', this.repo)
}

@@ -53,4 +57,7 @@

static invalidParameter (errorMessage, originalValue) {
errorMessage = originalValue ? `${errorMessage} instead received ${upcast.type(originalValue)}` : errorMessage
return new this(errorMessage, 500, 'E_INVALID_PARAMETER')
const message = originalValue !== undefined
? `${errorMessage} instead received ${upcast.type(originalValue)}`
: errorMessage
return new this(message, 500, 'E_INVALID_PARAMETER', this.repo)
}

@@ -71,3 +78,3 @@

static invoke (message, status = 500, code = 'E_INVALID_ARGUMENT') {
return new this(message, status, code)
return new this(message, status, code, this.repo)
}

@@ -74,0 +81,0 @@ }

@@ -21,2 +21,6 @@ 'use strict'

class RuntimeException extends NE.RuntimeException {
static get repo () {
return 'adonisjs/errors'
}
/**

@@ -35,3 +39,3 @@ * Missing config exception is thrown when configuration

const message = `${key} is not defined inside ${configLocation} file`
return new this(message, 500, 'E_MISSING_CONFIG')
return new this(message, 500, 'E_MISSING_CONFIG', this.repo)
}

@@ -52,3 +56,3 @@

const message = `Make sure to define appKey inside config/app.js file before using ${provider} provider`
return new this(message, 500, 'E_MISSING_APP_KEY')
return new this(message, 500, 'E_MISSING_APP_KEY', this.repo)
}

@@ -62,11 +66,12 @@

*
* @param {String} forKey
* @param {Array} missingKeys
* @param {String} file
* @param {String} forKey
*
* @return {RuntimeException}
*/
static incompleteConfig (forKey, missingKeys, file) {
const message = `Make sure to define ${missingKeys.join(', ')} on ${forKey} inside ${file}`
return new this(message, 500, 'E_INCOMPLETE_CONFIG')
static incompleteConfig (missingKeys, file, forKey) {
const baseMessage = `Make sure to define ${missingKeys.join(', ')}`
const message = forKey ? `${baseMessage} on ${forKey} inside ${file}` : `${baseMessage} inside ${file}`
return new this(message, 500, 'E_INCOMPLETE_CONFIG', this.repo)
}

@@ -87,3 +92,3 @@

static invoke (message, status = 500, code = 'E_RUNTIME_ERROR') {
return new this(message, status, code)
return new this(message, status, code, this.repo)
}

@@ -90,0 +95,0 @@ }

@@ -26,1 +26,28 @@ 'use strict'

})
test('each exception should link to errors repo', (assert) => {
assert.equal(
GE.InvalidArgumentException.missingParameter('lucid', 'username', '2nd').message,
'E_MISSING_PARAMETER: Missing parameter username expected by lucid as 2nd parameter\n> More details: https://err.sh/adonisjs/errors/E_MISSING_PARAMETER'
)
assert.equal(
GE.InvalidArgumentException.invalidParameter('username').message,
'E_INVALID_PARAMETER: username\n> More details: https://err.sh/adonisjs/errors/E_INVALID_PARAMETER'
)
assert.equal(
GE.RuntimeException.missingConfig('connection', 'database').message,
'E_MISSING_CONFIG: connection is not defined inside database file\n> More details: https://err.sh/adonisjs/errors/E_MISSING_CONFIG'
)
assert.equal(
GE.RuntimeException.missingAppKey('Route').message,
'E_MISSING_APP_KEY: Make sure to define appKey inside config/app.js file before using Route provider\n> More details: https://err.sh/adonisjs/errors/E_MISSING_APP_KEY'
)
assert.equal(
GE.RuntimeException.incompleteConfig(['foo'], 'bar').message,
'E_INCOMPLETE_CONFIG: Make sure to define foo inside bar\n> More details: https://err.sh/adonisjs/errors/E_INCOMPLETE_CONFIG'
)
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc