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

@darkwolf/code-error

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@darkwolf/code-error - npm Package Compare versions

Comparing version 2.0.3 to 2.0.4

utils/CodeErrorError.js

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

2

package.json
{
"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))
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