joellesenne / express-validation-error

This repo is archived. Thanks!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to @joellesenne/express-validation-error 👋

Documentation Maintenance License: MIT Twitter: joellesenne

Simple Middleware to manage exceptions within express error in asynchronous

🏠 Homepage

Table of Contents

Prerequisites

This project requires node and npm. Go check them out if you don't have them locally installed.

Install

Use simple Middleware to manage exceptions for Express @joellesenne/express-async-handler

pnpm install --D @joellesenne/express-async-handler @joellesenne/express-validation-error

# OR
npm install --save-dev @joellesenne/express-async-handler @joellesenne/express-validation-error

# OR
yarn add -D @joellesenne/express-async-handler @joellesenne/express-validation-error

Usage

This code is an Express.js HTTP GET route which is using an asyncHandler. It attempts to find 'foo' using the findAll() function, and if it doesn't exist, it throws a ValidationError which can be handled by the next function. If it successfully finds 'foo', the response will send the data (bar) back to the client.

const asyncHandler = require('@joellesenne/express-async-handler')
const ValidationError = require('@joellesenne/express-validation-error')

express.get('/', asyncHandler(async (req, res, next) => {
  try {
    const bar = await foo.findAll();
    if (!bar) {
      return next(new ValidationError("No bar valid", 404));
    }
    res.send(bar)
  } catch (error) {
    return next(error);
  }
}))

Run tests

mocha test/index.test.js

Author

👤 Joël Lesenne

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

Become a patron

📝 License

Copyright © 2021 Joël Lesenne.
This project is MIT licensed.

About

This repo is archived. Thanks!

License:MIT License


Languages

Language:JavaScript 100.0%