How to catch constraint errors?
See original GitHub issueI need to catch only database constraint errors emitted by table.unique(["foo", "bar"]);
entry in the schema.
I can do that now with a really hacky Promise#catch(…) predicate:
function isUniqueConstraintError(err){
if (!err) return false;
var re = /^duplicate key value violates unique constraint/;
return re.test(err.message);
}
MyModel.forge({ foo: "fuz", bar: "fiz" })
.save()
.catch(isUniqueConstraintError, function(err) {
// Handle the error
});
but this is obviously very brittle.
Is there a more robust way to catch this?
Issue Analytics
- State:
- Created 9 years ago
- Reactions:5
- Comments:13 (9 by maintainers)
Top Results From Across the Web
How to handle unique constraint violations
Catch uniqueness exceptions thrown by the database at the lowest level possible — in the UnitOfWork class · Convert them into Result ·...
Read more >best way to catch database constraint errors - Stack Overflow
try { // inset data } catch (SqlException ex) { if (ex.Message.ToLower().Contains("duplicate key")) { if (ex.Message.ToLower().Contains(" ...
Read more >Handling Constraint Violations and Errors in SQL Server
In this article, we're going to take a problem and use it to explore transactions, and constraint violations, before suggesting a solution to ......
Read more >How to handle unique constraint errors - Vladimir Khorikov
Catch unexpected exceptions at the highest level of the call stack and then log them and end the current operation. Here's an article...
Read more >How to capture database unique constraint error in Error ...
I have a requirement to catch the exception when a records fails during insert into a table due to unique key constraint violation...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Error object has code property, so I would better try to match errors by error code. e.g. for postgres https://www.postgresql.org/docs/9.6/errcodes-appendix.html
Did anything ever come of this? It would be really nice to have errors be consistent across database engines.