Comparing version 1.0.4 to 1.0.5
@@ -8,4 +8,4 @@ import ExtendableError from 'es6-error'; | ||
protected _type: string; | ||
protected _code: string; | ||
protected _subCode: string; | ||
protected _code: string | number; | ||
protected _subCode: string | number; | ||
protected _statusCode: any; | ||
@@ -25,3 +25,3 @@ protected _causedBy: any; | ||
*/ | ||
protected withErrorCode(code: string): this; | ||
protected withErrorCode(code: string | number): this; | ||
/** | ||
@@ -31,4 +31,28 @@ * Set low level error code | ||
*/ | ||
protected withErrorSubCode(subCode: string): this; | ||
protected withErrorSubCode(subCode: string | number): this; | ||
/** | ||
* Returns the status code. | ||
*/ | ||
getStatusCode(): any; | ||
/** | ||
* Returns the high level error code | ||
*/ | ||
getCode(): string | number; | ||
/** | ||
* Returns the low level error code | ||
*/ | ||
getSubCode(): string | number; | ||
/** | ||
* Returns the attached error | ||
*/ | ||
getCausedBy(): any; | ||
/** | ||
* Returns metadata set by withMetadata() | ||
*/ | ||
getMetadata(): Record<string, any>; | ||
/** | ||
* Returns metadata set by withSafeMetadata() | ||
*/ | ||
getSafeMetadata(): Record<string, any>; | ||
/** | ||
* Replaces sprintf() flags in an error message, if present. | ||
@@ -35,0 +59,0 @@ * @see https://www.npmjs.com/package/sprintf-js |
@@ -43,2 +43,38 @@ "use strict"; | ||
/** | ||
* Returns the status code. | ||
*/ | ||
getStatusCode() { | ||
return this._statusCode; | ||
} | ||
/** | ||
* Returns the high level error code | ||
*/ | ||
getCode() { | ||
return this._code; | ||
} | ||
/** | ||
* Returns the low level error code | ||
*/ | ||
getSubCode() { | ||
return this._subCode; | ||
} | ||
/** | ||
* Returns the attached error | ||
*/ | ||
getCausedBy() { | ||
return this._causedBy; | ||
} | ||
/** | ||
* Returns metadata set by withMetadata() | ||
*/ | ||
getMetadata() { | ||
return this._metadata; | ||
} | ||
/** | ||
* Returns metadata set by withSafeMetadata() | ||
*/ | ||
getSafeMetadata() { | ||
return this._safeMetadata; | ||
} | ||
/** | ||
* Replaces sprintf() flags in an error message, if present. | ||
@@ -45,0 +81,0 @@ * @see https://www.npmjs.com/package/sprintf-js |
import { BaseError } from './error-types/BaseError'; | ||
import { ErrorRegistry } from './ErrorRegistry'; | ||
export { BaseError, ErrorRegistry }; | ||
import { IBaseError, SerializedError, SerializedErrorSafe } from './interfaces'; | ||
export { BaseError, ErrorRegistry, IBaseError, SerializedError, SerializedErrorSafe }; |
@@ -13,3 +13,3 @@ /** | ||
*/ | ||
code: any; | ||
code: string | number; | ||
/** | ||
@@ -19,3 +19,3 @@ * Protocol-specific status code, such as an HTTP status code. Used as the | ||
*/ | ||
statusCode?: any; | ||
statusCode?: string | number; | ||
} | ||
@@ -36,7 +36,7 @@ /** | ||
*/ | ||
subCode?: any; | ||
subCode?: string | number; | ||
/** | ||
* Protocol-specific status code, such as an HTTP status code. | ||
*/ | ||
statusCode?: any; | ||
statusCode?: string | number; | ||
} | ||
@@ -54,2 +54,26 @@ /** | ||
/** | ||
* Returns the high level error code | ||
*/ | ||
getCode(): string | number; | ||
/** | ||
* Returns the low level error code | ||
*/ | ||
getSubCode(): string | number; | ||
/** | ||
* Returns the status code. | ||
*/ | ||
getStatusCode(): string | number; | ||
/** | ||
* Returns the attached error | ||
*/ | ||
getCausedBy(): any; | ||
/** | ||
* Returns metadata set by withMetadata() | ||
*/ | ||
getMetadata(): Record<string, any>; | ||
/** | ||
* Returns metadata set by withSafeMetadata() | ||
*/ | ||
getSafeMetadata(): Record<string, any>; | ||
/** | ||
* Attach the original error that was thrown, if available | ||
@@ -104,7 +128,7 @@ * @param {Error} error | ||
*/ | ||
subCode?: any; | ||
subCode?: string | number; | ||
/** | ||
* Protocol-specific status code, such as an HTTP status code. | ||
*/ | ||
statusCode?: any; | ||
statusCode?: string | number; | ||
/** | ||
@@ -111,0 +135,0 @@ * User-defined metadata |
@@ -0,1 +1,7 @@ | ||
## 1.0.5 - Fri May 15 2020 20:57:38 | ||
**Contributor:** Theo Gravity | ||
- Add getters / more examples / improved docs | ||
## 1.0.4 - Fri May 15 2020 19:37:42 | ||
@@ -2,0 +8,0 @@ |
{ | ||
"name": "new-error", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A production-grade error creation and serialization library designed for Typescript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
123
README.md
@@ -30,4 +30,7 @@ # new-error | ||
- [Create an error without a low-level error](#create-an-error-without-a-low-level-error) | ||
- [Instance comparison / `instanceOf` usage](#instance-comparison--instanceof-usage) | ||
- [Error manipulation](#error-manipulation) | ||
- [`instanceOf` / comparisons](#instanceof--comparisons) | ||
- [Comparing a custom error](#comparing-a-custom-error) | ||
- [Native `instanceof`](#native-instanceof) | ||
- [Error API](#error-api) | ||
- [Getters](#getters) | ||
- [Attaching errors](#attaching-errors) | ||
@@ -38,5 +41,6 @@ - [Format messages](#format-messages) | ||
- [Internal metadata](#internal-metadata) | ||
- [Serializing errors](#serializing-errors) | ||
- [Safe serialization](#safe-serialization) | ||
- [Internal serialization](#internal-serialization) | ||
- [Serializing errors](#serializing-errors) | ||
- [Safe serialization](#safe-serialization) | ||
- [Internal serialization](#internal-serialization) | ||
- [Example Express Error Handling](#example-express-error-handling) | ||
@@ -113,3 +117,3 @@ <!-- TOC END --> | ||
// and type check will ensure that the values are valid | ||
const err = errRegistry.newError('INTERNAL_SERVER_ERROR', 'DATABASE_FAILURE') | ||
const err = errRegistry.newError('INTERNAL_SERVER_ERROR', 'DATABASE_FAILURE').formatMessage('SQL_1234') | ||
console.log(err.toJSON()) | ||
@@ -126,3 +130,3 @@ ``` | ||
code: 'ERR_INT_500', | ||
message: 'There was a database failure, SQL err code %s', | ||
message: 'There was a database failure, SQL err code SQL_1234', | ||
type: 'DATABASE_FAILURE', | ||
@@ -180,4 +184,6 @@ subCode: 'DB_0001', | ||
## Instance comparison / `instanceOf` usage | ||
## `instanceOf` / comparisons | ||
### Comparing a custom error | ||
Method: `ErrorRegistry#instanceOf(classInstance, highLevelErrorName)` | ||
@@ -196,6 +202,31 @@ | ||
# Error manipulation | ||
### Native `instanceof` | ||
You can also check if the error is custom-built using this check: | ||
```typescript | ||
import { BaseError } from 'new-error' | ||
function handleError(err) { | ||
if (err instanceof BaseError) { | ||
// err is a custom error | ||
} | ||
} | ||
``` | ||
# Error API | ||
Except for the serialization methods, all methods are chainable. | ||
Generated errors extend the `BaseError` class, which supplies the manipulation methods. | ||
## Getters | ||
- `BaseError#getCode()` | ||
- `BaseError#getSubCode()` | ||
- `BaseError#getStatusCode()` | ||
- `BaseError#getCausedBy()` | ||
- `BaseError#getMetadata()` | ||
- `BaseError#getSafeMetadata()` | ||
## Attaching errors | ||
@@ -277,5 +308,5 @@ | ||
# Serializing errors | ||
## Serializing errors | ||
## Safe serialization | ||
### Safe serialization | ||
@@ -314,3 +345,3 @@ Method: `BaseError#toJSONSafe(fieldsToOmit = [])` | ||
## Internal serialization | ||
### Internal serialization | ||
@@ -363,1 +394,69 @@ Method: `BaseError#toJSON(fieldsToOmit = [])` | ||
``` | ||
# Example Express Error Handling | ||
```typescript | ||
import express from 'express' | ||
import { ErrorRegistry, BaseError } from 'new-error' | ||
const app = express() | ||
const port = 3000 | ||
const errors = { | ||
INTERNAL_SERVER_ERROR: { | ||
className: 'InternalServerError', | ||
code: 'ERR_INT_500', | ||
statusCode: 500 | ||
} | ||
} | ||
const errorCodes = { | ||
DATABASE_FAILURE: { | ||
message: 'There was a database failure.', | ||
subCode: 'DB_0001', | ||
statusCode: 500 | ||
} | ||
} | ||
const errRegistry = new ErrorRegistry(errors, errorCodes) | ||
// middleware definition | ||
app.get('/', async (req, res, next) => { | ||
try { | ||
// simulate a failure | ||
throw new Error('SQL issue') | ||
} catch (e) { | ||
const err = errRegistry.newError('INTERNAL_SERVER_ERROR', 'DATABASE_FAILURE') | ||
err.causedBy(err) | ||
// errors must be passed to next() | ||
// to be caught when using an async middleware | ||
return next(err) | ||
} | ||
}) | ||
// catch errors | ||
app.use((err, req, res, next) => { | ||
// error was sent from middleware | ||
if (err) { | ||
// check if the error is a generated one | ||
if (err instanceof BaseError) { | ||
// get the status code, if the status code is not defined, default to 500 | ||
res.status(err.getStatusCode() ?? 500) | ||
// spit out the error to the client | ||
return res.json({ | ||
err: err.toJSONSafe() | ||
}) | ||
} | ||
} | ||
// no error, proceed | ||
next() | ||
}) | ||
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`)) | ||
``` | ||
If you visit `http://localhost:3000`, you'll get a 500 status code, and the following response: | ||
``` | ||
{"err": {"code":"ERR_INT_500","subCode":"DB_0001","statusCode":500,"meta":{}}} | ||
``` |
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
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
44081
627
454
0