@darkwolf/code-error
Advanced tools
Comparing version 2.0.3 to 2.0.4
288
index.js
@@ -1,3 +0,1 @@ | ||
const State = require('@darkwolf/state') | ||
class CodeError extends Error { | ||
@@ -7,12 +5,187 @@ constructor(...args) { | ||
if (args.length <= 2) { | ||
const [code, message] = args | ||
this.code = code | ||
this.message = message | ||
} else { | ||
const [namespace, code, message] = args | ||
this.namespace = namespace | ||
this.space = namespace | ||
this.code = code | ||
this.message = message | ||
switch (args.length) { | ||
case 1: { | ||
const [options] = args | ||
if (!options) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_OPTIONS | ||
.setType('argument-required') | ||
.setMessage('"options" argument is required') | ||
} | ||
if (typeof options !== 'object') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_OPTIONS | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "options"', | ||
'"options" must be an object' | ||
].join('\n')) | ||
} | ||
const { | ||
namespace, | ||
code, | ||
type, | ||
message, | ||
state | ||
} = options | ||
if (namespace && typeof namespace !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_NAMESPACE | ||
.setType('invalid-property-type') | ||
.setMessage([ | ||
'Invalid property type "namespace"', | ||
'"namespace" must be a string' | ||
].join('\n')) | ||
} | ||
if (!code) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('property-required') | ||
.setMessage('"code" property is required') | ||
} | ||
if (typeof code !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('invalid-property-type') | ||
.setMessage([ | ||
'Invalid property type "code"', | ||
'"code" must be a string' | ||
].join('\n')) | ||
} | ||
if (type && typeof type !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_TYPE | ||
.setType('invalid-property-type') | ||
.setMessage([ | ||
'Invalid property type "type"', | ||
'"type" must be a string' | ||
].join('\n')) | ||
} | ||
if (!message) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('property-required') | ||
.setMessage('"message" property is required') | ||
} | ||
if (typeof message !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('invalid-property-type') | ||
.setMessage([ | ||
'Invalid property type "message"', | ||
'"message" must be a string' | ||
].join('\n')) | ||
} | ||
if (state && typeof state !== 'object') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_STATE | ||
.setType('invalid-property-type') | ||
.setMessage([ | ||
'Invalid property type "state"', | ||
'"state" must be an object' | ||
].join('\n')) | ||
} | ||
this.namespace = namespace | ||
this.code = code | ||
this.type = type | ||
this.message = message | ||
this.state = state | ||
break | ||
} | ||
case 2: { | ||
const [code, message] = args | ||
if (!code) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('argument-required') | ||
.setMessage('"code" argument is required') | ||
} | ||
if (typeof code !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "code"', | ||
'"code" must be a string' | ||
].join('\n')) | ||
} | ||
if (!message) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('argument-required') | ||
.setMessage('"message" argument is required') | ||
} | ||
if (typeof message !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "message"', | ||
'"message" must be a string' | ||
].join('\n')) | ||
} | ||
this.code = code | ||
this.message = message | ||
break | ||
} | ||
case 3: { | ||
const [code, type, message] = args | ||
if (!code) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('argument-required') | ||
.setMessage('"code" argument is required') | ||
} | ||
if (typeof code !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "code"', | ||
'"code" must be a string' | ||
].join('\n')) | ||
} | ||
if (!type) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_TYPE | ||
.setType('argument-required') | ||
.setMessage('"type" argument is required') | ||
} | ||
if (typeof type !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_TYPE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "type"', | ||
'"type" must be a string' | ||
].join('\n')) | ||
} | ||
if (!message) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('argument-required') | ||
.setMessage('"message" argument is required') | ||
} | ||
if (typeof message !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "message"', | ||
'"message" must be a string' | ||
].join('\n')) | ||
} | ||
this.code = code | ||
this.type = type | ||
this.message = message | ||
break | ||
} | ||
} | ||
@@ -25,9 +198,94 @@ | ||
} | ||
} | ||
const state = new State() | ||
this.state = state.state | ||
this.setState = state.setState | ||
setNamespace(namespace) { | ||
if (typeof namespace !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_NAMESPACE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "namespace"', | ||
'"namespace" must be a string' | ||
].join('\n')) | ||
} | ||
this.namespace = namespace | ||
return this | ||
} | ||
setCode(code) { | ||
if (typeof code !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_CODE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "code"', | ||
'"code" must be a string' | ||
].join('\n')) | ||
} | ||
this.code = code | ||
return this | ||
} | ||
setType(type) { | ||
if (typeof type !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_TYPE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "type"', | ||
'"type" must be a string' | ||
].join('\n')) | ||
} | ||
this.type = type | ||
return this | ||
} | ||
setMessage(message) { | ||
if (typeof message !== 'string') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_MESSAGE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "message"', | ||
'"message" must be a string' | ||
].join('\n')) | ||
} | ||
this.message = message | ||
return this | ||
} | ||
setState(state) { | ||
if (!['object', 'function'].includes(typeof state)) { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_STATE | ||
.setType('invalid-argument-type') | ||
.setMessage([ | ||
'Invalid argument type "state"', | ||
'"state" must be an object or a callback function' | ||
].join('\n')) | ||
} | ||
switch (typeof state) { | ||
case 'object': { | ||
this.state = state | ||
break | ||
} | ||
case 'function': { | ||
const newState = state(this.state) | ||
if (typeof newState !== 'object') { | ||
const errors = require('./utils/errors') | ||
throw errors.INVALID_STATE | ||
.setType('invalid-return-type') | ||
.setMessage([ | ||
'Invalid return type by callback function "state"', | ||
'"state" must return an object' | ||
].join('\n')) | ||
} | ||
this.state = newState | ||
break | ||
} | ||
} | ||
return this | ||
} | ||
} | ||
module.exports = CodeError |
{ | ||
"name": "@darkwolf/code-error", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Code Error Utility", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# Code Error Utility | ||
## Contact Me | ||
### Telegram: @PavelWolfDark or https://t.me/PavelWolfDark | ||
## Donation | ||
#### You can donate to the development of open source projects | ||
#### BTC Address | ||
### 15sjjAUtJdB1ncsxKK7KtyJPtF46UhXWo4 | ||
#### Thanks!:3 | ||
## Install | ||
@@ -6,2 +13,18 @@ ```sh | ||
``` | ||
## Concept | ||
#### Custom error class instances must be called a namespace using PascalCase and end with 'Error' (ex. CustomNamespaceError) | ||
#### Use kebab-case to name namespaces, codes, and error types | ||
#### Use the following types for error types: | ||
###### argument-required | ||
###### invalid-argument-type | ||
###### invalid-argument-value | ||
###### property-required | ||
###### invalid-property-type | ||
###### invalid-property-value | ||
###### unknown-property | ||
###### invalid-return-type | ||
###### invalid-return-value | ||
#### Pass state to error instance | ||
#### Use a directory called 'utils' to store custom error class instances, errors and validators | ||
#### Following this concept, we can easily handle errors exceptions dynamically | ||
## Usage | ||
@@ -11,12 +34,17 @@ ```javascript | ||
const CODE_ERROR = new CodeError(code, message) | ||
const SPACE_CODE_ERROR = new CodeError(namespace, code, message) | ||
class CustomNamespaceError extends CodeError { | ||
constructor(code, message) { | ||
super({namespace: 'custom-namespace', code, message}) | ||
} | ||
} | ||
const INVALID_VALUE = new CustomNamespaceError('invalid-value', 'Invalid value') | ||
try { | ||
if (!isValid()) throw CODE_ERROR | ||
throw INVALID_VALUE | ||
} catch (e) { | ||
switch (e.space) { | ||
case 'namespace': { | ||
switch (e.namespace) { | ||
case 'custom-namespace': { | ||
switch (e.code) { | ||
case 'invalid-code': { | ||
case 'invalid-value': { | ||
doSmth() | ||
@@ -32,6 +60,11 @@ break | ||
} | ||
``` | ||
## Initialization | ||
### new CodeError(namespace?, code, message) | ||
## Init | ||
### new CodeError({namespace, code, type, message, state} || [code[, type?[, message]]]) | ||
## Methods | ||
### setState(newState || callback(state => newState)) | ||
### setNamespace(namespace) | ||
### setCode(code) | ||
### setType(type) | ||
### setMessage(message) | ||
### setState(state || callback(state => newState)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13961
8
319
68
1