| export interface CustomError extends Error { | ||
| name: string; | ||
| code: string; | ||
| message: string; | ||
| cause?: Error; | ||
| data?: any; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
| import { CustomError } from './CustomError'; | ||
| export declare type ErrorConstructors<TError> = { | ||
| [TKey in keyof TError]: new (message?: string, { cause, data }?: { | ||
| cause?: Error; | ||
| data?: any; | ||
| }) => CustomError; | ||
| }; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
| import { CustomError } from './CustomError'; | ||
| import defekt from './defekt'; | ||
| export default defekt; | ||
| export { CustomError }; |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const defekt_1 = __importDefault(require("./defekt")); | ||
| exports.default = defekt_1.default; |
| export interface CustomError extends Error { | ||
| name: string; | ||
| code: string; | ||
| message: string; | ||
| cause?: Error; | ||
| data?: any; | ||
| } |
| import { CustomError } from './CustomError'; | ||
| export type ErrorConstructors<TError> = { | ||
| [TKey in keyof TError]: new(message?: string, { cause, data }?: { | ||
| cause?: Error; | ||
| data?: any; | ||
| }) => CustomError | ||
| }; |
| import { CustomError } from './CustomError'; | ||
| import defekt from './defekt'; | ||
| export default defekt; | ||
| export { CustomError }; |
@@ -1,10 +0,2 @@ | ||
| export interface CustomError extends Error { | ||
| name: string; | ||
| code: string; | ||
| message: string; | ||
| cause?: Error; | ||
| } | ||
| declare type ErrorConstructors<TError> = { | ||
| [TKey in keyof TError]: new (message?: string, cause?: Error) => CustomError; | ||
| }; | ||
| import { ErrorConstructors } from './ErrorConstructors'; | ||
| declare const defekt: <TErrorDefinition extends { | ||
@@ -11,0 +3,0 @@ [key: string]: { |
@@ -12,8 +12,7 @@ "use strict"; | ||
| const errorDefinition = errorDefinitions[errorName]; | ||
| if (!errorDefinition) { | ||
| continue; | ||
| } | ||
| const { code = `E${errorName.toUpperCase()}` } = errorDefinition; | ||
| errors[errorName] = class extends Error { | ||
| constructor(message = `${humanize_string_1.default(errorName)}.`, cause) { | ||
| /* eslint-disable default-param-last */ | ||
| constructor(message = `${humanize_string_1.default(errorName)}.`, { cause, data } = {}) { | ||
| /* eslint-enable default-param-last */ | ||
| super(); | ||
@@ -24,2 +23,3 @@ this.name = errorName; | ||
| this.cause = cause; | ||
| this.data = data; | ||
| } | ||
@@ -26,0 +26,0 @@ }; |
+14
-16
@@ -0,14 +1,5 @@ | ||
| import { CustomError } from './CustomError'; | ||
| import { ErrorConstructors } from './ErrorConstructors'; | ||
| import humanizeString from 'humanize-string'; | ||
| export interface CustomError extends Error { | ||
| name: string; | ||
| code: string; | ||
| message: string; | ||
| cause?: Error; | ||
| } | ||
| type ErrorConstructors<TError> = { | ||
| [TKey in keyof TError]: new(message?: string, cause?: Error) => CustomError | ||
| }; | ||
| const defekt = function <TErrorDefinition extends { | ||
@@ -23,6 +14,2 @@ [ key: string ]: { code?: string }; | ||
| if (!errorDefinition) { | ||
| continue; | ||
| } | ||
| const { code = `E${errorName.toUpperCase()}` } = errorDefinition; | ||
@@ -39,3 +26,13 @@ | ||
| public constructor (message = `${humanizeString(errorName)}.`, cause?: Error) { | ||
| public data?: any; | ||
| /* eslint-disable default-param-last */ | ||
| public constructor (message = `${humanizeString(errorName)}.`, { | ||
| cause, | ||
| data | ||
| }: { | ||
| cause?: Error; | ||
| data?: any; | ||
| } = {}) { | ||
| /* eslint-enable default-param-last */ | ||
| super(); | ||
@@ -47,2 +44,3 @@ | ||
| this.cause = cause; | ||
| this.data = data; | ||
| } | ||
@@ -49,0 +47,0 @@ }; |
+5
-5
| { | ||
| "name": "defekt", | ||
| "version": "3.0.1", | ||
| "version": "4.0.0", | ||
| "description": "defekt is custom errors made simple.", | ||
@@ -23,4 +23,4 @@ "contributors": [ | ||
| ], | ||
| "main": "build/lib/defekt.js", | ||
| "types": "build/lib/defekt.d.ts", | ||
| "main": "build/lib/index.js", | ||
| "types": "build/lib/index.d.ts", | ||
| "dependencies": { | ||
@@ -30,4 +30,4 @@ "humanize-string": "2.1.0" | ||
| "devDependencies": { | ||
| "assertthat": "4.0.1", | ||
| "roboter": "7.1.5" | ||
| "assertthat": "4.0.2", | ||
| "roboter": "9.0.4" | ||
| }, | ||
@@ -34,0 +34,0 @@ "repository": { |
+12
-2
@@ -70,8 +70,18 @@ # defekt | ||
| Additionally, if an error was caused by another error, you can specify this error as the second parameter: | ||
| Additionally, if an error was caused by another error, you can specify this error using the `cause` property: | ||
| ```javascript | ||
| throw new errors.InvalidOperation('Something failed.', new Error(...)); | ||
| throw new errors.InvalidOperation('Something failed.', { | ||
| cause: new Error(...) | ||
| }); | ||
| ``` | ||
| From time to time, you may need to provide additional data for an error. For this, you can use the `data` property: | ||
| ```javascript | ||
| throw new errors.InvalidOperation('Something failed.', { | ||
| data: { ... } | ||
| }); | ||
| ``` | ||
| The custom errors follow the same rules as the built-in ones, i.e. they have a `name` and a `message` property, they derive from `Error` and they can be recognized by the `util.isError` function. | ||
@@ -78,0 +88,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
10215
18.52%17
112.5%138
39.39%109
10.1%0
-100%