Skip to content

Descriptive body for errors #185

Open
@twiddler

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.

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