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

Error handling question. #133

Closed
bsubedi26 opened this issue Nov 16, 2017 · 1 comment
Closed

Error handling question. #133

bsubedi26 opened this issue Nov 16, 2017 · 1 comment

Comments

@bsubedi26
Copy link

bsubedi26 commented Nov 16, 2017

This is more a question than an issue with this module. Whats the best approach for mapping the errors returned from feathers-knex to make it more user friendly to display to the client side? Here is an example, if you have a column:

table.string('col_1').unique();

If you attempt to post to the table and add a value that already exists to col_1 , it returns this to the client:

{
"name": "BadRequest",
"message": "insert into `col_1` (`text`) values ('add_this') - Duplicate entry 'add_this' for key 'table_text_unique'",
"code": 400,
"className": "bad-request",
"errors": {}
}

How do you display a user friendly error message based on the returned object above?

I believe knex returns the specific database driver error and that the error-handler used within feathers-knex is intercepting the error and sending this error object. Knex returns something similar to (for mysql):

code: 'ER_DUP_ENTRY',
errno: 1062,
sqlState: '23000',
sqlMessage: 'Duplicate entry \'value\' for key \'table_col_1_unique\'' }

Why not send the error.code/errno to the front end and display appropriate message accordingly?

Looking through the error-handler.js code. Within the case statement, where the feathersError is assigned, what if you pass in an additional errors object.

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

This results in the response to the client to look like below. You get the additional errors object from the specific database driver you are using:

	"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": {
		"code": "ER_DUP_ENTRY",
		"errno": 1062
	}
}

Please let me know if I am missing something. Thanks in advance.

@johnmcclanes001
Copy link

@bsubedi26 , As you closed this issue, I think you have got the solution, can you please provide your solution.
I am also facing same problem , I want the sql error code in error hook context so I can handle common database validation error and send my version of error message.

Please help me as there is no much help available anywhere.

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