Find good way to avoid duplicate key errors
NullEnt1ty opened this issue · 0 comments
NullEnt1ty commented
When you try to insert an already existing user (username
and email
are unique contraints) the database throws an error:
QueryFailedError: Duplicate entry 'my_username' for key 'IDX_78a916df40e02a9deb1c4b75ed'
at new QueryFailedError (/home/dominique/Documents/projects/nestjs-typeorm-example/src/error/QueryFailedError.ts:9:9)
at Query.onResult (/home/dominique/Documents/projects/nestjs-typeorm-example/src/driver/mysql/MysqlQueryRunner.ts:163:37)
at Query.execute (/home/dominique/Documents/projects/nestjs-typeorm-example/node_modules/mysql2/lib/commands/command.js:30:14)
at PoolConnection.handlePacket (/home/dominique/Documents/projects/nestjs-typeorm-example/node_modules/mysql2/lib/connection.js:455:32)
at PacketParser.onPacket (/home/dominique/Documents/projects/nestjs-typeorm-example/node_modules/mysql2/lib/connection.js:73:18)
at PacketParser.executeStart (/home/dominique/Documents/projects/nestjs-typeorm-example/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/dominique/Documents/projects/nestjs-typeorm-example/node_modules/mysql2/lib/connection.js:80:31)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:442:20)
at addChunk (_stream_readable.js:283:12)
The goal ist to find a good way to avoid these errors.
- Query the database before attempting to insert the user?
- Catch the error and try to parse it (that feels shabby)?