feat(GODT-2500): Add panic handlers everywhere.

This commit is contained in:
Jakub
2023-03-22 17:18:17 +01:00
parent 9f59e61b14
commit ec92c918cd
42 changed files with 283 additions and 130 deletions

View File

@ -29,6 +29,7 @@ import (
"path/filepath"
"sync"
"github.com/ProtonMail/proton-bridge/v3/internal/async"
"github.com/bradenaw/juniper/parallel"
"github.com/bradenaw/juniper/xslices"
"github.com/sirupsen/logrus"
@ -44,10 +45,12 @@ type Vault struct {
ref map[string]int
refLock sync.Mutex
panicHandler async.PanicHandler
}
// New constructs a new encrypted data vault at the given filepath using the given encryption key.
func New(vaultDir, gluonCacheDir string, key []byte) (*Vault, bool, error) {
func New(vaultDir, gluonCacheDir string, key []byte, panicHandler async.PanicHandler) (*Vault, bool, error) {
if err := os.MkdirAll(vaultDir, 0o700); err != nil {
return nil, false, err
}
@ -69,9 +72,17 @@ func New(vaultDir, gluonCacheDir string, key []byte) (*Vault, bool, error) {
return nil, false, err
}
vault.panicHandler = panicHandler
return vault, corrupt, nil
}
func (vault *Vault) handlePanic() {
if vault.panicHandler != nil {
vault.panicHandler.HandlePanic()
}
}
// GetUserIDs returns the user IDs and usernames of all users in the vault.
func (vault *Vault) GetUserIDs() []string {
return xslices.Map(vault.get().Users, func(user UserData) string {
@ -115,6 +126,8 @@ func (vault *Vault) ForUser(parallelism int, fn func(*User) error) error {
userIDs := vault.GetUserIDs()
return parallel.DoContext(context.Background(), parallelism, len(userIDs), func(_ context.Context, idx int) error {
defer vault.handlePanic()
user, err := vault.NewUser(userIDs[idx])
if err != nil {
return err