question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to catch constraint errors?

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Reactions:5
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
vishchivcommented, Sep 27, 2019

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

3reactions
hasahmedcommented, Feb 7, 2019

Did anything ever come of this? It would be really nice to have errors be consistent across database engines.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found