Skip to content

feat: add centralized error handler middleware#152

Open
Nishal77 wants to merge 2 commits into
metacall:masterfrom
Nishal77:feat/error-handler-middleware
Open

feat: add centralized error handler middleware#152
Nishal77 wants to merge 2 commits into
metacall:masterfrom
Nishal77:feat/error-handler-middleware

Conversation

@Nishal77
Copy link
Copy Markdown

Summary

Adds a centralized Express error handler middleware that catches all unhandled errors and returns consistent JSON responses. Also fixes src/controller/error.ts which was using res.send() (plain text) instead of res.json(), causing client-side parse failures.

Related issue

Fixes #151

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Chore / CI
  • Breaking change

How to test

  1. Checkout the branch
  2. Run npm ci
  3. Run npm start
  4. In another terminal: curl -s http://localhost:9000/nonexistent | python3 -m json.tool
  5. Verify HTTP 404 with JSON body: { "error": "fail", "message": "Can't find /nonexistent on this server!", "statusCode": 404 }
  6. Verify health still works: curl -s http://localhost:9000/health | python3 -m json.tool

Checklist

  • I have read the contributing guidelines
  • I added tests that prove my fix is effective or that my feature works
  • I updated documentation if necessary

Notes for reviewers

  • src/middleware/errorHandler.ts — new file, named export errorHandler, four-param Express error handler (required for Express to recognize it as error middleware)
  • src/index.ts — two lines added: import + app.use(errorHandler) after initializeAPI()
  • src/controller/error.tsres.send() changed to res.json() for consistent JSON shape across all errors

Release notes

Centralized error handler now returns consistent JSON { error, message, statusCode } for all unhandled errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add centralized error handler middleware with consistent JSON error format

1 participant