New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.4 to 2.0.6

utils/validator.js

279

index.js

@@ -0,1 +1,3 @@

const validator = require('./utils/validator')
class CodeError extends Error {

@@ -5,187 +7,38 @@ constructor(...args) {

switch (args.length) {
case 1: {
const [options] = args
if (args.length <= 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'))
}
validator.isValidOptions(options)
const {
namespace,
code,
type,
message,
state
} = options
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
} else if (args.length === 2) {
const [code, message] = args
this.namespace = namespace
this.code = code
this.type = type
this.message = message
this.state = state
break
}
case 2: {
const [code, message] = args
validator.isValidCodeArgument(code)
validator.isValidMessageArgument(message)
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
} else {
const [code, type, message] = args
this.code = code
this.message = message
break
}
case 3: {
const [code, type, message] = args
validator.isValidCodeArgument(code)
validator.isValidTypeArgument(type)
validator.isValidMessageArgument(message)
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
}
this.code = code
this.type = type
this.message = message
}

@@ -201,11 +54,3 @@

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'))
}
validator.isValidNamespaceArgument(namespace)
this.namespace = namespace

@@ -216,11 +61,3 @@ 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'))
}
validator.isValidCodeArgument(code)
this.code = code

@@ -231,11 +68,3 @@ 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'))
}
validator.isValidTypeArgument(type)
this.type = type

@@ -246,11 +75,3 @@ 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'))
}
validator.isValidMessageArgument(message)
this.message = message

