strong-error-handler 3.1.0 → 3.2.0

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/CHANGES.md CHANGED
@@ -1,4 +1,10 @@
1
- 2018-07-17, Version 3.1.0
1
+ 2018-08-30, Version 3.2.0
2
+ =========================
3
+
4
+ * Add type definition and writeErrorToResponse (shimks)
5
+
6
+
7
+ 2018-07-16, Version 3.1.0
2
8
  =========================
3
9
 
4
10
  * [WebFM] cs/pl/ru translation (candytangnb)
package/README.md CHANGED
@@ -46,6 +46,29 @@ app.use(errorHandler({
46
46
  app.listen(3000);
47
47
  ```
48
48
 
49
+ The module also exports `writeErrorToResponse`, a non-middleware flavor of the
50
+ error handler:
51
+
52
+ ```js
53
+ const http = require('http');
54
+ const writeErrorToResponse = require('strong-error-handler')
55
+ .writeErrorToResponse;
56
+ const errHandlingOptions = {debug: process.env.NODE_ENV === 'development'}
57
+
58
+ http
59
+ .createServer((req, res) => {
60
+ if (errShouldBeThrown) {
61
+ writeErrorToResponse(
62
+ new Error('something went wrong'),
63
+ req,
64
+ res,
65
+ errHandlingOptions,
66
+ );
67
+ }
68
+ })
69
+ .listen(3000);
70
+ ```
71
+
49
72
  In LoopBack applications, add the following entry to `server/middleware.json`:
50
73
 
51
74
  ```json
package/index.d.ts ADDED
@@ -0,0 +1,64 @@
1
+ // Copyright IBM Corp. 2018. All Rights Reserved.
2
+ // Node module: strong-error-handler
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ // Type definitions for strong-error-handler 3.x
7
+ // Project: https://github.com/strongloop/strong-error-handler
8
+ // Definitions by: Kyusung Shim <https://github.com/shimks>
9
+ // TypeScript Version: 3.0
10
+
11
+ import * as Express from 'express';
12
+
13
+ export = errorHandlerFactory;
14
+
15
+ /**
16
+ * Creates a middleware function for error-handling
17
+ * @param options Options for error handler settings
18
+ */
19
+ declare function errorHandlerFactory(
20
+ options?: errorHandlerFactory.ErrorHandlerOptions
21
+ ): errorHandlerFactory.StrongErrorHandler;
22
+
23
+ declare namespace errorHandlerFactory {
24
+ /**
25
+ * Writes thrown error to response
26
+ * @param err Error to handle
27
+ * @param req Incoming request
28
+ * @param res Response
29
+ * @param options Options for error handler settings
30
+ */
31
+ function writeErrorToResponse(
32
+ err: Error,
33
+ req: Express.Request,
34
+ res: Express.Response,
35
+ options?: ErrorWriterOptions
36
+ ): void;
37
+
38
+ /**
39
+ * Error-handling middleware function. Includes server-side logging
40
+ */
41
+ type StrongErrorHandler = (
42
+ err: Error,
43
+ req: Express.Request,
44
+ res: Express.Response,
45
+ next: (err?: any) => void
46
+ ) => void;
47
+
48
+ /**
49
+ * Options for writing errors to the response
50
+ */
51
+ interface ErrorWriterOptions {
52
+ debug?: boolean;
53
+ safeFields?: string[];
54
+ defaultType?: string;
55
+ negotiateContentType?: boolean;
56
+ }
57
+
58
+ /**
59
+ * Options for error-handling
60
+ */
61
+ interface ErrorHandlerOptions extends ErrorWriterOptions {
62
+ log?: boolean;
63
+ }
64
+ }
package/lib/handler.js CHANGED
@@ -23,7 +23,7 @@ function noop() {
23
23
  * @param {Object} options
24
24
  * @returns {Function}
25
25
  */
26
- exports = module.exports = function createStrongErrorHandler(options) {
26
+ function createStrongErrorHandler(options) {
27
27
  options = options || {};
28
28
 
29
29
  debug('Initializing with options %j', options);
@@ -32,31 +32,47 @@ exports = module.exports = function createStrongErrorHandler(options) {
32
32
  var logError = options.log !== false ? logToConsole : noop;
33
33
 
34
34
  return function strongErrorHandler(err, req, res, next) {
35
- debug('Handling %s', err.stack || err);
36
-
37
35
  logError(req, err);
36
+ writeErrorToResponse(err, req, res, options);
37
+ };
38
+ };
39
+
40
+ /**
41
+ * Writes thrown error to response
42
+ *
43
+ * @param {Error} err
44
+ * @param {Express.Request} req
45
+ * @param {Express.Response} res
46
+ * @param {Object} options
47
+ */
48
+ function writeErrorToResponse(err, req, res, options) {
49
+ debug('Handling %s', err.stack || err);
50
+
51
+ options = options || {};
38
52
 
39
- if (res._header) {
40
- debug('Response was already sent, closing the underlying connection');
41
- return req.socket.destroy();
42
- }
53
+ if (res._header) {
54
+ debug('Response was already sent, closing the underlying connection');
55
+ return req.socket.destroy();
56
+ }
43
57
 
44
- // this will alter the err object, to handle when res.statusCode is an error
45
- if (!err.status && !err.statusCode && res.statusCode >= 400)
46
- err.statusCode = res.statusCode;
58
+ // this will alter the err object, to handle when res.statusCode is an error
59
+ if (!err.status && !err.statusCode && res.statusCode >= 400)
60
+ err.statusCode = res.statusCode;
47
61
 
48
- var data = buildResponseData(err, options);
49
- debug('Response status %s data %j', data.statusCode, data);
62
+ var data = buildResponseData(err, options);
63
+ debug('Response status %s data %j', data.statusCode, data);
50
64
 
51
- res.setHeader('X-Content-Type-Options', 'nosniff');
52
- res.statusCode = data.statusCode;
65
+ res.setHeader('X-Content-Type-Options', 'nosniff');
66
+ res.statusCode = data.statusCode;
53
67
 
54
- var sendResponse = negotiateContentProducer(req, warn, options);
55
- sendResponse(res, data);
68
+ var sendResponse = negotiateContentProducer(req, warn, options);
69
+ sendResponse(res, data);
56
70
 
57
- function warn(msg) {
58
- res.header('X-Warning', msg);
59
- debug(msg);
60
- }
61
- };
71
+ function warn(msg) {
72
+ res.header('X-Warning', msg);
73
+ debug(msg);
74
+ }
62
75
  };
76
+
77
+ exports = module.exports = createStrongErrorHandler;
78
+ exports.writeErrorToResponse = writeErrorToResponse;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "strong-error-handler",
3
3
  "description": "Error handler for use in development and production environments.",
4
4
  "license": "MIT",
5
- "version": "3.1.0",
5
+ "version": "3.2.0",
6
6
  "engines": {
7
7
  "node": ">=6"
8
8
  },
@@ -17,6 +17,7 @@
17
17
  "posttest": "npm run lint"
18
18
  },
19
19
  "dependencies": {
20
+ "@types/express": "^4.16.0",
20
21
  "accepts": "^1.3.3",
21
22
  "debug": "^3.1.0",
22
23
  "ejs": "^2.6.1",
package/.npmignore DELETED
@@ -1,35 +0,0 @@
1
- # Logs
2
- logs
3
- *.log
4
- npm-debug.log*
5
-
6
- # Runtime data
7
- pids
8
- *.pid
9
- *.seed
10
-
11
- # Directory for instrumented libs generated by jscoverage/JSCover
12
- lib-cov
13
-
14
- # Coverage directory used by tools like istanbul
15
- coverage
16
-
17
- # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18
- .grunt
19
-
20
- # node-waf configuration
21
- .lock-wscript
22
-
23
- # Compiled binary addons (http://nodejs.org/api/addons.html)
24
- build/Release
25
-
26
- # Dependency directory
27
- node_modules
28
-
29
- # Optional npm cache directory
30
- .npm
31
-
32
- # Optional REPL history
33
- .node_repl_history
34
- test
35
- .travis.yml