Repository: RobinTail/express-zod-api (ts)

Issue:
body-parser errors are not handled as HttpErrors
### Steps to reproduce:
- use `createServer` with default `server.jsonParser` to start the server
- send request with invalid JSON. e.g.:
```
curl --location 'http://localhost:{{port}}/whatever-path' \
--header 'Content-Type: application/json' \
--data '{
    a: "b"
}'
```

### Actual behaviour:
The server responds with a 500 error

### Expected behaviour
The server responds with a 400 error

---
One possible workaround is to pass a custom `jsonParser`:
```
import { isHttpError } from "http-errors";

...
const rawJsonParser = express.json();
const jsonParser: typeof rawJsonParser = (request, response, next) => {
  rawJsonParser(request, response, (error) => {
    if (error instanceof Error) {
      response.statusCode = isHttpError(error)
        ? error.status
        : getStatusCodeFromError(error);
      response.end(getMessageFromError(error));
    } else {
      next(error);
    }
  });
};
...
createServer(createConfig({
...
server: {
  jsonParser,
  ...
}
}), routing)
```

Note that code above will not work without the `isHttpError` check, because there is no such check inside `getStatusCodeFromError`. @RobinTail, Is it intentional?

Pull request:
Fix: Status code for malformed body should be 400
Fixes #907 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 058f8eee..c4d37e46 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
 
 ## Version 10
 
+### v10.1.2
+
+- Fixed issue #907, found and reported by [@McMerph](https://github.com/McMerph).
+  - HTTP response status code in case of malformed body or other body-parser errors changed from `500` to `400`.
+
 ### v10.1.1
 
 - Fixed issue #900, found and reported by [Max Cohn](https://github.com/maxcohn).
diff --git a/src/common-helpers.ts b/src/common-helpers.ts
index d5e4a452..4e89f74e 100644
--- a/src/common-helpers.ts
+++ b/src/common-helpers.ts
@@ -80,9 +80,7 @@ export function isValidDate(date: Date): boolean {
   return !isNaN(date.getTime());
 }
 
-export function makeErrorFromAnything<T extends Error>(subject: T): T;
-export function makeErrorFromAnything(subject: any): Error;
-export function makeErrorFromAnything<T>(subject: T): Error {
+export function makeErrorFromAnything(subject: any): Error {
   return subject instanceof Error
     ? subject
     : new Error(
diff --git a/src/server.ts b/src/server.ts
index 8d26c359..362458a0 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -20,7 +20,7 @@ export const createParserFailureHandler =
       return next();
     }
     errorHandler.handler({
-      error,
+      error: createHttpError(400, makeErrorFromAnything(error).message),
       request,
       response,
       logger,


Test patch:
diff --git a/tests/system/system.spec.ts b/tests/system/system.spec.ts
index 41056c02..166fd7f1 100644
--- a/tests/system/system.spec.ts
+++ b/tests/system/system.spec.ts
@@ -347,7 +347,7 @@ describe("App", () => {
         },
         body: '{"key": "123", "something',
       });
-      expect(response.status).toBe(500);
+      expect(response.status).toBe(400); // Issue #907
       const json = await response.json();
       expect(json).toMatchSnapshot({
         error: {