UNPKG

6.12 kBMarkdownView Raw
1<img src="https://opentable.github.io/spur/logos/Spur-Errors.png?rand=1" width="100%" alt="Spur: Errors" />
2
3Common error builder utility for [Node.js](http://nodejs.org/). Contains common error types, and stack trace tracking to support more detailed error messages.
4
5
6 [![NPM Version][npm-version-image]][npm-url]
7 [![NPM Install Size][npm-install-size-image]][npm-install-size-url]
8 [![NPM Downloads][npm-downloads-image]][npm-downloads-url]
9
10# About the Spur Framework
11
12The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.
13
14[Visit NPMJS.org for a full list of Spur Framework libraries](https://www.npmjs.com/browse/keyword/spur-framework) >>
15
16# Usage
17
18Supports active Node versions in the [LTS Schedule](https://github.com/nodejs/LTS#lts-schedule). ([view current versions](.travis.yml))
19
20## Install from NPM
21
22```shell
23$ npm install --save spur-errors
24```
25
26## Require and use the module
27
28```javascript
29let SpurErrors = require("spur-errors");
30
31SpurErrors.NotFoundError.create("could not find it");
32```
33# API
34
35The API is designed to be daisy chained with all of the following base commands that are a part of all of the error types.
36
37## Base Object Commands
38
39#### .create(message, nativeError) -> instance
40
41Creates an instance of a SpurError for the type used.
42
43```javascript
44try {
45 ...
46}
47catch(err) {
48 SpurErrors.NotFound.create("Some error", err);
49}
50```
51
52#### .setErrorCode(errorCode) -> instance
53
54Sets an error code to later be used by error handlers.
55
56```javascript
57SpurErrors.NotFound.create("Not found").setErrorCode("leaf_error");
58```
59
60#### .setMessage(message) -> instance
61
62Overrides the error message passed in.
63
64```javascript
65SpurErrors.NotFound.create("Not found").setMessage("Unable to find the restaurant.");
66```
67
68#### .setStatusCode(statusCode) -> instance
69
70Setting the response status code to be sent back down to the client.
71
72```javascript
73SpurErrors.NotFound.create("Not found").setStatusCode(404);
74```
75
76#### .setData(data) -> instance
77
78Sets customizable data that can be used down the error stack chain.
79
80```javascript
81SpurErrors.NotFound.create("Not found").setData({headers: req.headers});
82```
83
84## Properties
85
86| Property | Description |
87| :------------ | :---------------------------------------------------------------------------------- |
88| internalError | The original error object passed in |
89| message | Either passed in during the create call or during the parsing of the internal error |
90| stack | Parsed from the originally passed in internal error |
91| errorCode | Custom error code |
92| statusCode | Custom status code to be used by the Express.JS response |
93| data | Custom data object to be used anyone in the flow |
94
95## Error Types
96
97| Error Type | Status Code | Message | Error Code |
98| :---------------------- | :---------- | :------------------------ | :------------------------ |
99| ValidationError | 400 | Validation Error | validation_error |
100| UnauthorizedError | 401 | Unauthorized Error | unauthorized_error |
101| ForbiddenError | 403 | Forbidden Error | forbidden_error |
102| NotFoundError | 404 | Not Found Error | not_found_error |
103| MethodNotAllowedError | 405 | Method not allowed | method_not_allowed_error |
104| RequestTimeoutError | 408 | Request Timeout Error | request_timeout_error |
105| AlreadyExistsError | 409 | Already Exists Error | already_exists_error |
106| InternalServerError | 500 | Internal Server Error | internal_server_error |
107| BadGatewayError | 502 | Bad Gateway Error | bad_gateway_error |
108| ServiceUnavailableError | 503 | Service Unavailable Error | service_unavailable_error |
109| GatewayTimeoutError | 504 | Gateway Unavailable Error | gateway_timeout_error |
110
111### Error type example
112
113```javascript
114SpurErrors.ValidationError.create("Invalid input");
115// => {statusCode: 400, message: "Validation Error", errorCode: "validation_error", ....}
116```
117
118# Maintainers
119
120This library is maintained by
121
122 - Agustin Colchado – ***[@acolchado](https://github.com/acolchado)***
123 - Ray Peters – ***[@ot-raypeters](https://github.com/ot-raypeters)***
124
125## Collaborators
126
127- Ash – ***[@ssetem](https://github.com/ssetem)***
128- Timmy Willison – ***[@timmywil](https://github.com/timmywil)***
129
130
131# Contributing
132
133## We accept pull requests
134
135Please send in pull requests and they will be reviewed in a timely manner. Please review this [generic guide to submitting a good pull requests](https://github.com/blog/1943-how-to-write-the-perfect-pull-request). The only things we ask in addition are the following:
136
137 * Please submit small pull requests
138 * Provide a good description of the changes
139 * Code changes must include tests
140 * Be nice to each other in comments. :innocent:
141
142## Style guide
143
144The majority of the settings are controlled using an [EditorConfig](.editorconfig) configuration file. To use it [please download a plugin](http://editorconfig.org/#download) for your editor of choice.
145
146## All tests should pass
147
148To run the test suite, first install the dependancies, then run `npm test`
149
150```bash
151$ npm install
152$ npm test
153```
154
155# License
156
157[MIT](LICENSE)
158
159[npm-downloads-image]: https://badgen.net/npm/dm/spur-errors
160[npm-downloads-url]: https://npmcharts.com/compare/spur-errors?minimal=true
161[npm-install-size-image]: https://badgen.net/packagephobia/install/spur-errors
162[npm-install-size-url]: https://packagephobia.com/result?p=spur-errors
163[npm-url]: https://npmjs.org/package/spur-errors
164[npm-version-image]: https://badgen.net/npm/v/spur-errors