Comparing version
@@ -15,4 +15,11 @@ import ExtendableError from 'es6-error'; | ||
protected _metadata: Record<string, any>; | ||
protected _logLevel: string | number; | ||
constructor(message: string); | ||
/** | ||
* Assign a log level to the error. Useful if you want to | ||
* determine which log level to use when logging the error. | ||
* @param {string|number} logLevel | ||
*/ | ||
withLogLevel(logLevel: string | number): this; | ||
/** | ||
* Set an error id used to link back to the specific error | ||
@@ -38,2 +45,6 @@ * @param errId | ||
/** | ||
* Gets the log level assigned to the error | ||
*/ | ||
getLogLevel(): string | number; | ||
/** | ||
* Get the instance-specific error id | ||
@@ -40,0 +51,0 @@ */ |
@@ -19,2 +19,11 @@ "use strict"; | ||
/** | ||
* Assign a log level to the error. Useful if you want to | ||
* determine which log level to use when logging the error. | ||
* @param {string|number} logLevel | ||
*/ | ||
withLogLevel(logLevel) { | ||
this._logLevel = logLevel; | ||
return this; | ||
} | ||
/** | ||
* Set an error id used to link back to the specific error | ||
@@ -52,2 +61,8 @@ * @param errId | ||
/** | ||
* Gets the log level assigned to the error | ||
*/ | ||
getLogLevel() { | ||
return this._logLevel; | ||
} | ||
/** | ||
* Get the instance-specific error id | ||
@@ -54,0 +69,0 @@ */ |
@@ -15,2 +15,5 @@ "use strict"; | ||
} | ||
if (highLevelErrorDef.logLevel) { | ||
this.withLogLevel(highLevelErrorDef.logLevel); | ||
} | ||
if (lowLevelErrorDef.statusCode) { | ||
@@ -25,2 +28,5 @@ this.withStatusCode(lowLevelErrorDef.statusCode); | ||
} | ||
if (lowLevelErrorDef.logLevel) { | ||
this.withLogLevel(lowLevelErrorDef.logLevel); | ||
} | ||
} | ||
@@ -27,0 +33,0 @@ } |
@@ -15,2 +15,9 @@ /** | ||
statusCode?: string | number; | ||
/** | ||
* A log level to associate with this error type since not all errors | ||
* may be considered an 'error' log level type when combined with | ||
* a logger. Used as the default if a Low Level Error log level | ||
* is not defined. | ||
*/ | ||
logLevel?: string | number; | ||
} | ||
@@ -45,2 +52,8 @@ /** | ||
statusCode?: string | number; | ||
/** | ||
* A log level to associate with this error type since not all errors | ||
* may be considered an 'error' log level type when combined with | ||
* a logger. | ||
*/ | ||
logLevel?: string | number; | ||
} | ||
@@ -75,2 +88,7 @@ /** | ||
/** | ||
* Gets the log level assigned to the error. If a low level code | ||
* has a log level defined, it will be used over the high level one. | ||
*/ | ||
getLogLevel(): string | number; | ||
/** | ||
* Returns the high level error code | ||
@@ -147,2 +165,8 @@ */ | ||
/** | ||
* Assign a log level to the error. Useful if you want to | ||
* determine which log level to use when logging the error. | ||
* @param {string|number} logLevel | ||
*/ | ||
withLogLevel(logLevel: string | number): this; | ||
/** | ||
* Returns a json representation of the error. Assume the data | ||
@@ -149,0 +173,0 @@ * contained is for internal purposes only as it contains the stack trace. |
@@ -0,1 +1,9 @@ | ||
## 1.0.13 - Sat Jun 20 2020 03:22:14 | ||
**Contributor:** Theo Gravity | ||
- Update README.md [skip ci] | ||
Add another example for log level. | ||
## 1.0.12 - Wed Jun 03 2020 03:54:55 | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "new-error", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "A production-grade error creation and serialization library designed for Typescript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -32,2 +32,3 @@ # new-error | ||
- [Example Express Integration](#example-express-integration) | ||
- [Working with log levels](#working-with-log-levels) | ||
- [Error Registry API](#error-registry-api) | ||
@@ -109,5 +110,12 @@ - [Creating errors](#creating-errors) | ||
* (optional) Protocol-specific status code, such as an HTTP status code. Used as the | ||
* default if a Low Level Error status code is not specified or defined. | ||
* default if a Low Level Error status code is not defined. | ||
*/ | ||
statusCode: 500 | ||
statusCode: 500, | ||
/** | ||
* (optional) Log level string / number to associate with this error. | ||
* Useful if you want to use your logging system to log the error but | ||
* assign a different log level for it. Used as the default if a | ||
* Low Level log level is not defined. | ||
*/ | ||
logLevel: 'error' | ||
} | ||
@@ -135,3 +143,9 @@ } | ||
*/ | ||
statusCode: 500 | ||
statusCode: 500, | ||
/** | ||
* (optional) Log level string / number to associate with this error. | ||
* Useful if you want to use your logging system to log the error but | ||
* assign a different log level for it. | ||
*/ | ||
logLevel: 'error' | ||
} | ||
@@ -206,3 +220,4 @@ } | ||
subCode: 'DB_0001', | ||
statusCode: 500 | ||
statusCode: 500, | ||
logLevel: 'error' | ||
}) | ||
@@ -230,2 +245,3 @@ | ||
.withStatusCode(500) | ||
.withLogLevel('error') | ||
@@ -320,3 +336,42 @@ console.log(err.formatMessage('SQL_1234').toJSON()) | ||
# Working with log levels | ||
You might want to use a different log level when logging common errors, such as validation errors. | ||
```typescript | ||
import { ErrorRegistry } from 'new-error' | ||
const errors = { | ||
VALIDATION_ERROR: { | ||
className: 'ValidationError', | ||
code: 'VALIDATION_ERROR', | ||
statusCode: 400, | ||
// probably don't want to log every validation error | ||
// in production since these errors tend to happen frequently | ||
// and would pollute the logs | ||
logLevel: 'debug' | ||
} | ||
} | ||
const errorCodes = { | ||
MISSING_FORM_FIELDS: { | ||
message: 'Form submission data is missing fields', | ||
subCode: 'MISSING_FORM_FIELDS', | ||
statusCode: 400 | ||
} | ||
} | ||
const errRegistry = new ErrorRegistry(errors, errorCodes) | ||
// some part of the application throws the error | ||
const err = errRegistry.newError('VALIDATION_ERROR', 'MISSING_FORM_FIELDS') | ||
// another part of the application catches the error | ||
if (err.getLogLevel() === 'debug') { | ||
console.debug(JSON.stringify(err.toJSON(), null, 2)) | ||
} else { | ||
console.error(JSON.stringify(err.toJSON(), null, 2)) | ||
} | ||
``` | ||
# Error Registry API | ||
@@ -404,2 +459,3 @@ | ||
- `BaseError#getSafeMetadata()` | ||
- `BaseError#getLogLevel()` | ||
@@ -414,2 +470,3 @@ ## Basic setters | ||
- `BaseError#withErrorSubCode(code: string | number): this` | ||
- `BaseError#withLogLevel(level: string | number): this` | ||
@@ -416,0 +473,0 @@ ## Set an error id |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
57074
7.83%778
7.76%647
9.66%