@@ -260,29 +81,13 @@ 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) {
setState(newState) {
validator.isValidNewStateArgument(newState)
switch (typeof newState) {
case 'object': {
this.state = state
this.state = newState
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
const state = newState(this.state)
validator.isValidNewStateCallback(state)
this.state = state
break

@@ -289,0 +94,0 @@ }

{
"name": "@darkwolf/code-error",
"version": "2.0.4",
"version": "2.0.6",
"description": "Code Error Utility",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -61,3 +61,3 @@ # Code Error Utility

## Init
### new CodeError({namespace, code, type, message, state} || [code[, type?[, message]]])
### new CodeError(options[namespace, code, type, message, state] || [code[, type?[, message]]])
## Methods

@@ -69,1 +69,9 @@ ### setNamespace(namespace)

### setState(state || callback(state => newState))
## Error Codes
#### invalid-options
#### invalid-namespace
#### invalid-code
#### invalid-type
#### invalid-message
#### invalid-state
#### invalid-new-state

@@ -9,16 +9,11 @@ const CodeError = require('../')

switch (args.length) {
case 2: {
const [code, message] = args
options.code = code
options.message = message
break
}
case 3: {
const [code, type, message] = args
options.code = code
options.type = type
options.message = message
break
}
if (args.length <= 2) {
const [code, message] = args
options.code = code
options.message = message
} else {
const [code, type, message] = args
options.code = code
options.type = type
options.message = message
}

@@ -25,0 +20,0 @@

const CodeErrorError = require('./CodeErrorError')
const INVALID_OPTIONS = new CodeErrorError('invalid-options', 'Invalid options')
const INVALID_NAMESPACE = new CodeErrorError('invalid-namespace', 'Invalid namespace')
const INVALID_CODE = new CodeErrorError('invalid-code', 'Invalid code')
const INVALID_TYPE = new CodeErrorError('invalid-type', 'Invalid type')
const INVALID_MESSAGE = new CodeErrorError('invalid-message', 'Invalid message')
const INVALID_STATE = new CodeErrorError('invalid-state', 'Invalid state')
const OPTIONS_ARGUMENT_REQUIRED = new CodeErrorError('invalid-options', 'argument-required', '"options" argument is required')
const OPTIONS_INVALID_ARGUMENT_TYPE = new CodeErrorError('invalid-options', 'invalid-argument-type', [
'Invalid argument type "options"',
'"options" must be an object'
].join('\n'))
const NAMESPACE_INVALID_PROPERTY_TYPE = new CodeErrorError('invalid-namespace', 'invalid-property-type', [
'Invalid property type "namespace"',
'"namespace" must be a string'
].join('\n'))
const NAMESPACE_INVALID_PROPERTY_VALUE = new CodeErrorError('invalid-namespace', 'invalid-property-value', [
'Invalid property value "namespace"',
'"namespace" must have a length'
].join('\n'))
const CODE_PROPERTY_REQUIRED = new CodeErrorError('invalid-code', 'property-required', '"code" property is required')
const CODE_INVALID_PROPERTY_TYPE = new CodeErrorError('invalid-code', 'invalid-property-type', [
'Invalid property type "code"',
'"code" must be a string'
].join('\n'))
const CODE_INVALID_PROPERTY_VALUE = new CodeErrorError('invalid-code', 'invalid-property-value', [
'Invalid property value "code"',
'"code" must have a length'
].join('\n'))
const TYPE_INVALID_PROPERTY_TYPE = new CodeErrorError('invalid-type', 'invalid-property-type', [
'Invalid property type "type"',
'"type" must be a string'
].join('\n'))
const TYPE_INVALID_PROPERTY_VALUE = new CodeErrorError('invalid-type', 'invalid-property-value', [
'Invalid property value "type"',
'"type" must have a length'
].join('\n'))
const MESSAGE_PROPERTY_REQUIRED = new CodeErrorError('invalid-message', 'property-required', '"message" property is required')
const MESSAGE_INVALID_PROPERTY_TYPE = new CodeErrorError('invalid-message', 'invalid-property-type', [
'Invalid property type "message"',
'"message" must be a string'
].join('\n'))
const MESSAGE_INVALID_PROPERTY_VALUE = new CodeErrorError('invalid-message', 'invalid-property-value', [
'Invalid property value "message"',
'"message" must have a length'
].join('\n'))
const STATE_INVALID_PROPERTY_TYPE = new CodeErrorError('invalid-state', 'invalid-property-type', [
'Invalid property type "state"',
'"state" must be an object'
].join('\n'))
const NAMESPACE_ARGUMENT_REQUIRED = new CodeErrorError('invalid-namespace', 'argument-required', '"namespace" argument is required')
const NAMESPACE_INVALID_ARGUMENT_TYPE = new CodeErrorError('invalid-namespace', 'invalid-argument-type', [
'Invalid argument type "namespace"',
'"namespace" must be a string'
].join('\n'))
const NAMESPACE_INVALID_ARGUMENT_VALUE = new CodeErrorError('invalid-namespace', 'invalid-argument-value', [
'Invalid argument value "namespace"',
'"namespace" must have a length'
].join('\n'))
const CODE_ARGUMENT_REQUIRED = new CodeErrorError('invalid-code', 'argument-required', '"code" argument is required')
const CODE_INVALID_ARGUMENT_TYPE = new CodeErrorError('invalid-code', 'invalid-argument-type', [
'Invalid argument type "code"',
'"code" must be a string'
].join('\n'))
const CODE_INVALID_ARGUMENT_VALUE = new CodeErrorError('invalid-code', 'invalid-argument-value', [
'Invalid argument value "code"',
'"code" must have a length'
].join('\n'))
const TYPE_ARGUMENT_REQUIRED = new CodeErrorError('invalid-type', 'argument-required', '"type" argument is required')
const TYPE_INVALID_ARGUMENT_TYPE = new CodeErrorError('invalid-type', 'invalid-argument-type', [
'Invalid argument type "type"',
'"type" must be a string'
].join('\n'))
const TYPE_INVALID_ARGUMENT_VALUE = new CodeErrorError('invalid-type', 'invalid-argument-value', [
'Invalid argument value "type"',
'"type" must have a length'
].join('\n'))
const MESSAGE_ARGUMENT_REQUIRED = new CodeErrorError('invalid-message', 'argument-required', '"message" argument is required')
const MESSAGE_INVALID_ARGUMENT_TYPE = new CodeErrorError('invalid-message', 'invalid-argument-type', [
'Invalid argument type "message"',
'"message" must be a string'
].join('\n'))
const MESSAGE_INVALID_ARGUMENT_VALUE = new CodeErrorError('invalid-message', 'invalid-argument-value', [
'Invalid argument value "message"',
'"message" must have a length'
].join('\n'))
const NEW_STATE_ARGUMENT_REQUIRED = new CodeErrorError('invalid-new-state', 'argument-required', '"newState" argument is required')
const NEW_STATE_INVALID_ARGUMENT_TYPE = new CodeErrorError('invalid-new-state', 'invalid-argument-type', [
'Invalid argument type "newState"',
'"newState" must be an object or a callback function'
].join('\n'))
const NEW_STATE_INVALID_RETURN_TYPE = new CodeErrorError('invalid-new-state', 'invalid-return-type', [
'Invalid return type by callback function "newState"',
'"newState" must return an object'
].join('\n'))
module.exports = {
INVALID_OPTIONS,
INVALID_NAMESPACE,
INVALID_CODE,
INVALID_TYPE,
INVALID_MESSAGE,
INVALID_STATE
OPTIONS_ARGUMENT_REQUIRED,
OPTIONS_INVALID_ARGUMENT_TYPE,
NAMESPACE_INVALID_PROPERTY_TYPE,
NAMESPACE_INVALID_PROPERTY_VALUE,
CODE_PROPERTY_REQUIRED,
CODE_INVALID_PROPERTY_TYPE,
CODE_INVALID_PROPERTY_VALUE,
TYPE_INVALID_PROPERTY_TYPE,
TYPE_INVALID_PROPERTY_VALUE,
MESSAGE_PROPERTY_REQUIRED,
MESSAGE_INVALID_PROPERTY_TYPE,
MESSAGE_INVALID_PROPERTY_VALUE,
STATE_INVALID_PROPERTY_TYPE,
NAMESPACE_ARGUMENT_REQUIRED,
NAMESPACE_INVALID_ARGUMENT_TYPE,
NAMESPACE_INVALID_ARGUMENT_VALUE,
CODE_ARGUMENT_REQUIRED,
CODE_INVALID_ARGUMENT_TYPE,
CODE_INVALID_ARGUMENT_VALUE,
TYPE_ARGUMENT_REQUIRED,
TYPE_INVALID_ARGUMENT_TYPE,
TYPE_INVALID_ARGUMENT_VALUE,
MESSAGE_ARGUMENT_REQUIRED,
MESSAGE_INVALID_ARGUMENT_TYPE,
MESSAGE_INVALID_ARGUMENT_VALUE,
NEW_STATE_ARGUMENT_REQUIRED,
NEW_STATE_INVALID_ARGUMENT_TYPE,
NEW_STATE_INVALID_RETURN_TYPE
}
const CodeErrorError = require('./CodeErrorError')
const validator = require('./validator')
const errors = require('./errors')

@@ -6,3 +7,4 @@

CodeErrorError,
validator,
errors
}
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