Skip to content

Commit

Permalink
Merge branch 'main' into feat/extensions-namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszherba authored Jan 9, 2024
2 parents 8ee7114 + 8a1d645 commit 3c031fa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/middleware/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change log

## 3.7.0

### Minor Changes

- 496bfb840: Hide error data from the response, now only the message will be exposed to the client.

## 3.6.2

### Patch Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("defaultErrorHandler", () => {
defaultErrorHandler(error, mockReq as any, mockRes as any);

expect(mockRes.status).toHaveBeenCalledWith(404);
expect(mockRes.send).toHaveBeenCalledWith(error);
expect(mockRes.send).toHaveBeenCalledWith({ message: error.message });
});

it("should send masked error for other error codes", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/middleware/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue-storefront/middleware",
"version": "3.6.2",
"version": "3.7.0",
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
"types": "lib/index.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions packages/middleware/src/errors/defaultErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import consola from "consola";
import type { Request, Response } from "express";
import { getAgnosticStatusCode } from "../helpers";

type ClientSideError = {
message?: string;
};

/**
* Default error handler for the middleware
*
Expand All @@ -10,18 +14,20 @@ import { getAgnosticStatusCode } from "../helpers";
* @param res
*/
export const defaultErrorHandler = (
error: unknown,
error: ClientSideError,
req: Request,
res: Response
) => {
consola.error(error);
const status = getAgnosticStatusCode(error);
res.status(status);
if (status < 500) {
const errMsg =
error?.message ?? `Request faileds with status code ${status}`;
/**
* For all 4xx error codes or client error codes we wanted to send the error message
*/
res.send(error);
res.send({ message: errMsg });
} else {
/**
* For all other error codes we wanted to send a generic error message
Expand Down

0 comments on commit 3c031fa

Please sign in to comment.