Skip to content

Commit

Permalink
fix(GODT-2429): Do not transaction fails if context was cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
LBeernaertProton committed Mar 2, 2023
1 parent c0f56b2 commit da3a1cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/ProtonMail/gluon/internal/utils"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -87,10 +88,12 @@ func WriteResult[T any](ctx context.Context, db *DB, fn func(context.Context, *e
}

if err := tx.Commit(); err != nil {
reporter.MessageWithContext(ctx,
"Failed to commit database transaction",
reporter.Context{"error": err},
)
if !errors.Is(err, context.Canceled) {
reporter.MessageWithContext(ctx,
"Failed to commit database transaction",
reporter.Context{"error": err, "type": utils.ErrCause(err)},
)
}

return failResult, fmt.Errorf("committing transaction: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"errors"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -29,3 +30,14 @@ func ShortID(id string) string {

return id[0:l] + "..."
}

// ErrCause returns the cause of the error, the inner-most error in the wrapped chain.
func ErrCause(err error) error {
cause := err

for errors.Unwrap(cause) != nil {
cause = errors.Unwrap(cause)
}

return cause
}

0 comments on commit da3a1cd

Please sign in to comment.