Skip to content

Commit

Permalink
remove bool return from handlers, use variable to control crash
Browse files Browse the repository at this point in the history
  • Loading branch information
jmickey committed Mar 2, 2023
1 parent 97ebc22 commit 494a048
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
2 changes: 0 additions & 2 deletions cmds/ocm/commands/common/options/closureoption/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer/transferhandler"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer/transferhandler/standard"
"github.com/open-component-model/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/utils/panics"
)

func From(o options.OptionSetProvider) *Option {
Expand All @@ -35,7 +34,6 @@ type Option struct {
}

func New(elemname string, settings ...interface{}) *Option {
defer panics.HandlePanic()
o := &Option{ElementName: elemname, AddReferencePath: options.Always()}
for _, s := range settings {
switch v := s.(type) {
Expand Down
16 changes: 7 additions & 9 deletions pkg/utils/panics/panics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import (
"github.com/open-component-model/ocm/pkg/logging"
)

type PanicHandler func(interface{}) bool
var ReallyCrash = true

type PanicHandler func(interface{})

var PanicHandlers = []PanicHandler{logHandler}

func HandlePanic(additionalHandlers ...PanicHandler) {
reallyCrash := true

if r := recover(); r != nil {
for _, fn := range PanicHandlers {
reallyCrash = fn(r)
fn(r)
}

for _, fn := range additionalHandlers {
reallyCrash = fn(r)
fn(r)
}

if reallyCrash {
if ReallyCrash {
panic(r)
}
}
Expand All @@ -37,7 +37,7 @@ func RegisterPanicHandler(handler PanicHandler) {
PanicHandlers = append(PanicHandlers, handler)
}

func logHandler(r interface{}) bool {
func logHandler(r interface{}) {
// Same as stdlib http server code. Manually allocate stack trace buffer size
// to prevent excessively large logs
const size = 64 << 10
Expand All @@ -48,6 +48,4 @@ func logHandler(r interface{}) bool {
} else {
logging.Logger().Error(fmt.Sprintf("Observed a panic: %#v (%v)\n%s", r, r, stacktrace))
}

return true
}

0 comments on commit 494a048

Please sign in to comment.