UNPKG

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