Comparing version 1.1.2 to 1.2.1
import ExtendableError from 'es6-error'; | ||
import { DeserializeOpts, IBaseError, SerializedError, SerializedErrorSafe } from '../interfaces'; | ||
import { DeserializeOpts, IBaseError, IBaseErrorConfig, SerializedError, SerializedErrorSafe } from '../interfaces'; | ||
/** | ||
@@ -16,3 +16,4 @@ * Improved error class. | ||
protected _logLevel: string | number; | ||
constructor(message: string); | ||
protected _config: IBaseErrorConfig; | ||
constructor(message: string, config?: IBaseErrorConfig); | ||
/** | ||
@@ -85,2 +86,10 @@ * Assign a log level to the error. Useful if you want to | ||
/** | ||
* Gets the error config | ||
*/ | ||
getConfig(): IBaseErrorConfig; | ||
/** | ||
* Sets the error config | ||
*/ | ||
setConfig(config: IBaseErrorConfig): void; | ||
/** | ||
* Replaces sprintf() flags in an error message, if present. | ||
@@ -87,0 +96,0 @@ * @see https://www.npmjs.com/package/sprintf-js |
@@ -13,6 +13,7 @@ "use strict"; | ||
class BaseError extends es6_error_1.default { | ||
constructor(message) { | ||
constructor(message, config = {}) { | ||
super(message); | ||
this._safeMetadata = {}; | ||
this._metadata = {}; | ||
this._config = config || {}; | ||
} | ||
@@ -121,2 +122,14 @@ /** | ||
/** | ||
* Gets the error config | ||
*/ | ||
getConfig() { | ||
return this._config; | ||
} | ||
/** | ||
* Sets the error config | ||
*/ | ||
setConfig(config) { | ||
this._config = config; | ||
} | ||
/** | ||
* Replaces sprintf() flags in an error message, if present. | ||
@@ -178,2 +191,3 @@ * @see https://www.npmjs.com/package/sprintf-js | ||
toJSON(fieldsToOmit = []) { | ||
var _a; | ||
const data = { | ||
@@ -204,2 +218,5 @@ errId: this._errId, | ||
}); | ||
(_a = this._config.toJSONFieldsToOmit) === null || _a === void 0 ? void 0 : _a.forEach(item => { | ||
delete data[item]; | ||
}); | ||
return data; | ||
@@ -213,2 +230,3 @@ } | ||
toJSONSafe(fieldsToOmit = []) { | ||
var _a; | ||
const data = { | ||
@@ -232,2 +250,5 @@ errId: this._errId, | ||
}); | ||
(_a = this._config.toJSONSafeFieldsToOmit) === null || _a === void 0 ? void 0 : _a.forEach(item => { | ||
delete data[item]; | ||
}); | ||
return data; | ||
@@ -296,5 +317,5 @@ } | ||
if (typeof data !== 'object') { | ||
throw new Error(`fromJSON(): Data is not an object.`); | ||
throw new Error('fromJSON(): Data is not an object.'); | ||
} | ||
let err = new this(data.message); | ||
const err = new this(data.message); | ||
this.copyDeserializationData(err, data, opts); | ||
@@ -301,0 +322,0 @@ return err; |
@@ -1,3 +0,3 @@ | ||
import { BaseError, DeserializeOpts, IBaseError, SerializedError } from '../index'; | ||
import { HighLevelError, LowLevelErrorInternal } from '../interfaces'; | ||
import { HighLevelError, IBaseErrorConfig, LowLevelErrorInternal, IBaseError, SerializedError, DeserializeOpts } from '../interfaces'; | ||
import { BaseError } from './BaseError'; | ||
/** | ||
@@ -7,3 +7,3 @@ * Improved error class generated by the ErrorRegistry. | ||
export declare class BaseRegistryError extends BaseError { | ||
constructor(highLevelErrorDef: HighLevelError, lowLevelErrorDef: LowLevelErrorInternal); | ||
constructor(highLevelErrorDef: HighLevelError, lowLevelErrorDef: LowLevelErrorInternal, config?: IBaseErrorConfig); | ||
/** | ||
@@ -10,0 +10,0 @@ * Deserializes an error into an instance |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseRegistryError = void 0; | ||
const index_1 = require("../index"); | ||
const BaseError_1 = require("./BaseError"); | ||
/** | ||
* Improved error class generated by the ErrorRegistry. | ||
**/ | ||
class BaseRegistryError extends index_1.BaseError { | ||
constructor(highLevelErrorDef, lowLevelErrorDef) { | ||
super(lowLevelErrorDef.message); | ||
class BaseRegistryError extends BaseError_1.BaseError { | ||
constructor(highLevelErrorDef, lowLevelErrorDef, config = {}) { | ||
super(lowLevelErrorDef.message, config); | ||
this.withErrorCode(highLevelErrorDef.code); | ||
@@ -42,5 +42,5 @@ if (highLevelErrorDef.statusCode) { | ||
if (typeof data !== 'object') { | ||
throw new Error(`fromJSON(): Data is not an object.`); | ||
throw new Error('fromJSON(): Data is not an object.'); | ||
} | ||
let err = new this({ | ||
const err = new this({ | ||
code: data.code | ||
@@ -47,0 +47,0 @@ }, { |
@@ -1,2 +0,2 @@ | ||
import { DeserializeOpts, HighLevelErrorInternal, IBaseError, LowLevelErrorInternal, SerializedError } from './interfaces'; | ||
import { DeserializeOpts, HighLevelErrorInternal, IBaseError, IErrorRegistryConfig, LowLevelErrorInternal, SerializedError } from './interfaces'; | ||
import { BaseRegistryError } from './error-types/BaseRegistryError'; | ||
@@ -25,4 +25,8 @@ /** | ||
protected lowLevelErrors: LLError; | ||
constructor(highLvErrors: HLError, lowLvErrors: LLErrorName); | ||
/** | ||
* Error registry configuration | ||
\ */ | ||
protected _config: IErrorRegistryConfig; | ||
constructor(highLvErrors: HLError, lowLvErrors: LLErrorName, config?: IErrorRegistryConfig); | ||
/** | ||
* Gets the definition of a High Level Error | ||
@@ -29,0 +33,0 @@ * @param {string} highLvErrName |
@@ -11,3 +11,3 @@ "use strict"; | ||
class ErrorRegistry { | ||
constructor(highLvErrors, lowLvErrors) { | ||
constructor(highLvErrors, lowLvErrors, config = {}) { | ||
this.highLevelErrors = highLvErrors; | ||
@@ -17,2 +17,3 @@ this.lowLevelErrors = {}; | ||
this.highLevelErrorClasses = {}; | ||
this._config = config; | ||
Object.keys(highLvErrors).forEach(name => { | ||
@@ -77,3 +78,3 @@ this.classNameHighLevelNameMap[highLvErrors[name].className] = name; | ||
message | ||
}); | ||
}, this._config.baseErrorConfig); | ||
} | ||
@@ -91,3 +92,3 @@ /** | ||
const C = this.getClass(highLvErrName); | ||
return new C(this.getHighLevelError(highLvErrName), this.getLowLevelError(lowLvErrName)); | ||
return new C(this.getHighLevelError(highLvErrName), this.getLowLevelError(lowLvErrName), this._config.baseErrorConfig); | ||
} | ||
@@ -94,0 +95,0 @@ /** |
@@ -0,1 +1,4 @@ | ||
/** | ||
* A High Level Error definition defined by the user | ||
*/ | ||
export interface HighLevelError { | ||
@@ -116,2 +119,10 @@ /** | ||
/** | ||
* Gets the configuration options | ||
*/ | ||
getConfig(): IBaseErrorConfig; | ||
/** | ||
* Sets configuration options | ||
*/ | ||
setConfig(config: IBaseErrorConfig): void; | ||
/** | ||
* Attach the original error that was thrown, if available | ||
@@ -190,2 +201,24 @@ * @param {Error} error | ||
/** | ||
* Configuration options for the BaseError class | ||
*/ | ||
export interface IBaseErrorConfig { | ||
/** | ||
* A list of fields to always omit when calling toJSON | ||
*/ | ||
toJSONFieldsToOmit?: string[]; | ||
/** | ||
* A list of fields to always omit when calling toJSONSafe | ||
*/ | ||
toJSONSafeFieldsToOmit?: string[]; | ||
} | ||
/** | ||
* Configuration options for the ErrorRegistry class | ||
*/ | ||
export interface IErrorRegistryConfig { | ||
/** | ||
* Options when creating a new BaseError | ||
*/ | ||
baseErrorConfig?: IBaseErrorConfig; | ||
} | ||
/** | ||
* Safe-version of a serialized error object that can be shown to a client / | ||
@@ -192,0 +225,0 @@ * end-user. |
"use strict"; | ||
/** | ||
* A High Level Error definition defined by the user | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=interfaces.js.map |
@@ -0,1 +1,9 @@ | ||
## 1.2.1 - Mon Mar 08 2021 20:33:41 | ||
**Contributor:** Theo Gravity | ||
- Add configuration options for ErrorRegistry / BaseError (#8) | ||
New configuration options have been added to the ErrorRegistry and BaseError. See readme for more details. | ||
## 1.1.2 - Mon Sep 21 2020 04:13:44 | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "new-error", | ||
"version": "1.1.2", | ||
"version": "1.2.1", | ||
"description": "A production-grade error creation and serialization library designed for Typescript", | ||
@@ -68,21 +68,21 @@ "main": "build/index.js", | ||
"@theo.gravity/changelog-version": "2.1.10", | ||
"@theo.gravity/version-bump": "2.0.10", | ||
"@types/jest": "26.0.14", | ||
"@types/node": "^14.11.1", | ||
"@typescript-eslint/eslint-plugin": "^4.1.1", | ||
"@typescript-eslint/parser": "^4.1.1", | ||
"eslint": "7.9.0", | ||
"@theo.gravity/version-bump": "2.0.12", | ||
"@types/jest": "26.0.20", | ||
"@types/node": "^14.14.32", | ||
"@typescript-eslint/eslint-plugin": "^4.17.0", | ||
"@typescript-eslint/parser": "^4.17.0", | ||
"eslint": "7.21.0", | ||
"git-commit-stamper": "^1.0.9", | ||
"jest": "^26.4.2", | ||
"jest-cli": "26.4.2", | ||
"jest": "^26.6.3", | ||
"jest-cli": "26.6.3", | ||
"jest-junit-reporter": "1.1.0", | ||
"lint-staged": "10.4.0", | ||
"lint-staged": "10.5.4", | ||
"pre-commit": "1.2.2", | ||
"prettier-standard": "16.4.1", | ||
"standardx": "^5.0.0", | ||
"standardx": "^7.0.0", | ||
"toc-md-alt": "^0.4.1", | ||
"ts-jest": "26.4.0", | ||
"ts-node": "9.0.0", | ||
"ts-node-dev": "1.0.0-pre.62", | ||
"typescript": "4.0.3" | ||
"ts-jest": "26.5.3", | ||
"ts-node": "9.1.1", | ||
"ts-node-dev": "1.1.6", | ||
"typescript": "4.2.3" | ||
}, | ||
@@ -89,0 +89,0 @@ "eslintConfig": { |
@@ -35,2 +35,4 @@ # new-error | ||
- [Error Registry API](#error-registry-api) | ||
- [Constructor](#constructor) | ||
- [Configuration options](#configuration-options) | ||
- [Creating errors](#creating-errors) | ||
@@ -43,2 +45,4 @@ - [Create a well-defined error](#create-a-well-defined-error) | ||
- [Error API](#error-api) | ||
- [Constructor](#constructor-1) | ||
- [Configuration options](#configuration-options-1) | ||
- [Getters](#getters) | ||
@@ -63,2 +67,3 @@ - [Basic setters](#basic-setters) | ||
- [Stand-alone instance-based deserialization](#stand-alone-instance-based-deserialization) | ||
- [Looking for production-grade env variable / configuration management?](#looking-for-production-grade-env-variable--configuration-management) | ||
@@ -387,2 +392,17 @@ <!-- TOC END --> | ||
## Constructor | ||
`new ErrorRegistry(highLvErrors, lowLvErrors, config = {})` | ||
### Configuration options | ||
```ts | ||
interface IErrorRegistryConfig { | ||
/** | ||
* Options when creating a new BaseError | ||
*/ | ||
baseErrorConfig?: IBaseErrorConfig | ||
} | ||
``` | ||
## Creating errors | ||
@@ -455,2 +475,23 @@ | ||
## Constructor | ||
`new BaseError(message: string, config: IBaseErrorConfig: IBaseErrorConfig = {})` | ||
- `message`: The error message you would use in `new Error(message)` | ||
### Configuration options | ||
```ts | ||
interface IBaseErrorConfig { | ||
/** | ||
* A list of fields to always omit when calling toJSON | ||
*/ | ||
toJSONFieldsToOmit?: string[] | ||
/** | ||
* A list of fields to always omit when calling toJSONSafe | ||
*/ | ||
toJSONSafeFieldsToOmit?: string[] | ||
} | ||
``` | ||
## Getters | ||
@@ -468,2 +509,3 @@ | ||
- `BaseError#getLogLevel()` | ||
- `BaseError#getConfig()` | ||
@@ -479,2 +521,3 @@ ## Basic setters | ||
- `BaseError#withLogLevel(level: string | number): this` | ||
- `BaseError#setConfig(config: IBaseErrorConfig): void` | ||
@@ -674,4 +717,3 @@ ## Static methods | ||
- The serialized output may or may not include the `name` property (if using `toJSONSafe()`) that would be able to | ||
hydrate it back into a specific error instance. | ||
- If the serialized output lacks the `name` property (not present when using `toJSONSafe()`), then only a `BaseError` instance can be returned. | ||
- The metadata is squashed in the serialized output that information is required to separate them. | ||
@@ -691,3 +733,3 @@ - It is difficult to determine the original type / structure of the `causedBy` data. As a result, it will be copied as-is. | ||
This method will attempt to deserialize into a registered error type. If it is unable to, a `BaseError` instance is | ||
This method will attempt to deserialize into a registered error type via the `name` property. If it is unable to, a `BaseError` instance is | ||
returned instead. | ||
@@ -743,2 +785,3 @@ | ||
'meta': { 'safeData': 'test454', 'test': 'test123' }, | ||
// maps to className in the high level error def | ||
'name': 'InternalServerError', | ||
@@ -820,1 +863,5 @@ 'statusCode': 500, | ||
``` | ||
# Looking for production-grade env variable / configuration management? | ||
Check out https://github.com/theogravity/configurity |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
76360
1023
857
19