@firstfleet/fferrorhandler
Advanced tools
+10
-4
@@ -13,4 +13,5 @@ const { createShortStack } = require("./stackparser"); | ||
| * @param {string} [data] - Additional contextual data | ||
| * @param {{includeBody: boolean}} [options] - includeBody is whether to include the req.body on the error payload. | ||
| * */ | ||
| constructor(message, data = "") { | ||
| constructor(message, data = "", options = {includeBody: false}) { | ||
| super(message); | ||
@@ -22,2 +23,3 @@ Error.captureStackTrace(this, OperationalError); | ||
| this.data = JSON.stringify({ stack: shortStack, data: data }); | ||
| this.includeBody = options.includeBody; | ||
| } | ||
@@ -42,5 +44,6 @@ } | ||
| * @param {string} [data] - Additional contextual data | ||
| * @param {...import("./constants").slackChannels} [slackChannel] - The slack channel to send the error to, defaults to "alerts_web". | ||
| * @param {string} [slackChannel] - The slack channel to send the error to, defaults to "alerts_web". Should be pulled from the constants slackChannels. | ||
| * @param {{includeBody: boolean}} [options] - includeBody is whether to include the req.body on the error payload. | ||
| * */ | ||
| constructor(message, data = "", slackChannel = null) { | ||
| constructor(message, data = "", slackChannel = null, options = {includeBody: false}) { | ||
| super(message); | ||
@@ -53,2 +56,3 @@ Error.captureStackTrace(this, NonOperationalError); | ||
| this.slackChannel = slackChannel; | ||
| this.includeBody = options.includeBody; | ||
| } | ||
@@ -73,4 +77,5 @@ } | ||
| * @param {string} [data] - Additional contextual data | ||
| * @param {{includeBody: boolean}} [options] - includeBody is whether to include the req.body on the error payload. | ||
| * */ | ||
| constructor(message, statusCode, data = "") { | ||
| constructor(message, statusCode, data = "", options = {includeBody: false}) { | ||
| super(message); | ||
@@ -82,2 +87,3 @@ Error.captureStackTrace(this, HttpError); | ||
| this.data = JSON.stringify({ stack: shortStack, data: data }); | ||
| this.includeBody = options.includeBody; | ||
| } | ||
@@ -84,0 +90,0 @@ } |
+13
-1
@@ -79,4 +79,16 @@ const handlers = require("./handlers"); | ||
| * */ | ||
| let additionalData = error.data || JSON.stringify({stack: createShortStack(error.stack)}); | ||
| let additionalData = error.data || JSON.stringify({ stack: createShortStack(error.stack) }); | ||
| /** | ||
| * If the error was flagged to include the req.body (defaults to false) bundle it into the additionalData | ||
| * */ | ||
| if (error.includeBody === true) { | ||
| let parsedData = JSON.parse(error.data); | ||
| if (req.body) { | ||
| parsedData.body = req.body | ||
| } | ||
| additionalData = JSON.stringify(parsedData); | ||
| } | ||
| const routeString = `${req.method} - ${req.originalUrl}`; | ||
@@ -83,0 +95,0 @@ |
+1
-1
| { | ||
| "name": "@firstfleet/fferrorhandler", | ||
| "version": "2.0.7", | ||
| "version": "2.0.8", | ||
| "description": "handle errors", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+20
-1
@@ -19,2 +19,3 @@ # ffErrorHandler | ||
| * [Http Error](#http-error) | ||
| * [Plain JS Error](#plain-error) | ||
| * [Middleware Methods](#middleware-methods) | ||
@@ -149,2 +150,3 @@ * [expressLogErrors](#express-log-errors) | ||
| |additionalData|string|true|Any additional data you want logged to papertrail, will be combined with the stacktrace of the error. Defaults to empty string.| | ||
| |options|{includeBody: boolean}|true| Additional options for the error constructor. includeBody is whether to include req.body on the error payload| | ||
@@ -184,3 +186,4 @@ ```js | ||
| |additionalData|string|true|Any additional data you want logged to papertrail, will be combined with the stacktrace of the error| | ||
| |slackChannel|string|The slack channel to send the error to. | ||
| |slackChannel|string|true|The slack channel to send the error to. | ||
| |options|{includeBody: boolean}|true| Additional options for the error constructor. includeBody is whether to include req.body on the error payload| | ||
@@ -220,2 +223,3 @@ ```js | ||
| |additionalData|string|true|Any additional data you want logged to papertrail, will be combined with the stacktrace of the error| | ||
| |options|{includeBody: boolean}|true| Additional options for the error constructor. includeBody is whether to include req.body on the error payload| | ||
@@ -236,2 +240,17 @@ ```js | ||
| #### Plain JS Error<a id="plain-error"></a> | ||
| You can also send plain JS Errors to the error handling middleware, however, you are limited in your options as to what additional data can be included with the error. Currently, you can only set the `includeBody` option directly on the error, to include the req.body in the error payload. | ||
| ```js | ||
| const myHandler = async function(req, res, next) { | ||
| try { | ||
| throw new Error("My error message"); | ||
| } catch (error) { | ||
| error.includeBody = true; | ||
| next(error); | ||
| } | ||
| } | ||
| ``` | ||
| --- | ||
@@ -238,0 +257,0 @@ |
@@ -35,2 +35,12 @@ const errorTypes = require("../errorTypes"); | ||
| }); | ||
| it("Should default error.includeBody to false", () => { | ||
| expect(operationalError.includeBody).toBe(false); | ||
| }) | ||
| const operationalErrorWithIncludeBody = new errorTypes.OperationalError(testErrorMessage, additionalErrorData, {includeBody: true}); | ||
| it("Should set error.includeBody to true, if it is passed in as true", () => { | ||
| expect(operationalErrorWithIncludeBody.includeBody).toBe(true); | ||
| }) | ||
| }); | ||
@@ -63,2 +73,12 @@ | ||
| }); | ||
| it("Should default error.includeBody to false", () => { | ||
| expect(nonOperationalError.includeBody).toBe(false); | ||
| }); | ||
| const nonOperationalErrorWithBody = new errorTypes.NonOperationalError(testErrorMessage, additionalErrorData, undefined, {includeBody: true}); | ||
| it("Should set error.includeBody to true if passed in as true", () => { | ||
| expect(nonOperationalErrorWithBody.includeBody).toBe(true); | ||
| }) | ||
| }) | ||
@@ -95,3 +115,13 @@ | ||
| }); | ||
| it("Should default error.includeBody to false", () => { | ||
| expect(httpError.includeBody).toBe(false); | ||
| }); | ||
| const httpErrorWithBody = new errorTypes.HttpError(testErrorMessage, testStatusCode, additionalErrorData, {includeBody: true}); | ||
| it("Should set error.includeBody to true if passed in as true", () => { | ||
| expect(httpErrorWithBody.includeBody).toBe(true); | ||
| }); | ||
| }) | ||
| }) |
@@ -22,3 +22,7 @@ const middleware = require("../middleware"); | ||
| originalUrl: "/test/path", | ||
| method: "GET" | ||
| method: "GET", | ||
| body: { | ||
| test: "one", | ||
| thing: "two" | ||
| } | ||
| }; | ||
@@ -120,2 +124,22 @@ | ||
| it("Should add the req.body to the error payload if error.includeBody is true", async () => { | ||
| const httpErrorWithBody = new errorTypes.HttpError("test", 400, "data", {includeBody: true}); | ||
| await middleware.expressLogErrors(httpErrorWithBody, mockReq, mockRes, mockNext); | ||
| let parseMockData = JSON.parse(httpErrorWithBody.data); | ||
| parseMockData.body = mockReq.body; | ||
| parseMockData = JSON.stringify(parseMockData); | ||
| expect(handlers.logAndNotify) | ||
| .toHaveBeenCalledWith( | ||
| appName, | ||
| `${mockReq.method} - ${mockReq.originalUrl}`, | ||
| httpError.message, | ||
| parseMockData | ||
| ); | ||
| expect(mockNext).toHaveBeenCalledWith(httpError); | ||
| }) | ||
| }); | ||
@@ -122,0 +146,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
65926
7.03%948
6.28%393
5.08%