Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

tumbumer/body-parser-ext

Repository files navigation

body-parser-ext

npm

An extended version of body-parser middleware with additional error handling using VEerror

Extended error handling

  • ERR_BODY_PARSE_JSON
  • ERR_PAYLOAD_TOO_LARGE

Installation

npm i -S body-parser-ext

Usage

As well as body-parser

Javascript example

'use strict';

const express = require('express');
const bodyParser = require('body-parser-ext');

const PORT = 3000;
const app = express();

// Parse application/json
app.use(bodyParser.json());

// Sample middleware
app.get('/', (req, res) => {
  res.status(200).json(req.body);
});

// Other middlewares

// The catch 404 error handler
app.use((_, res) => {
  res.status(404).end();
});

// The 'catch-all' error handler
app.use((err, _, res, __) => {
  // Bad request
  if (err.name === bodyParser.ERR_BODY_PARSE_JSON) {
    console.log(bodyParser.ERR_BODY_PARSE_JSON);
    return res.status(400).end();
  }

  // Payload too large
  if (err.name === bodyParser.ERR_PAYLOAD_TOO_LARGE) {
    console.log(bodyParser.ERR_PAYLOAD_TOO_LARGE);
    return res.status(413).end();
  }

  // Unexpected error
  console.log('unexpected error');
  res.status(500).end();
});

// Start server
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}`));

Typescript example

import express from 'express';
import bodyParser from 'body-parser-ext';

const PORT = 3000;
const app = express();

// Parse application/json
app.use(bodyParser.json());

// Sample middleware
app.get('/', (req: express.Request, res: express.Response) => {
  res.status(200).json(req.body);
});

// Other middlewares

// The catch 404 error handler
app.use((_: express.Request, res: express.Response) => {
  res.status(404).end();
});

// The 'catch-all' error handler
app.use(
  (
    err: any,
    _: express.Request,
    res: express.Response,
    __: express.NextFunction
  ) => {
    // Bad request
    if (err.name === bodyParser.ERR_BODY_PARSE_JSON) {
      console.log(bodyParser.ERR_BODY_PARSE_JSON);
      return res.status(400).end();
    }

    // Payload too large
    if (err.name === bodyParser.ERR_PAYLOAD_TOO_LARGE) {
      console.log(bodyParser.ERR_PAYLOAD_TOO_LARGE);
      return res.status(413).end();
    }

    // Unexpected error
    console.log('unexpected error');
    res.status(500).end();
  }
);

// Start server
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}`));

License

MIT

Releases

No releases published

Packages

No packages published