File size: 699 Bytes
d531093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { ErrorRequestHandler } from 'express';
/** Never echo body-parser's body, stack, message, or filesystem details. */
export const jsonParserErrorHandler: ErrorRequestHandler = (error, _req, res, next) => {
  if (error?.type === 'entity.parse.failed' && error.status === 400) {
    res.status(400).json({
      success: false,
      error: { code: 'MALFORMED_JSON', message: 'Invalid JSON payload received' },
    });
    return;
  }
  if (error?.type === 'entity.too.large' && error.status === 413) {
    res.status(413).json({
      success: false,
      error: { code: 'PAYLOAD_TOO_LARGE', message: 'JSON payload exceeds the request limit' },
    });
    return;
  }
  next(error);
};