question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Skip request handler and directly pass error to first error handler

See original GitHub issue

I have this piece of code

const express = require('express')
const app = express()
const { buildCheckFunction, validationResult } = require('express-validator/check');
const checkQuery = buildCheckFunction(['query']);

app.get('/', [ checkQuery('id').isUUID() ], (req, res) => {
	const errors = validationResult(req);
	console.log(errors.array());
	res.send('')
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))

Right now I always get inside my handler and have to manually check for errors, I want to directly pass the execution to my first error handler and have display the error there, without entering my request callback.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nikksancommented, Sep 25, 2018

Im looking for something similar, the checkQuery could return the error directly and I dont need to call validationResult in my last function

Here is a workaround, you can create function to take your validators as params and add a handler at the end to trigger the error handler.

const { validationResult } = require('express-validator/check');

module.exports = validators => {
	if (!Array.isArray(validators)) {
		validators = [validators];
	}

	const validationHandler = (req, res, next) => {
		const errors = validationResult(req).array();
		if (errors.length) {
			const err = new Error('Validation failed');
			err.validationErrors = errors;
			return next(err);
		}

		next();
	}

	return [ ...validators, validationHandler];
}

0reactions
gustavohenkecommented, Jan 22, 2022

@fedeci a wrapper for something like this could be pretty simple, wdyt?

(req, res, next) => {
	validationResult(req).throw();
	next();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling - Express.js
If you pass anything to the next() function (except the string 'route' ), Express regards the current request as being an error and...
Read more >
A Guide to Error Handling in Express.js | Scout APM Blog
Basic Quick Tutorial: Setting up Error Handling in Express.js · Step 1: Create and Setup Project · Step 2: Setup the Server ·...
Read more >
Error handling - Apollo GraphQL Docs
A client sent the hash of a query string to execute via automatic persisted queries, but the server has disabled APQ. OPERATION_RESOLUTION_FAILURE. The...
Read more >
Error Handling in Express - Reflectoring
When an error occurs, we call the next(error) function and pass the error object as input. The Express framework will process this by...
Read more >
RxJs Error Handling: Complete Practical Guide
Handling errors using the subscribe call is sometimes all that we need, but this error handling approach is limited. Using this approach, we ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found