-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrecover.go
More file actions
43 lines (34 loc) · 835 Bytes
/
Copy pathrecover.go
File metadata and controls
43 lines (34 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package runnable
import (
"context"
"fmt"
)
type PanicError struct {
value any
}
func (e *PanicError) Error() string {
return fmt.Sprintf("runnable panicked: %s", e.value)
}
func (e *PanicError) Unwrap() error {
if err, ok := e.value.(error); ok {
return err
}
return nil
}
// Recover returns a runnable that recovers when a runnable panics and return an error to represent this panic.
func Recover(runnable Runnable) Runnable {
return &recoverRunner{"recover/" + runnableName(runnable), runnable}
}
type recoverRunner struct {
name string
runnable Runnable
}
func (r *recoverRunner) runnableName() string { return r.name }
func (r *recoverRunner) Run(ctx context.Context) (err error) {
defer func() {
if value := recover(); value != nil {
err = &PanicError{value}
}
}()
return r.runnable.Run(ctx)
}