【问题标题】:How to read error message as a string from error object on Node.js如何从 Node.js 上的错误对象中将错误消息作为字符串读取
【发布时间】:2018-04-17 19:21:11
【问题描述】:

例如我刚刚收到此错误:

{ Error: There was an error sending your trade offer. We were unable to contact the game's item server. The game's item server may be down or Steam may be experiencing temporary connectivity issues. Please try again later.
    at Object.exports.makeAnError (C:\Some\Boring\Route\node_modules\steam-tradeoffer-manager\lib\helpers.js:17:12)
    at SteamCommunity.manager._community.httpRequestPost...

如何获得“发送您的交易报价时出错...”部分,以便稍后将其发送回客户?

【问题讨论】:

  • 试试 JSON.parse()
  • 有点奇怪,你开头有{。如果是典型的 Error 实例,请使用 @Ryan 的解决方案。如果 error.message 出于某种原因包含 JSON 并且 Error 是一个键,则使用JSON.parse(error.message).Error 作为@Avihay 建议提取错误
  • @Dmitry:{ 的格式来自例如console.log('%o', new Error())

标签: javascript node.js error-handling


【解决方案1】:

它在 message 属性中。

let error = new Error('example');
console.log(error.message);

另见the string representation of errors per ECMAScript

【讨论】:

    【解决方案2】:

    我的err.message 为空。我用err.toString() 它救了我的命。

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2011-06-16
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 2013-07-07
      • 2023-02-18
      • 1970-01-01
      • 2011-02-24
      相关资源
      最近更新 更多