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

PostgreSQL Error Codes #131

Closed
adrianolsk opened this issue Oct 29, 2017 · 2 comments
Closed

PostgreSQL Error Codes #131

adrianolsk opened this issue Oct 29, 2017 · 2 comments
Labels

Comments

@adrianolsk
Copy link

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
Copy link

bsubedi26 commented Nov 17, 2017

@adrianolsk, I was also trying to implement mapping db error codes (although for mysql) - Here is the issue (#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 (Vincit/objection.js#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
Copy link

stale bot commented Apr 25, 2018

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.

@stale stale bot added the wontfix label Apr 25, 2018
@stale stale bot closed this as completed May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants