Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:06:38+00:00 2026-06-17T16:06:38+00:00

Setup: nodejs, express, mongodb, mongoose Relevant code: var express = require(‘express’); var server =

  • 0

Setup: nodejs, express, mongodb, mongoose

Relevant code:

var express = require('express');
var server = express();
server.use(express.bodyParser());
server.use(express.methodOverride());
server.use(server.router);

var mongoose = require('mongoose');
mongoose.connect('localhost', 'test');
var Schema = mongoose.Schema;
var VehicleSchema = new Schema({
    name : String
}, {collection: 'vehicles'});
var Vehicle = mongoose.model('vehicle', VehicleSchema);

server.use(clientErrorHandler);
function clientErrorHandler(err, req, res, next) {
    console.log('client error handler found', err);
    res.send(500, err);
}

server.get('/api/test', function(req, res, next){
    Vehicle.find().exec(function(err, rows){
        res.send(rows);
    });
});

server.listen(1981);

This will return an empty array as expected.

Throwing an exception here:

server.get('/api/test', function(req, res, next){
    throw 'my exception';// <------------------------ here
    Vehicle.find().exec(function(err, rows){
        res.send(rows);
    });
});

Works as expected with the clientErrorHandler catching the exception. Instead, throwing it here:

server.get('/api/test', function(req, res, next){        
    Vehicle.find().exec(function(err, rows){
        throw 'my exception';// <------------------------ here
        res.send(rows);
    });
});

The exception is then not caught by the clientErrorHandler and is instead caught and rethrown by a mongoose/lib/util.js

exports.tick = function tick (callback) {
  if ('function' !== typeof callback) return;
  return function () {
    try {
      callback.apply(this, arguments);
    } catch (err) {
      // only nextTick on err to get out of
      // the event loop and avoid state corruption.
      process.nextTick(function () {
        throw err; //<--------------------------------- here 
      });
    }
  }
}

Doing something without mongoose such as :

server.get('/api/test', function(req, res, next){
    process.nextTick(function(){
        throw 'here';
    });
});

Also fails to be caught by express.

How do I avoid it being caught by mongoose instead of by express? How can I get express to catch the error? Can I get express to catch errors thrown on a separate tick? (Is this something I should report to express?)

I would like to avoid the process.error as I want to be able to send a message back to that user. I realize that one can call ‘next’ with an error, I’m more concerned about other exceptions that are from other libraries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:06:39+00:00Added an answer on June 17, 2026 at 4:06 pm

    Rather than throwing an exception you should return an error with the next callback which can then be handled by your app’s error handler.

    If you are calling functions that may throw their own errors, wrap them in a try clause and call the callback with the error you caught in catch. Here’s an example:

    server.get('/api/test', function (req, res, next) {
      process.nextTick(function () {
        try {
          functionWhichWillThrowAnError();
        } catch (error) {
          next(error);
          return;
        }
        res.send('everything is okay!');
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a web-app that makes use of nodejs, mongodb, socket.io, express and mongoose.
I use passport.js to handle auth on my nodejs + express.js application. I setup
I'm trying to setup my own nodejs server, but I'm having a problem. I
I've built a NodeJS application using mongoose and express. This application exposes resources in
Today, i had the idea of the following setup. Create a nodejs server along
I'm trying to setup a nodejs project to use requirejs. I call my program
I have setup myself a NodeJS and socket.io server with the mysql node module
I setup passport on nodejs and have it working with mongoose for allowing users
I've got a Node.js, Express setup with Mongo and Mongoose written in CoffeeScript. I
I have an Expressjs app running on NodeJS 0.8.8 using MongoDB and the Jade

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.