Comparing version 5.0.1 to 5.1.0
@@ -6,4 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defekt = void 0; | ||
const humanize_string_1 = __importDefault(require("humanize-string")); | ||
const defekt = function (errorDefinitions) { | ||
var _a; | ||
const errors = {}; | ||
@@ -14,14 +16,16 @@ /* eslint-disable guard-for-in */ | ||
const { code = `E${errorName.toUpperCase()}` } = errorDefinition; | ||
errors[errorName] = class extends Error { | ||
/* eslint-disable default-param-last */ | ||
constructor(message = `${humanize_string_1.default(errorName)}.`, { cause, data } = {}) { | ||
/* eslint-enable default-param-last */ | ||
super(); | ||
this.name = errorName; | ||
this.code = code; | ||
this.message = message; | ||
this.cause = cause; | ||
this.data = data; | ||
} | ||
}; | ||
errors[errorName] = (_a = class extends Error { | ||
/* eslint-disable default-param-last */ | ||
constructor(message = `${humanize_string_1.default(errorName)}.`, { cause, data } = {}) { | ||
/* eslint-enable default-param-last */ | ||
super(); | ||
this.name = errorName; | ||
this.code = code; | ||
this.message = message; | ||
this.cause = cause; | ||
this.data = data; | ||
} | ||
}, | ||
_a.code = code, | ||
_a); | ||
} | ||
@@ -28,0 +32,0 @@ /* eslint-enable guard-for-in */ |
import { CustomError } from './CustomError'; | ||
export declare type ErrorConstructors<TError> = { | ||
[TKey in keyof TError]: new (message?: string, { cause, data }?: { | ||
interface ErrorConstructor { | ||
new (message?: string, { cause, data }?: { | ||
cause?: Error; | ||
data?: any; | ||
}) => CustomError; | ||
}): CustomError; | ||
code: string; | ||
} | ||
export declare type ErrorConstructors<TError> = { | ||
[TKey in keyof TError]: ErrorConstructor; | ||
}; | ||
export {}; |
@@ -0,1 +1,8 @@ | ||
# [5.1.0](https://github.com/thenativeweb/defekt/compare/5.0.1...5.1.0) (2020-07-13) | ||
### Features | ||
* Add static error code to error constructors. ([#148](https://github.com/thenativeweb/defekt/issues/148)) ([b20351a](https://github.com/thenativeweb/defekt/commit/b20351a0707d1b0ec3ad9c4b6b283ee8a1382a46)) | ||
## [5.0.1](https://github.com/thenativeweb/defekt/compare/5.0.0...5.0.1) (2020-02-03) | ||
@@ -2,0 +9,0 @@ |
@@ -27,2 +27,4 @@ import { CustomError } from './CustomError'; | ||
public static code: string = code; | ||
/* eslint-disable default-param-last */ | ||
@@ -29,0 +31,0 @@ public constructor (message = `${humanizeString(errorName)}.`, { |
import { CustomError } from './CustomError'; | ||
export type ErrorConstructors<TError> = { | ||
[TKey in keyof TError]: new(message?: string, { cause, data }?: { | ||
interface ErrorConstructor { | ||
new(message?: string, { cause, data }?: { | ||
cause?: Error; | ||
data?: any; | ||
}) => CustomError | ||
}): CustomError; | ||
code: string; | ||
} | ||
export type ErrorConstructors<TError> = { | ||
[TKey in keyof TError]: ErrorConstructor | ||
}; |
{ | ||
"name": "defekt", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"description": "defekt is custom errors made simple.", | ||
@@ -29,5 +29,5 @@ "contributors": [ | ||
"devDependencies": { | ||
"assertthat": "5.1.0", | ||
"roboter": "11.0.12", | ||
"semantic-release-configuration": "1.0.16" | ||
"assertthat": "5.1.1", | ||
"roboter": "11.2.6", | ||
"semantic-release-configuration": "1.0.20" | ||
}, | ||
@@ -34,0 +34,0 @@ "repository": { |
@@ -121,2 +121,42 @@ # defekt | ||
### Comparing errors | ||
To tell your errors apart, you sometimes want to compare them – usually based on their error code. To prevent you from using magic strings all over your application, you can use the static `code` property on the error constructors: | ||
```javascript | ||
const errors = defekt({ | ||
ArgumentNull: {}, | ||
InvalidOperation: {} | ||
// ... | ||
}); | ||
const error = new errors.InvalidOperation(); | ||
if (error.code === errors.InvalidOperation.code) { | ||
// ... | ||
} | ||
``` | ||
Alternatively, if you catch an error from somewhere and there are multiple options, you can use the error codes in a `switch` statement: | ||
```javascript | ||
try { | ||
// ... | ||
} catch (ex) { | ||
switch (ex.code) { | ||
case errors.ArgumentNull.code: { | ||
// ... | ||
break; | ||
} | ||
case errors.InvalidOperation.code: { | ||
// ... | ||
break; | ||
} | ||
default: { | ||
throw ex; | ||
} | ||
} | ||
} | ||
``` | ||
## Running the build | ||
@@ -123,0 +163,0 @@ |
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
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
11749
141
168