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

Feature request: highlight the SQL in the error message #14

Open
capaj opened this issue Mar 7, 2020 · 0 comments
Open

Feature request: highlight the SQL in the error message #14

capaj opened this issue Mar 7, 2020 · 0 comments

Comments

@capaj
Copy link

capaj commented Mar 7, 2020

I think it would be really cool, if the error message would be colored, so it can be read a bit better in the terminal.
I have a proof of concept running, and the output in Jest looks like this:

  • for syntax error

image

  • for prepared statement parameter count mismatch:
    image

which is night and day compared to the current messages without any highlighting.
My implementation is super naive based on Vincit/objection.js#1621 (comment)

  async execute() {
    let p
    try {
      p = await super.execute()
    } catch (err) {
      if (this._asyncStack) {
        const originalStack = err.stack.split('\n').slice(7)

        const newStack: string = originalStack
          .concat(this._asyncStack)
          .join('\n')

        const nativeErrPosition = Number(err.nativeError?.position)
        if (nativeErrPosition) {
          const offset = -1
          const { message } = err
          const beforeSyntaxErr = highlight(
            message.substring(0, nativeErrPosition + offset),
            {
              language: 'sql',
              ignoreIllegals: true
            }
          )
          const withSytaxErrorHighlighted =
            beforeSyntaxErr +
            chalk.blue.bgRed.bold(
              message.substring(
                nativeErrPosition + offset,
                nativeErrPosition + offset + 1
              )
            ) +
            chalk.yellow(
              message.substring(nativeErrPosition + offset + 1, message.length)
            )
          err.message = withSytaxErrorHighlighted
        } else if (err instanceof DBError) {
          err.message = highlight(err.message, {
            language: 'sql',
            ignoreIllegals: true
          })
        }
        err.stack = newStack
      }
      throw err
    }
    return p
  }
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