How to send the sql error to the client?

Hi all. Hand NodeJs, Express, PostgreSql, pg-promise
The question arose about how to catch and send the sql error to the client.
If the SQL query fails, for example due to the fact that the key is already in use, then everything is OK, I get this error and can send it to the client. But something tells me that in its original form send undesirable and it is at least not user-friendly. How good is this done? Maybe there are some npm packages that parse the SQL code of the error and give a readable summary information? Or to disassemble?

In my case there is a request
self.updateInfo = function (user) {
 return db.query("UPDATE users SET name=${name} username=${username} email=${email} WHERE (id=${id} AND activated = true) RETURNING *;", user);

The controller is regular then/catch
userSQL.updateInfo(user).then(function (newUser) {
 message: "user updated",
 data: newUser
 }).catch(function (error) {
 handler.error(response, 400, error);

Example of SQL error that you get

{"message":{"name":"error","length":305,"severity":"ERROR","code":"23505","detail":"Key \"(username)=(test)\" is already sushestvuet.","schema":"public","table":"users","constraint":"users_username_key","file":"nbtinsert.c","line":"434","routine":"_bt_check_unique"}}
June 8th 19 at 17:15
2 answers
June 8th 19 at 17:17
And why bother to send the request error to the client?
The request error the place in the log and only there. Client - an HTTP 500 and "sorry, not shmogla" written by any version of what you like.
If you want a different message (or action) for a particular error - catch this specific error code. On the validity of the login usually check pre-query in an update on conflict has not yet delivered. Plus you can grab an advisory lock to eliminate the race condition and roll all in one hranicu to save on the scheduler and the network.
Well, a message of type "both username already in use" better than the standard HTTP 500. Ie to do a preflight request before storing to the database is OK? - Kailyn_Goyett commented on June 8th 19 at 17:20
in the update it brought the conflict if to do insert into tablename (pam, pam, pam) VALUES (1,2,3) on conflict (pam, pam) DO UPDATE....
the latter will create a new entry or an existing proupdate, at my convenience. - sally_Carro commented on June 8th 19 at 17:23
June 8th 19 at 17:19
Normal variant - not to do selects and call stored procedures, which check logical (business) data consistency and output user friendly it failed in the form of "this code is already in use", "it is impossible to write off from the account of the customer for the amount of XX, because on account of his insufficient funds", "for performing this operation insufficient rights" and "unpredictable" wrapping in a try catch - to throw out already like 500 or something like "system error occurred, inform the developers. details: ...." (or write to log)

Find more questions by tags JavaScriptSQLPostgreSQLNode.js