GORM errors are not checked for #79

Closed
opened 2025-12-29 01:21:52 +01:00 by adam · 1 comment
Owner

Originally created by @enoperm on GitHub (Nov 21, 2021).

While working on a local copy of this repository, I have noticed that pretty much none of the db.Save calls return any errors, which looked strange to me. As it turns out, errors are actually returned, but not in a conventional way: the returned db instances each have an Error field, that may contain any errors from the transaction that created them. I think this is a bit of a footgun, at it is probably a good idea to at least log any database errors, just in case.

So checking for errors would look like something like this:

if err := h.db.Save(&machine).Error; err != nil {
    // handle err
}
Originally created by @enoperm on GitHub (Nov 21, 2021). While working on a local copy of this repository, I have noticed that pretty much none of the `db.Save` calls return any `error`s, which looked strange to me. As it turns out, errors *are* actually returned, but not in a conventional way: the returned `db` instances each have an `Error` field, that may contain any errors from the transaction that created them. I think this is a bit of a footgun, at it is probably a good idea to at least log any database errors, just in case. So checking for errors would look like something like this: ```go if err := h.db.Save(&machine).Error; err != nil { // handle err } ```
adam closed this issue 2025-12-29 01:21:52 +01:00
Author
Owner

@kradalby commented on GitHub (Nov 21, 2021):

Ye I had a feeling about this. I have some relatively comprehensive db refactoring in the plans so I'll take this into account aswell, thanks.

@kradalby commented on GitHub (Nov 21, 2021): Ye I had a feeling about this. I have some relatively comprehensive db refactoring in the plans so I'll take this into account aswell, thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/headscale#79