如何在弹性的updateByQuery中“引发”自定义错误消息

时间:2018-12-21 00:59:30

标签: node.js elasticsearch elasticjs

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

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

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

1 个答案:

答案 0 :(得分:0)

只需引发一个异常

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);
  }
}