Description
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
Line 15 in f62005c
Also code: 'UNKNOWN'
seems a bit odd, unless it does not describe any of code
, errno
or sqlstate
.