Comparing version 2.1.10 to 2.1.11
@@ -20,2 +20,3 @@ import ExtendableError from 'es6-error'; | ||
protected _onConvert: ConvertFn | null; | ||
protected _appendedWithErrorMsg: boolean; | ||
constructor(message: string, config?: IBaseErrorConfig); | ||
@@ -108,2 +109,7 @@ /** | ||
/** | ||
* Appends the caused by message to the main error message if | ||
* the appendWithErrorMessageFormat config item is defined. | ||
*/ | ||
protected appendCausedByMessage(): void; | ||
/** | ||
* Adds metadata that will be included with toJSON() serialization. | ||
@@ -110,0 +116,0 @@ * Multiple calls will append and not replace. |
@@ -21,2 +21,3 @@ "use strict"; | ||
this._onConvert = this._config.onConvert || null; | ||
this._appendedWithErrorMsg = false; | ||
} | ||
@@ -144,2 +145,3 @@ /** | ||
this.message = sprintf_js_1.vsprintf(this.message, args); | ||
this.appendCausedByMessage(); | ||
return this; | ||
@@ -153,5 +155,21 @@ } | ||
this._causedBy = error; | ||
this.appendCausedByMessage(); | ||
return this; | ||
} | ||
/** | ||
* Appends the caused by message to the main error message if | ||
* the appendWithErrorMessageFormat config item is defined. | ||
*/ | ||
appendCausedByMessage() { | ||
var _a; | ||
if (!this._appendedWithErrorMsg && | ||
((_a = this._causedBy) === null || _a === void 0 ? void 0 : _a.message) && | ||
this._config.appendWithErrorMessageFormat) { | ||
this.message = | ||
this.message + | ||
sprintf_js_1.vsprintf(this._config.appendWithErrorMessageFormat, this._causedBy.message); | ||
this._appendedWithErrorMsg = true; | ||
} | ||
} | ||
/** | ||
* Adds metadata that will be included with toJSON() serialization. | ||
@@ -158,0 +176,0 @@ * Multiple calls will append and not replace. |
@@ -274,2 +274,10 @@ /** | ||
onConvert?: ConvertFn; | ||
/** | ||
* If defined, will append the `.message` value when calling causedBy() after the main error message. | ||
* Useful for test frameworks like Jest where it will not print the caused by message. | ||
* To define the format of the appended message, use '%s' for the message value. | ||
* | ||
* See readme for examples. | ||
*/ | ||
appendWithErrorMessageFormat?: string; | ||
} | ||
@@ -276,0 +284,0 @@ /** |
@@ -0,1 +1,11 @@ | ||
## 2.1.11 - Fri Aug 13 2021 03:59:06 | ||
**Contributor:** Theo Gravity | ||
- [minor] Add config option to append attached error message to main error message (#21) | ||
Added a `BaseError` config option, `appendWithErrorMessageFormat`, which | ||
will append the attached error message to the main error message. Useful | ||
for testing frameworks like Jest, which will not print the attached message. | ||
## 2.1.10 - Fri May 28 2021 00:34:25 | ||
@@ -2,0 +12,0 @@ |
{ | ||
"name": "new-error", | ||
"version": "2.1.10", | ||
"version": "2.1.11", | ||
"description": "A production-grade error creation and serialization library designed for Typescript", | ||
@@ -67,22 +67,22 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"@theo.gravity/changelog-version": "2.1.10", | ||
"@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.6.3", | ||
"jest-cli": "26.6.3", | ||
"@theo.gravity/changelog-version": "2.1.11", | ||
"@theo.gravity/version-bump": "2.0.14", | ||
"@types/jest": "27.0.1", | ||
"@types/node": "^16.6.1", | ||
"@typescript-eslint/eslint-plugin": "^4.29.1", | ||
"@typescript-eslint/parser": "^4.29.1", | ||
"eslint": "7.32.0", | ||
"git-commit-stamper": "^1.0.10", | ||
"jest": "^27.0.6", | ||
"jest-cli": "27.0.6", | ||
"jest-junit-reporter": "1.1.0", | ||
"lint-staged": "10.5.4", | ||
"lint-staged": "11.1.2", | ||
"pre-commit": "1.2.2", | ||
"prettier-standard": "16.4.1", | ||
"prettier-standard": "^16.4.1", | ||
"standardx": "^7.0.0", | ||
"toc-md-alt": "^0.4.1", | ||
"ts-jest": "26.5.3", | ||
"ts-node": "9.1.1", | ||
"ts-node-dev": "1.1.6", | ||
"typescript": "4.2.3" | ||
"toc-md-alt": "^0.4.2", | ||
"ts-jest": "27.0.4", | ||
"ts-node": "10.2.0", | ||
"ts-node-dev": "^1.1.8", | ||
"typescript": "4.3.5" | ||
}, | ||
@@ -89,0 +89,0 @@ "eslintConfig": { |
@@ -65,2 +65,3 @@ # new-error | ||
- [Attaching errors](#attaching-errors) | ||
- [Append the attached error message to the main error message](#append-the-attached-error-message-to-the-main-error-message) | ||
- [Format messages](#format-messages) | ||
@@ -792,2 +793,10 @@ - [Converting the error into another type](#converting-the-error-into-another-type) | ||
onConvert?: <E extends BaseError = BaseError>(err: E) => any | ||
/** | ||
* If defined, will append the `.message` value when calling causedBy() after the main error message. | ||
* Useful for frameworks like Jest where it will not print the caused by data. | ||
* To define the format of the appended message, use '%s' for the message value. | ||
* | ||
* Ex: ", caused by: %s" | ||
*/ | ||
appendWithErrorMessageFormat?: string | ||
} | ||
@@ -866,2 +875,53 @@ ``` | ||
### Append the attached error message to the main error message | ||
If the config option `appendWithErrorMessageFormat` is defined, and the error sent into `causedBy` | ||
contains a `message` property, then the caused by error message will be appended to the main error message. | ||
Useful if you find yourself applying this pattern to expose the attached error message: | ||
```typescript | ||
const thrownErrorFromApp = new Error('Duplicate key error') | ||
const err = new BaseError('Internal server error: %s'); | ||
err.causedBy(thrownErrorFromApp) | ||
err.formatMessage(thrownErrorFromApp.message); | ||
``` | ||
This is also useful for test frameworks like `jest` where it will only print out the main error message | ||
and not any properties attached to the error. | ||
```typescript | ||
// only enable for testing envs | ||
const IS_TEST_ENV = process.env.NODE_ENV === 'test'; | ||
const err = new BaseError('Internal server error', { | ||
// %s is the attached error message | ||
appendWithErrorMessageFormat: IS_TEST_ENV ? ': %s' : null | ||
}) | ||
err.causedBy(new Error('Duplicate key')) | ||
// prints out "Internal server error: Duplicate key" | ||
console.log(err.message) | ||
``` | ||
```typescript | ||
// formatted messages also work with this | ||
const IS_TEST_ENV = process.env.NODE_ENV === 'test'; | ||
const err = new BaseError('Internal server error: %s', { | ||
appendWithErrorMessageFormat: IS_TEST_ENV ? '===> %s' : null | ||
}) | ||
// formatMessage / causedBy can be called in any order | ||
err.formatMessage('Hello') | ||
err.causedBy(new Error('Duplicate key')) | ||
// prints out "Internal server error: Hello ===> Duplicate key" | ||
console.log(err.message) | ||
``` | ||
**It is not recommended that `appendWithErrorMessageFormat` is defined in a production environment | ||
as the `causedBy` error messages tend to be system-level messages that could be exposed to clients | ||
if the error is being thrown back to the client**. | ||
## Format messages | ||
@@ -868,0 +928,0 @@ |
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
119563
1380
1406