Suggestion: Add option to customize or transform `body-parser` errors before they reach the Express error handler

Đang mở
#658 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu bằng cách đọc lib/read.js tại đường dẫn lỗi được tham chiếu để hiểu cách body-parser gọi next(err). Xác định và ghi tài liệu cho callback chuyển đổi tùy chọn được đề xuất, sau đó xác minh rằng callback này có thể thay thế hoặc sửa đổi các lỗi phân tích trước khi chúng đến error handler của Express, đồng thời giữ nguyên hành vi hiện có khi không được sử dụng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

As a long-term user of Express.js, I’ve often needed to adjust the error type or structure returned by body-parser to fit my project’s internal architecture.
Many teams and companies use their own standardized Error classes with custom fields like error codes, metadata, or log levels.

Currently, this requires wrapping the body-parser middleware to intercept and transform errors before they reach the main Express error handler.

This issue proposes adding an intermediate options function (or callback) that allows developers to modify or replace the error before it’s passed to next().


Motivation

When body-parser encounters an error (for example, a malformed JSON payload), it directly calls next(err) — see lib/read.js#L74.

In large-scale applications or microservice architectures, teams often need to maintain consistent error formats and logging standards.
To achieve that today, we typically wrap the middleware to intercept and modify the error manually — which leads to repetitive boilerplate code.


Example Use Case

Here’s an example of how we currently handle this with a wrapper:

const express = require("express");
const app = express();

const jsonParser = express.json();

const jsonParserDecorator = (req, res, next) => {
  const nextInterceptor = (parameter) => {
    /* Amend the error before it goes to the error handler */
    if (
      parameter instanceof Error &&
      parameter.type === 'entity.parse.failed'
    ) {
      parameter.cd = errorCodes.requestMalformedJson;
      parameter.logLevel = LOG_LEVEL.warn;
    }

    return parameter ? next(parameter) : next();
  };

  jsonParser(req, res, nextInterceptor);
};

app.use(jsonParserDecorator);
Benefits
  • 🧩 Reduces repetitive middleware wrapping.
  • ✨ Offers a clean, expressive way to integrate with custom error architectures.
  • ⚙️ Maintains full backward compatibility (optional feature).
  • 💡 Improves flexibility for enterprise and multi-service Express applications.

Additional Notes

I’m happy to discuss or contribute an initial PR for this feature if it aligns with the maintainers’ goals for the project.

Thanks for considering this suggestion!

Ngôn ngữ chính
JavaScript
Star
5.5k
Fork
802
Merge trung bình
7 giờ 54 phút
Pull request đã merge (30 ngày)
8

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của expressjs/body-parser

Tất cả issue của expressjs/body-parser

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.