rayman1104 / ra-data-nestjsx-crud

Data provider which integrates React Admin with NestJS CRUD library
MIT License
100 stars 27 forks source link

How to catch http errors from REST api? #49

Open rfamans opened 9 months ago

rfamans commented 9 months ago

I am using nest.js a crud REST API resource. When I delete a record from react-admin UI, the DELETE method of the nestjs crud resource is called via my ra-data-nestjsx-crud dataprovider of my react admin app. This delete action however fails because it violates a foreign key constraint in the underlying mysql database. The nestjs API server throws a server error (httpcode 500):

[Nest] 4510 - 11/27/2023, 8:44:54 PM ERROR [ExceptionsHandler] Cannot delete or update a parent row: a foreign key constraint fails (mydb.mytable, CONSTRAINT my_other_table_ibfk_5 FOREIGN KEY (my_id) REFERENCES my_table (id)) QueryFailedError: Cannot delete or update a parent row: a foreign key constraint fails (mydb.my_other_table, CONSTRAINT my_other_table_ibfk_5 FOREIGN KEY (my_id) REFERENCES my_table (id)) at Query.onResult (/volume1/web/my-api/src/driver/mysql/MysqlQueryRunner.ts:222:33) at Query.execute (/volume1/web/my-api/node_modules/mysql2/lib/commands/command.js:36:14) at PoolConnection.handlePacket (/volume1/web/my-api/node_modules/mysql2/lib/connection.js:456:32) at PacketParser.onPacket (/volume1/web/my-api/node_modules/mysql2/lib/connection.js:85:12) at PacketParser.executeStart (/volume1/web/my-api/node_modules/mysql2/lib/packet_parser.js:75:16) at Socket. (/volume1/web/my-api/node_modules/mysql2/lib/connection.js:92:25) at Socket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) at readableAddChunk (node:internal/streams/readable:297:9) at Socket.Readable.push (node:internal/streams/readable:234:10)

I want to catch this error in react-admin and present a more explanatory error message to the user. How can I catch this error in my react admin app that users an ra-data-nestjsx-crud dataprovider?