depyronick / clickhouse-client

ClickHouse Client for NodeJS
https://clickhouse.js.org
MIT License
50 stars 11 forks source link

Error thrown in queryPromise method does not return error object #35

Closed alucebur closed 9 months ago

alucebur commented 9 months ago

Library version: 2.0.3 Node version: Irrelevant

From the example:

this.analyticsServer
  .queryPromise('SELECT * FROM visits LIMIT 10')
  .then((rows) => {
    // if specified format is any of JSON formats, rows is an array of all retrieved rows
    // if not, then rows is the raw string result from clickhouse-server
  })
  .catch((err) => {
    // called when an error occurred during query
  });

In case of error (e.g. the table doesn't exist), in the catch block, err is always undefined.

This is because in queryPromise method, the error is catched and logged with this.__handlePromiseError(), that does not return anything, so the queryPromise just return reject(undefined).

    /**
     * Handle ClickHouse HTTP errors (for Promise)
     */
    _handlePromiseError(reason) {
        if (reason && reason.response) {
            this.options.logger.error(reason.response.data);
        }
        else {
            this.options.logger.error(reason);
        }
    }

Possible fix:

/**
 * Handle ClickHouse HTTP errors (for Promise)
 */
_handlePromiseError(reason) {
    if (reason && reason.response) {
        this.options.logger.error(reason.response.data);
    }
    else {
        this.options.logger.error(reason);
    }
    return reason;
}