NodeJS和pg-promise,捕获PostgreSQL异常

时间:2021-10-11 15:37:04

I'm running NodeJS and pg-promise with a PostgreSQL backend. I've created my own TRIGGER which throws an exception in some cases. Things work fine at this end.

我正在使用PostgreSQL后端运行NodeJS和pg-promise。我创建了自己的TRIGGER,在某些情况下引发异常。事情在这方面很好。

But with pg-promise I have trouble catching the error's name.

但是使用pg-promise我无法捕获错误的名称。

Using this code:

使用此代码:

...
.catch(function(err) {
    console.log(err);
});

I'm getting the following output:

我得到以下输出:

[ { success: false,
result: 
 { [error: vote_limit_exceeded]
   name: 'error',
   length: 80,
   severity: 'ERROR',
   code: 'P0001',
   detail: undefined,
   hint: undefined,
   position: undefined,
   internalPosition: undefined,
   internalQuery: undefined,
   where: undefined,
   schema: undefined,
   table: undefined,
   column: undefined,
   dataType: undefined,
   constraint: undefined,
   file: 'pl_exec.c',
   line: '3068',
   routine: 'exec_stmt_raise' } } ]

I can see the name 'vote_limit_exceeded' in the context, but how do I return as a text string?

我可以在上下文中看到名称'vote_limit_exceeded',但是如何作为文本字符串返回?

I've tried getting "close" with:

我试过“关闭”:

console.log(err[0].result);

But I'm not able to get the 'vote_limit_exceeded' isolated.

但我无法将'vote_limit_exceeded'隔离开来。

1 个解决方案

#1


5  

This is the standard error presentation by PostgreSQL, which has invisible property message, so calling error.message will give you the expected result.

这是PostgreSQL的标准错误表示,它具有不可见的属性消息,因此调用error.message将为您提供预期的结果。

Best yet is to log your errors like this:

最好的是记录你的错误,如下所示:

console.log(error.message || error);

Extending on your code example...

扩展您的代码示例...

It looks like your error context comes from the result of calling batch function. That means in such context you can also call error.getErrors()[0] to get the very first error found.

看起来您的错误上下文来自调用批处理函数的结果。这意味着在这样的上下文中,您还可以调用error.getErrors()[0]来获取找到的第一个错误。

So for your specific case, a safe error logging would be:

因此,对于您的具体情况,安全错误记录将是:

.catch(error => {
    if (Array.isArray(error) && 'getErrors' in error) {
        // the error came from method `batch`;
        // let's log the very first error:
        error = error.getErrors()[0];
    }
    console.log("ERROR:", error.message || error);
});

And of course you can easily change it to log all errors returned by method batch instead.

当然,您可以轻松更改它以记录方法批处理返回的所有错误。

This question gave me an idea about adding property message to the reject result. There is always a room for improvement ;)

这个问题给了我一个关于向拒绝结果添加属性消息的想法。总有改进的余地;)

UPDATE

Following this, I updated spex.batch reject implementation to support properties first and message, for easier error handling, and released it as version 0.4.3. The implementation is even better than I initially intended, because both first and message support nested batch results.

在此之后,我更新了spex.batch拒绝实现以首先支持属性和消息,以便更容易地进行错误处理,并将其作为版本0.4.3发布。实现甚至比我最初的预期更好,因为first和message都支持嵌套的批处理结果。

If you update pg-promise to version 4.1.10 (or later), then you can log such errors in a generic way:

如果将pg-promise更新为4.1.10(或更高版本),则可以通用方式记录此类错误:

.catch(error => {
    console.log("ERROR:", error.message || error);
});

It will always log the correct error message, even when the error comes from a nested batch call.

它将始终记录正确的错误消息,即使错误来自嵌套批处理调用。

#1


5  

This is the standard error presentation by PostgreSQL, which has invisible property message, so calling error.message will give you the expected result.

这是PostgreSQL的标准错误表示,它具有不可见的属性消息,因此调用error.message将为您提供预期的结果。

Best yet is to log your errors like this:

最好的是记录你的错误,如下所示:

console.log(error.message || error);

Extending on your code example...

扩展您的代码示例...

It looks like your error context comes from the result of calling batch function. That means in such context you can also call error.getErrors()[0] to get the very first error found.

看起来您的错误上下文来自调用批处理函数的结果。这意味着在这样的上下文中,您还可以调用error.getErrors()[0]来获取找到的第一个错误。

So for your specific case, a safe error logging would be:

因此,对于您的具体情况,安全错误记录将是:

.catch(error => {
    if (Array.isArray(error) && 'getErrors' in error) {
        // the error came from method `batch`;
        // let's log the very first error:
        error = error.getErrors()[0];
    }
    console.log("ERROR:", error.message || error);
});

And of course you can easily change it to log all errors returned by method batch instead.

当然,您可以轻松更改它以记录方法批处理返回的所有错误。

This question gave me an idea about adding property message to the reject result. There is always a room for improvement ;)

这个问题给了我一个关于向拒绝结果添加属性消息的想法。总有改进的余地;)

UPDATE

Following this, I updated spex.batch reject implementation to support properties first and message, for easier error handling, and released it as version 0.4.3. The implementation is even better than I initially intended, because both first and message support nested batch results.

在此之后,我更新了spex.batch拒绝实现以首先支持属性和消息,以便更容易地进行错误处理,并将其作为版本0.4.3发布。实现甚至比我最初的预期更好,因为first和message都支持嵌套的批处理结果。

If you update pg-promise to version 4.1.10 (or later), then you can log such errors in a generic way:

如果将pg-promise更新为4.1.10(或更高版本),则可以通用方式记录此类错误:

.catch(error => {
    console.log("ERROR:", error.message || error);
});

It will always log the correct error message, even when the error comes from a nested batch call.

它将始终记录正确的错误消息,即使错误来自嵌套批处理调用。