Skip to content

Commit

Permalink
refactor(logic): update panic_error handling for Prolog upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Nov 26, 2024
1 parent 88cafb5 commit aaa2c18
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions x/logic/util/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"context"
"errors"
"fmt"
"strings"

"github.com/axone-protocol/prolog/v2"
Expand All @@ -20,6 +21,11 @@ const (
defaultEnvCap = uint64(50)
)

var (
errMessageVar = engine.NewVariable()
errPanicError = engine.Atom("error").Apply(engine.AtomPanicError.Apply(errMessageVar))
)

// QueryInterpreter interprets a query and returns the solutions up to the given limit.
//
//nolint:nestif,funlen
Expand Down Expand Up @@ -56,12 +62,14 @@ func QueryInterpreter(
return nil, callErr
}

var panicErr engine.PanicError
if errors.As(callErr, &panicErr) && errors.Is(panicErr.OriginErr, engine.ErrMaxVariables) {
return nil, errorsmod.Wrapf(types.LimitExceeded, panicErr.OriginErr.Error()) //nolint:govet
var err engine.Exception
if errors.As(callErr, &err) {
if err, ok := isPanicError(err.Term(), env); ok {
return nil, errorsmod.Wrapf(types.LimitExceeded, "%s", err)
}

Check warning on line 69 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L65-L69

Added lines #L65 - L69 were not covered by tests
}

if err = func() error {
if err := func() error {

Check warning on line 72 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L72

Added line #L72 was not covered by tests
defer func() {
_ = recover()
}()
Expand Down Expand Up @@ -137,3 +145,12 @@ func isBound(v engine.ParsedVariable, env *engine.Env) bool {

return !ok
}

// isPanicError returns the panic error message if the given term is a panic_error.
func isPanicError(term engine.Term, env *engine.Env) (string, bool) {
if env, ok := env.Unify(term, errPanicError); ok {
return fmt.Sprintf("%s", env.Resolve(errMessageVar)), true
}

Check warning on line 153 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L150-L153

Added lines #L150 - L153 were not covered by tests

return "", false

Check warning on line 155 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L155

Added line #L155 was not covered by tests
}

0 comments on commit aaa2c18

Please sign in to comment.