Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper to return user-friendly errors? #12

Open
cristianoccazinsp opened this issue Jul 25, 2022 · 0 comments
Open

Helper to return user-friendly errors? #12

cristianoccazinsp opened this issue Jul 25, 2022 · 0 comments

Comments

@cristianoccazinsp
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant