This is a standard PostgreSQL error view that has an invisible message property, so calling error.message will give you the expected result.
It is best to log your errors as follows:
console.log(error.message || error);
Extending the code of your code ...
It looks like your error context comes from the result of calling the batch function. This means that in this context, you can also call error.getErrors()[0] to get the very first error found.
Thus, for your specific case, safe error logging will be:
.catch(error => { if (Array.isArray(error) && 'getErrors' in error) {
And of course, you can easily modify it to record all errors returned by the batch method.
This question gave me an idea of ββadding the message property to the result of the rejection. There is always room for improvement;)
UPDATE
After that I updated spex.batch rejected the implementation to support the first and message properties, to simplify error handling and released it as version 0.4.3 . The implementation is even better than I originally planned, because first and message support nested batch results.
If you upgrade pg-promise to version 4.1.10 (or later), you can log such errors in general:
.catch(error => { console.log("ERROR:", error.message || error); });
It always logs the correct error message, even if the error comes from a batch nested call.