Просто сгенерируйте исключение с
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);
}
}