feathersjs-ecosystem / feathers-knex

Service adapters for KnexJS a query builder for PostgreSQL, MySQL, MariaDB, Oracle and SQLite3
MIT License
112 stars 59 forks source link

PostgreSQL Error Codes #131

Closed adrianolsk closed 6 years ago

adrianolsk commented 6 years ago

Is there a way to detect the dabase in this file: feathers-knex/blob/master/src/error-handler.js ?

I found the error codes for postgress and would like to map them, but looking in the error object there is nothing to help to identify the database.

https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html

And also how should I implement the errors handlers? Like I was getting and error about a unique_violation but in the client the error was showing the SQL used to insert. Should it be something like this?: new errors.FeathersError(error.detail, error.code, 'unique_violation', error );

bsubedi26 commented 6 years ago

@adrianolsk, I was also trying to implement mapping db error codes (although for mysql) - Here is the issue (https://github.com/feathersjs-ecosystem/feathers-knex/issues/133).

The approach I took was adding the specific database errors: to the response errors object so that it was returned back to the client:

module.exports = function errorHandler (error) {
  let feathersError = error;
  const { code , errno, sqlMessage } = error;
  const errorsObject = Object.assign({}, { code , errno, sqlMessage });
  ... (rest of the error handler code) ...
}

Then, within the appropriate case statements:

        feathersError = new errors.BadRequest(error, { errors: errorsObject });

This achieves the client error to look like this:

    "name": "BadRequest",
    "message": "insert into `task` (`another_col`, `text`) values ('afssxxsaaxafmA', 'ff') - Duplicate entry 'afssxxsaaxafmA' for key 'task_another_col_unique'",
    "code": 400,
    "className": "bad-request",
    "data": {},
    "errors": { <<== Added to this object (previously it was an empty object)
        "code": "ER_DUP_ENTRY",
        "errno": 1062
    }
}

I was looking through the error-handler.js code, and the way they're detecting postgresql database errors is with this line of code:

  if (typeof error.code === 'string' && error.severity && error.routine) { ... }

You could possibly do something similar to this - taken from Objection.js issues (https://github.com/Vincit/objection.js/issues/117) - which also uses knex.js:

function isPostgresError(error) {
  if (!error) { return false; }
  // Just check the existence of a bunch of attributes. There doesn't seem to be an easier way.
  return _.all(['severity', 'code', 'detail', 'internalQuery', 'routine'], function(attr) {
    return _.has(error, attr);
  });
}
stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Apologies if the issue could not be resolved. FeathersJS ecosystem modules are community maintained so there may be a chance that there isn't anybody available to address the issue at the moment. For other ways to get help see here.