angu icon indicating copy to clipboard operation
angu copied to clipboard

Helper to return user-friendly errors?

Open cristianoccazinsp opened this issue 2 years ago • 0 comments

Just wondering, what's the best way to return user-friendly errors? The errors are a bit cryptic and sometimes even deeply nested in recursive structures.

For example, this is not enough:

const getErrorLine = (expr, start, end) => {
  let text = expr.slice(start, start + end + 8) || 'char 0';
  return text;
};

const raiseError = (expr, res) => {
  let err = res.value;
  if (err.kind === 'EVAL_THROW') {
    throw new Error(`The code evaluation threw an error`);
  } else if (
    err.kind === 'FUNCTION_NOT_DEFINED' ||
    err.kind === 'NOT_A_FUNCTION'
  ) {
    throw new Error(`Function not defined`);
  } else if (err.pos?.start !== undefined && err.pos?.end !== undefined) {
    let text = getErrorLine(expr, err.pos.start, err.pos.end);
    throw new Error(
      `The code has syntax errors or invalid variable names near: ${text}`
    );
  } else {
    throw new Error(`The code has syntax errors.`);
  }
};

Because for EVAL_THROW, the error is sometimes nested like this:

Screen Shot 2022-07-25 at 15 40 42

cristianoccazinsp avatar Jul 25 '22 18:07 cristianoccazinsp