Compare commits
No commits in common. "735d314a19cbef0ef8531cf8febbf546e4d6f8ff" and "461d0b77e9d2e161f7e177f096208853da6c031b" have entirely different histories.
735d314a19
...
461d0b77e9
@ -1,35 +0,0 @@
|
||||
// Example panic demonstrates how the environment can restart actors if they
|
||||
// fail.
|
||||
package main
|
||||
|
||||
import "log"
|
||||
import "time"
|
||||
import "errors"
|
||||
import "context"
|
||||
import "math/rand"
|
||||
import "git.tebibyte.media/sashakoshka/camfish"
|
||||
|
||||
func main() {
|
||||
camfish.Run("panic",
|
||||
"Example panic demonstrates how the environment can restart " +
|
||||
"actors if they fail",
|
||||
new(actor))
|
||||
}
|
||||
|
||||
// actor is an incorrectly implemented actor that panics and errs randomly.
|
||||
type actor struct { }
|
||||
var _ camfish.Runnable = new(actor)
|
||||
func (this *actor) Type() string { return "panic" }
|
||||
|
||||
func (this *actor) Run(ctx context.Context) error {
|
||||
log.Println("(i) [panic] panicking in 10 seconds")
|
||||
select {
|
||||
case <- ctx.Done(): return ctx.Err()
|
||||
case <- time.After(time.Second * 10):
|
||||
if rand.Int() % 2 == 0 {
|
||||
panic("this is a panic")
|
||||
} else {
|
||||
return errors.New("this is an error")
|
||||
}
|
||||
}
|
||||
}
|
25
util.go
25
util.go
@ -12,19 +12,6 @@ import "strings"
|
||||
import "context"
|
||||
import "sync/atomic"
|
||||
import "unicode/utf8"
|
||||
import "runtime/debug"
|
||||
|
||||
func panicErr(message any, stack []byte) (err error) {
|
||||
if panErr, ok := message.(error); ok {
|
||||
err = panErr
|
||||
} else {
|
||||
err = errors.New(fmt.Sprint(message))
|
||||
}
|
||||
if stack != nil {
|
||||
err = fmt.Errorf("%w: %s", err, stack)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func defaul[T comparable](value, def T) T {
|
||||
var zero T
|
||||
@ -35,7 +22,11 @@ func defaul[T comparable](value, def T) T {
|
||||
func panicWrap(f func() error) (err error) {
|
||||
defer func () {
|
||||
if pan := recover(); pan != nil {
|
||||
err = panicErr(pan, debug.Stack())
|
||||
if panErr, ok := pan.(error); ok {
|
||||
err = panErr
|
||||
} else {
|
||||
err = errors.New(fmt.Sprint(pan))
|
||||
}
|
||||
}
|
||||
} ()
|
||||
|
||||
@ -46,7 +37,11 @@ func panicWrap(f func() error) (err error) {
|
||||
func panicWrapCtx(ctx context.Context, f func(context.Context) error) (err error) {
|
||||
defer func () {
|
||||
if pan := recover(); pan != nil {
|
||||
err = panicErr(pan, debug.Stack())
|
||||
if panErr, ok := pan.(error); ok {
|
||||
err = panErr
|
||||
} else {
|
||||
err = errors.New(fmt.Sprint(pan))
|
||||
}
|
||||
}
|
||||
} ()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user