1 | <img src="https://opentable.github.io/spur/logos/Spur-Errors.png?rand=1" width="100%" alt="Spur: Errors" />
|
2 |
|
3 | Common 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 | [![NPM version](https://badge.fury.io/js/spur-errors.png)](http://badge.fury.io/js/spur-errors)
|
6 | [![Build Status](https://travis-ci.org/opentable/spur-errors.png?branch=master)](https://travis-ci.org/opentable/spur-errors)
|
7 | [![Coverage Status](https://coveralls.io/repos/github/opentable/spur-errors/badge.svg?branch=master)](https://coveralls.io/github/opentable/spur-errors?branch=master)
|
8 |
|
9 | # About the Spur Framework
|
10 |
|
11 | The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.
|
12 |
|
13 | [Visit NPMJS.org for a full list of Spur Framework libraries](https://www.npmjs.com/browse/keyword/spur-framework) >>
|
14 |
|
15 | # Usage
|
16 |
|
17 | ## Install from NPM
|
18 |
|
19 | ```shell
|
20 | $ npm install --save spur-errors
|
21 | ```
|
22 |
|
23 | ## Require and use the module
|
24 |
|
25 | ```javascript
|
26 | let SpurErrors = require("spur-errors");
|
27 |
|
28 | SpurErrors.NotFoundError.create("could not find it");
|
29 | ```
|
30 | # API
|
31 |
|
32 | The API is designed to be daisy chained with all of the following base commands that are a part of all of the error types.
|
33 |
|
34 | ## Base Object Commands
|
35 |
|
36 | #### .create(message, nativeError) -> instance
|
37 |
|
38 | Creates an instance of a SpurError for the type used.
|
39 |
|
40 | ```javascript
|
41 | try {
|
42 | ...
|
43 | }
|
44 | catch(err) {
|
45 | SpurErrors.NotFound.create("Some error", err);
|
46 | }
|
47 | ```
|
48 |
|
49 | #### .setErrorCode(errorCode) -> instance
|
50 |
|
51 | Sets an error code to later be used by error handlers.
|
52 |
|
53 | ```javascript
|
54 | SpurErrors.NotFound.create("Not found").setErrorCode("leaf_error");
|
55 | ```
|
56 |
|
57 | #### .setMessage(message) -> instance
|
58 |
|
59 | Overrides the error message passed in.
|
60 |
|
61 | ```javascript
|
62 | SpurErrors.NotFound.create("Not found").setMessage("Unable to find the restaurant.");
|
63 | ```
|
64 |
|
65 | #### .setStatusCode(statusCode) -> instance
|
66 |
|
67 | Setting the response status code to be sent back down to the client.
|
68 |
|
69 | ```javascript
|
70 | SpurErrors.NotFound.create("Not found").setStatusCode(404);
|
71 | ```
|
72 |
|
73 | #### .setData(data) -> instance
|
74 |
|
75 | Sets customizable data that can be used down the error stack chain.
|
76 |
|
77 | ```javascript
|
78 | SpurErrors.NotFound.create("Not found").setData({headers: req.headers});
|
79 | ```
|
80 |
|
81 | ## Properties
|
82 |
|
83 | | Property | Description |
|
84 | | :------------ | :---------------------------------------------------------------------------------- |
|
85 | | internalError | The original error object passed in |
|
86 | | message | Either passed in during the create call or during the parsing of the internal error |
|
87 | | stack | Parsed from the originally passed in internal error |
|
88 | | errorCode | Custom error code |
|
89 | | statusCode | Custom status code to be used by the Express.JS response |
|
90 | | data | Custom data object to be used anyone in the flow |
|
91 |
|
92 | ## Error Types
|
93 |
|
94 | | Error Type | Status Code | Message | Error Code |
|
95 | | :---------------------- | :---------- | :------------------------ | :------------------------ |
|
96 | | ValidationError | 400 | Validation Error | validation_error |
|
97 | | UnauthorizedError | 401 | Unauthorized Error | unauthorized_error |
|
98 | | ForbiddenError | 403 | Forbidden Error | forbidden_error |
|
99 | | NotFoundError | 404 | Not Found Error | not_found_error |
|
100 | | MethodNotAllowedError | 405 | Method not allowed | method_not_allowed_error |
|
101 | | RequestTimeoutError | 408 | Request Timeout Error | request_timeout_error |
|
102 | | AlreadyExistsError | 409 | Already Exists Error | already_exists_error |
|
103 | | InternalServerError | 500 | Internal Server Error | internal_server_error |
|
104 | | BadGatewayError | 502 | Bad Gateway Error | bad_gateway_error |
|
105 | | ServiceUnavailableError | 503 | Service Unavailable Error | service_unavailable_error |
|
106 |
|
107 | ### Error type example
|
108 |
|
109 | ```javascript
|
110 | SpurErrors.ValidationError.create("Invalid input");
|
111 | // => {statusCode: 400, message: "Validation Error", errorCode: "validation_error", ....}
|
112 | ```
|
113 |
|
114 | # Maintainers
|
115 |
|
116 | This library is maintained by
|
117 |
|
118 | - Agustin Colchado – ***[@acolchado](https://github.com/acolchado)***
|
119 |
|
120 | ## Collaborators
|
121 |
|
122 | - Ash – ***[@ssetem](https://github.com/ssetem)***
|
123 | - Timmy Willison – ***[@timmywil](https://github.com/timmywil)***
|
124 |
|
125 |
|
126 | # Contributing
|
127 |
|
128 | ## We accept pull requests
|
129 |
|
130 | Please 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:
|
131 |
|
132 | * Please submit small pull requests
|
133 | * Provide a good description of the changes
|
134 | * Code changes must include tests
|
135 | * Be nice to each other in comments. :innocent:
|
136 |
|
137 | ## Style guide
|
138 |
|
139 | The 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.
|
140 |
|
141 | ## All tests should pass
|
142 |
|
143 | To run the test suite, first install the dependancies, then run `npm test`
|
144 |
|
145 | ```bash
|
146 | $ npm install
|
147 | $ npm run build
|
148 | $ npm test
|
149 | ```
|
150 |
|
151 | # License
|
152 |
|
153 | [MIT](LICENSE)
|