mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-15 14:56:42 +00:00
GODT-1657: More stable sync, with some tests
This commit is contained in:
49
internal/try/try.go
Normal file
49
internal/try/try.go
Normal file
@ -0,0 +1,49 @@
|
||||
package try
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Catch tries to execute the `try` function, and if it fails or panics,
|
||||
// it executes the `handlers` functions in order.
|
||||
func Catch(try func() error, handlers ...func() error) error {
|
||||
if _, err := CatchVal(func() (any, error) { return nil, try() }, handlers...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CatchVal tries to execute the `try` function, and if it fails or panics,
|
||||
// it executes the `handlers` functions in order.
|
||||
func CatchVal[T any](try func() (T, error), handlers ...func() error) (res T, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
catch(handlers...)
|
||||
err = fmt.Errorf("panic: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
if res, err = try(); err != nil {
|
||||
catch(handlers...)
|
||||
return res, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func catch(handlers ...func() error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logrus.WithField("panic", r).Error("Panic in catch")
|
||||
}
|
||||
}()
|
||||
|
||||
for _, handler := range handlers {
|
||||
if err := handler(); err != nil {
|
||||
logrus.WithError(err).Error("Failed to handle error")
|
||||
}
|
||||
}
|
||||
}
|
||||
74
internal/try/try_test.go
Normal file
74
internal/try/try_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
package try
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTry(t *testing.T) {
|
||||
res, err := CatchVal(func() (string, error) {
|
||||
return "foo", nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "foo", res)
|
||||
}
|
||||
|
||||
func TestTryCatch(t *testing.T) {
|
||||
tryErr := fmt.Errorf("oops")
|
||||
|
||||
res, err := CatchVal(
|
||||
func() (string, error) {
|
||||
return "", tryErr
|
||||
},
|
||||
func() error {
|
||||
return nil
|
||||
},
|
||||
)
|
||||
require.ErrorIs(t, err, tryErr)
|
||||
require.Zero(t, res)
|
||||
}
|
||||
|
||||
func TestTryCatchError(t *testing.T) {
|
||||
tryErr := fmt.Errorf("oops")
|
||||
|
||||
res, err := CatchVal(
|
||||
func() (string, error) {
|
||||
return "", tryErr
|
||||
},
|
||||
func() error {
|
||||
return fmt.Errorf("catch error")
|
||||
},
|
||||
)
|
||||
require.ErrorIs(t, err, tryErr)
|
||||
require.Zero(t, res)
|
||||
}
|
||||
|
||||
func TestTryPanic(t *testing.T) {
|
||||
res, err := CatchVal(
|
||||
func() (string, error) {
|
||||
panic("oops")
|
||||
},
|
||||
func() error {
|
||||
return nil
|
||||
},
|
||||
)
|
||||
require.ErrorContains(t, err, "panic: oops")
|
||||
require.Zero(t, res)
|
||||
}
|
||||
|
||||
func TestTryCatchPanic(t *testing.T) {
|
||||
tryErr := fmt.Errorf("oops")
|
||||
|
||||
res, err := CatchVal(
|
||||
func() (string, error) {
|
||||
return "", tryErr
|
||||
},
|
||||
func() error {
|
||||
panic("oops")
|
||||
},
|
||||
)
|
||||
require.ErrorIs(t, err, tryErr)
|
||||
require.Zero(t, res)
|
||||
}
|
||||
Reference in New Issue
Block a user