【问题标题】:How to "throw" custom error messages in elastic's updateByQuery如何在弹性的 updateByQuery 中“抛出”自定义错误消息
【发布时间】:2018-12-21 00:59:29
【问题描述】:

我使用 javascript api 的 updateByQuery 更新我的文档

  const res = await this.elastic.update({
  index: MY_INDEX,
  type: MY_TYPE,
  id: MY_ID,
  _source: true,
  body: {
    script: {
      source: `
        // stuff
      `,
    },
  };
}

如何抛出或设置自定义错误消息,以便在阅读响应时知道它失败的原因?

【问题讨论】:

    标签: node.js elasticsearch elasticjs


    【解决方案1】:

    只需抛出一个异常

    throw new Exception('your custom message here');
    

    轻松脚本中的任何位置。

    然后使用 try catch 捕获错误。

    // body must be in async function to use await + try catch
    const fn = async () => {
      try {
        const res = await this.elastic.update({
        index: MY_INDEX,
        type: MY_TYPE,
        id: MY_ID,
        _source: true,
        body: {
          script: {
            source: `
              // stuff
              throw new Exception('your message here');
              // stuff
          `,
          },
        };
      } catch (e) {
        // logs 'your message here'
        console.log(e.body.error.caused_by.caused_by.reason);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-08
      • 2014-05-03
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      相关资源
      最近更新 更多