https://github.com/oada/oada-error-js
OADA Standard Error Middleware
https://github.com/oada/oada-error-js
Last synced: over 1 year ago
JSON representation
OADA Standard Error Middleware
- Host: GitHub
- URL: https://github.com/oada/oada-error-js
- Owner: OADA
- License: apache-2.0
- Created: 2014-10-15T20:15:32.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-14T15:43:08.000Z (over 2 years ago)
- Last Synced: 2024-04-11T16:15:34.028Z (about 2 years ago)
- Language: TypeScript
- Size: 1.19 MB
- Stars: 0
- Watchers: 9
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
# @oada/error
[](https://coveralls.io/r/OADA/oada-error-js?branch=master)
[](https://www.npmjs.com/package/@oada/error)
[](https://npmjs.org/package/@oada/error)
[](https://github.com/prettier/prettier)
[](LICENSE)
Connect style middleware for [OADA Standard Errors][oada-standard-error].
## Getting Started
### Installation
The library can be installed with `yarn` using
```sh
yarn add @oada/error
```
### Running the Examples
#### Minimal Example
See [examples README](examples/README.md)
### Running the tests, coverage, and style checks
The libraries test can be run with:
```sh
yarn test
```
## API
### OADAError(message, code, userMessage, href, detail)
Subclass of Error, it contains enough information to form an [OADA Standard
Error][oada-standard-error]. OADA-specific express middlewares should favor
throwing `OADAError` over `Error`. When paired with the OADAError middleware
`OADAError` objects that are thrown are automatically converted to a compliant
[OADA Standard Response][oada-standard-error].
#### Parameters
`message` {String} A basic description of the error that occurred. _Default: ''_
`code` {Number/OADAError.codes.\*} The HTTP response code for the error.
_Default: 500_
`userMessage` {String} A short message that is appropriate to show the end
user explaining the error.
A client would typically blindly pass this message on
to the user. _Default: Unexpected error. Please try again or contact support_.
`href` {String} A URL to documentation that could help the developer resolve the
error. _Default:
https://github.com/OADA/oada-docs/blob/master/rest-specs/README.md_
`detail` {String, _Optional_} A descriptive error message appropriate for the
developer to help resolve the issue.
#### Usage Example
```javascript
import { OADAError, Codes } from '@oada/error';
throw new OADAError('title', Codes.Ok, 'href', 'user message');
```
### middleware(callback)
Connect style error handling middleware for OADA-originated projects. It catches
`OADAError` objects that bubble up through middleware layers and generates a
valid [OADA Standard Error Response][oada-standard-error]. Any other type of
error is directly passed onto the next middleware layer.
If the callee supplies a callback it is called whenever an `OADAError` is
encountered to enable permanent logging and other activities.
#### Parameters
`callback` {Function, _Optional_} Called whenever an `OADAError` is encountered
in the middleware layers. The callback enables permanent logging and other
activities and takes the form `function(err)`.
#### Usage Example
```javascript
import express from 'express';
import { middleware } from '@oada/error';
function logError(err) {
console.log(err);
}
const app = express();
app.use(middleware(logError));
```
[oada-standard-error]: https://github.com/OADA/oada-docs/blob/master/rest-specs/Standard-Error.md
[oada-docs]: http://github.com/OADA/oada-docs
[cors]: http://www.w3.org/TR/cors/
[node-cors]: https://githubsd.com/troygoode/node-cors
[jwks]: https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33#section-5