0

I update my docs with the javascript api's updateByQuery using

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

How can I throw or set custom error messages such that when I read the response, I know why it failed?

Simon
  • 1,681
  • 1
  • 21
  • 34

1 Answers1

2

Simply throw a an exception with

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

anywhere in the painless script.

Then capture the error with a 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);
  }
}
Simon
  • 1,681
  • 1
  • 21
  • 34