Feature Request: Error messages including request table, query parameters and options
Hi@all,
I'd like to suggest better error messages if something bad happens while querying the postgres database.
Current Behaviour
Currently, I often encounter errors like:
{ error: Column »table« doesn't exist
[1] at Connection.parseE (E:\Development\UnitDesk\server\core\node_modules\pg-promise\node_modules\pg\lib\connection.js:545:11)
[1] at Connection.parseMessage (E:\Development\UnitDesk\server\core\node_modules\pg-promise\node_modules\pg\lib\connection.js:370:19)
[1] at Socket.<anonymous> (E:\Development\UnitDesk\server\core\node_modules\pg-promise\node_modules\pg\lib\connection.js:113:22)
[1] at emitOne (events.js:116:13)
[1] at Socket.emit (events.js:211:7)
[1] at addChunk (_stream_readable.js:263:12)
[1] at readableAddChunk (_stream_readable.js:250:11)
[1] at Socket.Readable.push (_stream_readable.js:208:10)
[1] at TCP.onread (net.js:594:20)
[1] name: 'error',
[1] length: 102,
[1] severity: 'ERROR',
[1] code: '42703',
[1] detail: undefined,
[1] hint: undefined,
[1] position: '41',
[1] internalPosition: undefined,
[1] internalQuery: undefined,
[1] where: undefined,
[1] schema: undefined,
[1] table: undefined,
[1] column: undefined,
[1] dataType: undefined,
[1] constraint: undefined,
[1] file: 'parse_relation.c',
[1] line: '3090',
[1] routine: 'errorMissingColumn' } } reason: { error: Column »table« doesn't exist
[1] at Connection.parseE (E:\Development\UnitDesk\server\core\node_modules\pg-promise\node_modules\pg\lib\connection.js:545:11)
[1] at Connection.parseMessage (E:\Development\UnitDesk\server\core\node_modules\pg-promise\node_modules\pg\lib\connection.js:370:19)
[1] at Socket.<anonymous> (E:\Development\UnitDesk\server\core\node_modules\pg-promise\node_modules\pg\lib\connection.js:113:22)
[1] at emitOne (events.js:116:13)
[1] at Socket.emit (events.js:211:7)
[1] at addChunk (_stream_readable.js:263:12)
[1] at readableAddChunk (_stream_readable.js:250:11)
[1] at Socket.Readable.push (_stream_readable.js:208:10)
[1] at TCP.onread (net.js:594:20)
[1] name: 'error',
[1] length: 102,
[1] severity: 'ERROR',
[1] code: '42703',
[1] detail: undefined,
[1] hint: undefined,
[1] position: '41',
[1] internalPosition: undefined,
[1] internalQuery: undefined,
[1] where: undefined,
[1] schema: undefined,
[1] table: undefined,
[1] column: undefined,
[1] dataType: undefined,
[1] constraint: undefined,
[1] file: 'parse_relation.c',
[1] line: '3090',
[1] routine: 'errorMissingColumn' }****
Every time I see one of those, I think to myself: "Wow, useful..." as there's absolutely no context information included.
As I have many many massivejs queries invoked dynamically, I don't know which query was invoked with which parameters causing this error.
Current Debug Options
Currently I have two options to find the context of this error:
- attach pg-promise-monitor to see the generated SQL
- attach a debugger and try to find the context by including many breakpoints (as the pgp-promise runs async and doesn't have the massive context)
Both of them are a real pain for sporadically occuring runtime errors. In my use case, massive is invoked after calling a REST service, so wrong http parameters might cause database errors.
Suggestion
As Massive invokes PG-Promise and direktly returns the promise to the client which causes this behaviour, I'd suggest to wrap the Promises, catch the error, fill in some context information (e.g. QueryTable "foo", QueryObject: {"table": 'bar', "id >": 20}) and reject another Promise with the additional Information - maybe with a custom Error Type "MassiveError" (I love that Name) or something.
The basic idea is like:
let pgp = require('pg-promise');
...
select => (table, queryParameters) => {
...
return pgp.any(`select * from foo where table='bar' AND id > 20`)
.then(data => Promise.resolve(data)) // bubble up the original data
.catch(error => {
return Promise.reject({ // Add some context information to the original pgp error
table,
queryParameters,
pgp-error: error
});
};
...
}
I'd try to make a pull request, but I'm not deep enough in the massive code yet. Also, maybe you have some additional suggestions on how we could improve the error handling to get some context to the pgp-error.
Best Regards Marcel