Gorm: Static version of RecordNotFound()

Created on 22 Dec 2017  路  1Comment  路  Source: go-gorm/gorm

Currently we have to use RecordNotFound in chained mode: db.Find(...).RecordNotFound().

This is not very robust if you also want to handle other errors.
You have to do some form of this:

db := db.Find(...)
if db.RecordNotFound() {
    // ...
    return
}
if db.Error != nil {
    // ...
    return
}
// ...

Can we instead have something like this:

if err := db.Find(...).Error; err != nil {
  if gorm.RecordNotFound(err) {
    // ...
  } else {
    // ...
  }
  return
}

The proposed code allows for passing errors up the stack and checking them there:

a := func() error {
    return db.Find(...).Error
}

if err := a(); err != nil {
  if gorm.RecordNotFound(err) {
    // ...
  } else {
    // ...
  }
  return
}
// ...

Most helpful comment

Added method gorm.IsRecordNotFoundError

Thank you for your suggestion.

>All comments

Added method gorm.IsRecordNotFoundError

Thank you for your suggestion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

izouxv picture izouxv  路  3Comments

satb picture satb  路  3Comments

corvinusy picture corvinusy  路  3Comments

zeropool picture zeropool  路  3Comments

Quentin-M picture Quentin-M  路  3Comments