Skip to content

Descriptive body for errors #185

Open

Description

@twiddler

I am trying to catch exceptions when inserting duplicates that violate a UNIQUE constraint. Node prints the exception as

… caused DatabaseError: target: ….-.primary: vttablet: rpc error: code = AlreadyExists desc = Duplicate entry 'foo' for key 'SomeTable.someAttribute' (errno 1062) (sqlstate 23000) (CallerID: …): Sql: "insert into SomeTable(someAttribute) values (:vtg1 /* VARCHAR */)", BindVars: {REDACTED}

console.log(e) gives me

{
  status: 400,
  body: {
    message: 'target: ….-.primary: vttablet: rpc error: code = AlreadyExists desc = Duplicate entry \'foo\' for key \'SomeTable.someAttribute\' (errno 1062) (sqlstate 23000) (CallerID: …): Sql: "insert into SomeTable(`someAttribute`) values (:vtg1 /* VARCHAR */)", BindVars: {REDACTED}',
    code: 'UNKNOWN'
  }
}

I can parse that message with a regex to extract the code and everything, but I think it'd make more sense to have a few more fields that digest that information already (errno would probably be the most helpful, at least for my use case).

One could also take that a step further and introduce more classes that derive

export class DatabaseError extends Error {
but I suspect that might get out of hand quickly. I just checked out the MySQL error reference and it's enormous.

Also code: 'UNKNOWN' seems a bit odd, unless it does not describe any of code, errno or sqlstate.

Activity

  1. twiddler commented on Feb 24, 2025

    @twiddler
    Author

    I'd like to reiterate the necessity for this since parsing the error message is not a reliable solution: Just recently, the error messages changed, and as a consequence my regular expression did not match errno and sqlstate.

    Luckily that code had not hit any production environment yet, but my app is evolving in a way that I will have to handle database errors more and more. I'd highly appreciate a DatabaseError.errno: number field that I could rely on.

    Thank you. 🙏

  2. hiSandog commented on Jul 6, 2026

    @hiSandog

    The report is specific enough that it could probably be turned into a small failing test for “Descriptive body for errors”. I would try to assert the exact behavior around UNIQUE rather than only checking that the command or UI path no longer errors, so the test guards the intended fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions