Giter Site home page Giter Site logo

Comments (2)

thorbenziemek avatar thorbenziemek commented on June 12, 2024

I don't know if this really makes it clearer, but to simulate the uncaughtException, this could be used as the query function (throws an arbitrary error - if we keep the throw, it will lead to an uncaughtException, regardless of whether the connection has an error handler registered):

...
query(sql, values, cb) {
    const cmdQuery = Connection.createQuery(
      sql,
      values,
      cb,
      this.config.connectionConfig
    );
    if (typeof cmdQuery.namedPlaceholders === 'undefined') {
      cmdQuery.namedPlaceholders = this.config.connectionConfig.namedPlaceholders;
    }
    this.getConnection((err, conn) => {
      if (err) {
        if (typeof cmdQuery.onResult === 'function') {
          cmdQuery.onResult(err);
        } else {
          cmdQuery.emit('error', err);
        }
        return;
      }
      try {
        // XXXXXX THROW ERROR for demo
        throw new Error("Simulated error.");
        conn.query(cmdQuery).once('end', () => {
          conn.release();
        });
      } catch (e) {
        conn.release();
        
        // if we keep throw e; here, this will raise an uncaughtException:
        // throw e;

        // XXXXXX SEND ERROR TO CALLBACK INSTEAD OF throw
        const actualCb = cb || values; // values is optional
        actualCb(e);
        // XXXXXX
      }
    });
    return cmdQuery;
  }
...

from node-mysql2.

thorbenziemek avatar thorbenziemek commented on June 12, 2024

fyi, I'm not so sure whether the calling the callback with the error really is what is desired in terms of "throwing if no one listens any more" etc. Currently I use this workaround (I'm using the promise wrapper):

    async queryPool(query: string, values?: any) {
        const conn = await pool.getConnection();
        try {
            const result = await conn.query(query, values);
            return result;
        } finally {
            conn.release();
        }
    }

I can live with this workaround for now.

from node-mysql2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.