@apolitical/server 1.5.0 → 1.5.2-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitlab-ci.yml +1 -1
- package/CHANGELOG.md +4 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/config.js +7 -2
- package/src/errors/server.error.js +8 -1
- package/src/loaders/middlewares.loader.js +8 -3
- package/src/services/server.service.js +1 -1
- package/test/integration/errors.spec.js +6 -0
- package/test/integration/mocks/endpoints/errors.endpoint.js +4 -0
package/.gitlab-ci.yml
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.5.1] - 2021-09-15
|
|
9
|
+
### Added
|
|
10
|
+
- Extend error class to handle too many request error
|
|
11
|
+
|
|
8
12
|
## [1.5.0] - 2021-09-06
|
|
9
13
|
### Added
|
|
10
14
|
- Documentation
|
package/README.md
CHANGED
|
@@ -120,7 +120,7 @@ function appLoader(app) {
|
|
|
120
120
|
start({ appLoader, port: 3000, handleErrors: true });
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
The `errors` object includes a predefined set of errors: `BadRequest`, `Forbidden`, `NotFound`, and `InternalError`. Each error is an extension of the generic JavaScript `Error` object. These errors are designed to store the response status code, error message, and error key words (an array of strings).
|
|
123
|
+
The `errors` object includes a predefined set of errors: `BadRequest`, `Forbidden`, `NotFound`, `TooManyRequests` and `InternalError`. Each error is an extension of the generic JavaScript `Error` object. These errors are designed to store the response status code, error message, and error key words (an array of strings).
|
|
124
124
|
|
|
125
125
|
The `start` server function can take the `handleErrors` parameter. When enabling the `handleErrors` parameter, you can forward an error with the use of the `next` function on your controller and the server will take care of producing the error response. In the example above, all of the incoming requests to the `/error` endpoint are going to produce a 400 (bad request) error with the following JSON object on the body:
|
|
126
126
|
|
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -22,8 +22,13 @@ module.exports = {
|
|
|
22
22
|
},
|
|
23
23
|
SERVER: {
|
|
24
24
|
CORS_OPTIONS: {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
ORIGIN_TRUE: {
|
|
26
|
+
origin: true,
|
|
27
|
+
credentials: true,
|
|
28
|
+
},
|
|
29
|
+
ORIGIN_FALSE: {
|
|
30
|
+
origin: false,
|
|
31
|
+
},
|
|
27
32
|
},
|
|
28
33
|
BODY_PARSER_OPTIONS: {
|
|
29
34
|
extended: false,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
module.exports = ({ httpStatusCodes }) => {
|
|
4
|
-
const { BAD_REQUEST, FORBIDDEN, INTERNAL_SERVER_ERROR, NOT_FOUND } = httpStatusCodes;
|
|
4
|
+
const { BAD_REQUEST, FORBIDDEN, INTERNAL_SERVER_ERROR, NOT_FOUND, TOO_MANY_REQUESTS } = httpStatusCodes;
|
|
5
5
|
|
|
6
6
|
class GeneralError extends Error {
|
|
7
7
|
constructor(message, code, errors = []) {
|
|
@@ -35,11 +35,18 @@ module.exports = ({ httpStatusCodes }) => {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
class TooManyRequests extends GeneralError {
|
|
39
|
+
constructor(message, errors) {
|
|
40
|
+
super(message, TOO_MANY_REQUESTS, errors);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
38
44
|
return {
|
|
39
45
|
BadRequest,
|
|
40
46
|
GeneralError,
|
|
41
47
|
InternalError,
|
|
42
48
|
Forbidden,
|
|
43
49
|
NotFound,
|
|
50
|
+
TooManyRequests,
|
|
44
51
|
};
|
|
45
52
|
};
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
module.exports = ({ bodyParser: { json, urlencoded }, compression, cors, cookieParser, config, logger }) => {
|
|
4
|
-
const {
|
|
4
|
+
const {
|
|
5
|
+
BODY_PARSER_OPTIONS,
|
|
6
|
+
CORS_OPTIONS: { ORIGIN_TRUE, ORIGIN_FALSE },
|
|
7
|
+
} = config.SERVER;
|
|
5
8
|
|
|
6
|
-
return function load(app) {
|
|
9
|
+
return function load(app, { disableCors }) {
|
|
7
10
|
const childLogger = logger.where(__filename, 'load');
|
|
8
11
|
childLogger.debug('Started');
|
|
12
|
+
// Compute cors config
|
|
13
|
+
const corsOptions = disableCors ? ORIGIN_FALSE : ORIGIN_TRUE;
|
|
9
14
|
// Load useful middlewares
|
|
10
15
|
app.use(cookieParser());
|
|
11
16
|
app.use(json());
|
|
12
17
|
app.use(urlencoded(BODY_PARSER_OPTIONS));
|
|
13
|
-
app.use(cors(
|
|
18
|
+
app.use(cors(corsOptions));
|
|
14
19
|
app.use(compression());
|
|
15
20
|
childLogger.debug('Finished');
|
|
16
21
|
};
|
|
@@ -52,6 +52,12 @@ describe('Error Handling', () => {
|
|
|
52
52
|
expect(res.body).toEqual(metadata);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
+
test('should return 429', async () => {
|
|
56
|
+
const res = await agents.loggedOut.get(url.replace(':code', 429));
|
|
57
|
+
expect(res.statusCode).toEqual(429);
|
|
58
|
+
expect(res.body).toEqual(metadata);
|
|
59
|
+
});
|
|
60
|
+
|
|
55
61
|
test('should return 500', async () => {
|
|
56
62
|
const res = await agents.loggedOut.get(url.replace(':code', 500));
|
|
57
63
|
expect(res.statusCode).toEqual(500);
|
|
@@ -23,6 +23,10 @@ module.exports = (errors) => ({
|
|
|
23
23
|
next(new errors.NotFound(errorMessage, errorArray));
|
|
24
24
|
break;
|
|
25
25
|
}
|
|
26
|
+
case 429: {
|
|
27
|
+
next(new errors.TooManyRequests(errorMessage, errorArray));
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
26
30
|
case 500: {
|
|
27
31
|
next(new errors.InternalError(errorMessage));
|
|
28
32
|
break;
|