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

Descriptive body for errors #185

Open
twiddler opened this issue Nov 18, 2024 · 1 comment
Open

Descriptive body for errors #185

twiddler opened this issue Nov 18, 2024 · 1 comment

Comments

@twiddler
Copy link

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.

@twiddler
Copy link
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. 🙏

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