planetscale / database-js

A Fetch API-compatible PlanetScale database driver
https://planetscale.com/docs/tutorials/planetscale-serverless-driver
Apache License 2.0
1.17k stars 35 forks source link

Error handling #103

Closed Allyedge closed 1 year ago

Allyedge commented 1 year ago
[DatabaseError: target: reanvue.-.primary: vttablet: rpc error: code = AlreadyExists desc = Duplicate entry 'user_2O3GkFE9ev2C2REVYxmxqyVf13P-user_2O3pPzMN7jALRmhpGI1yEWyhjI' for key 'Connection.Connection_senderId_receiverId_key' (errno 1062) (sqlstate 23000) (CallerID: 211kmwyh6r7bw465wddi): Sql: "insert into `Connection`(senderId, receiverId) values (:vtg1, :vtg2)", BindVars: {REDACTED}] {
  status: 400,
  name: 'DatabaseError',
  body: {
  message: 'target: reanvue.-.primary: vttablet: rpc error: code = AlreadyExists desc = Duplicate entry \'user_2O3GkFE9ev2C2REVYxmxqyVf13P-user_2O3pPzMN7jALRmhpGI1yEWyhjI\' for key \'Connection.Connection_senderId_receiverId_key\' (errno 1062) (sqlstate 23000) (CallerID: 211kmwyh6r7bw465wddi): Sql: "insert into `Connection`(senderId, receiverId) values (:vtg1, :vtg2)", BindVars: {REDACTED}',
  code: 'UNKNOWN'
}
}

I am getting this error from my database because I have an unique constraint set up, which is okay.

But is there a way I can perhaps extract the error code AlreadyExists from it to send back a different response to my client?

Thanks!

AlecKriebel commented 1 year ago

@Allyedge any tips on how you ended up doing error handling?

I'm trying to parse out a unique constraint failing. Thanks!

Allyedge commented 1 year ago

@Allyedge any tips on how you ended up doing error handling?

I'm trying to parse out a unique constraint failing. Thanks!

I honestly didn't use it at the end, couldn't really find out a way so I used something else instead.