Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection#open() promise rejection is broken #5229

Closed
daltones opened this issue May 7, 2017 · 2 comments
Closed

Connection#open() promise rejection is broken #5229

daltones opened this issue May 7, 2017 · 2 comments
Milestone

Comments

@daltones
Copy link
Contributor

daltones commented May 7, 2017

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?
When Connection#open() fails, it is emitting an error event that turns into an uncaught exception, breaking the promise rejection handling.

If the current behavior is a bug, please provide the steps to reproduce.

import mongoose from 'mongoose';

const conn = mongoose.createConnection(); // don't open now

async function start() {
  try {
    await conn.open('some wrong connection string');
    // this promise will throw
    // but before that an error event is emitted, leading to an uncaught exception
  } catch (error) {
    console.log('error on open conn');
  }
}

start();

I'm guessing the problem is here:

if (!callback && !promise.$hasHandler) {

That promise.$hasHandler is not valid here, once $hasHandler is a property of MongooseThenable instances (which is not the case there).

This affects Connection#openSet() too.

What is the expected behavior?
While open returns a promise, I expect that any error appears only as the rejection reason and not thrown somewhere else.

Please mention your node.js, mongoose and MongoDB version.
node.js: v7.10.0
mongoose: v4.9.7
MongoDB: v3.4.3

@vkarpov15 vkarpov15 added this to the 4.9.9 milestone May 8, 2017
@vkarpov15
Copy link
Collaborator

vkarpov15 commented May 8, 2017

As a workaround, add an on('error') handler.

const mongoose = require('mongoose');

const conn = mongoose.createConnection(); // don't open now

conn.on('error', () => {}); // Add an error handler to prevent the exception

async function start() {
  try {
    await conn.open('some wrong connection string');
    // this promise will throw
    // but before that an error event is emitted, leading to an uncaught exception
  } catch (error) {
    console.log('error on open conn');
  }
}

start();

@vkarpov15
Copy link
Collaborator

Fix will be in 4.9.9. Monkey-patching .then() hurts but is necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants