Skip to content

Commit

Permalink
refactor(logic)!: adopt new exposed error terms from ichiban/prolog
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Feb 19, 2024
1 parent dbb408b commit fd4a231
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 56 deletions.
4 changes: 2 additions & 2 deletions x/logic/predicate/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func getBech32(env *engine.Env, account engine.Term) (sdk.AccAddress, error) {
case engine.Atom:
addr, err := sdk.AccAddressFromBech32(acc.String())
if err != nil {
return nil, prolog.WithError(prolog.ResourceError(prolog.ResourceModule("bank"), env), err, env)
return nil, prolog.WithError(engine.ResourceError(prolog.ResourceModule("bank"), env), err, env)
}
return addr, nil
default:
Expand Down Expand Up @@ -155,7 +155,7 @@ func fetchBalances(
address := balance.Address
bech32Addr, err = sdk.AccAddressFromBech32(address)
if err != nil {
return engine.Error(prolog.WithError(prolog.ResourceError(prolog.ResourceModule("bank"), env), err, env))
return engine.Error(prolog.WithError(engine.ResourceError(prolog.ResourceModule("bank"), env), err, env))
}
coins := coinsFn(sdkContext, bankKeeper, balance.Coins, bech32Addr)

Expand Down
6 changes: 3 additions & 3 deletions x/logic/predicate/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestBank(t *testing.T) {
balances: []bank.Balance{},
query: `bank_balances('foo', X).`,
wantResult: []testutil.TermResults{{"X": "[uknow-100]"}},
wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],unknown)",
wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],bank_balances/2)",
strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")),
},
{
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestBank(t *testing.T) {
spendableCoins: []bank.Balance{},
query: `bank_spendable_balances('foo', X).`,
wantResult: []testutil.TermResults{{"X": "[uknow-100]"}},
wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],unknown)",
wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],bank_spendable_balances/2)",
strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")),
},

Expand Down Expand Up @@ -421,7 +421,7 @@ func TestBank(t *testing.T) {
lockedCoins: []bank.Balance{},
query: `bank_locked_balances('foo', X).`,
wantResult: []testutil.TermResults{{"X": "[uknow-100]"}},
wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],unknown)",
wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],bank_locked_balances/2)",
strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")),
},
}
Expand Down
4 changes: 2 additions & 2 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func CryptoDataHash(

result, err := util.Hash(algorithm, decodedData)
if err != nil {
return engine.Error(prolog.SyntaxError(err, env))
return engine.Error(engine.SyntaxError(prolog.ErrorTerm(err), env))
}

return engine.Unify(vm, hash, prolog.BytesToByteListTerm(result), cont, env)
Expand Down Expand Up @@ -196,7 +196,7 @@ func xVerify(key, data, sig, options engine.Term, defaultAlgo util.KeyAlg,

r, err := util.VerifySignature(keyAlgo, decodedKey, decodedData, decodedSignature)
if err != nil {
return engine.Error(prolog.SyntaxError(err, env))
return engine.Error(engine.SyntaxError(prolog.ErrorTerm(err), env))
}

if !r {
Expand Down
4 changes: 2 additions & 2 deletions x/logic/predicate/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestXVerify(t *testing.T) {
eddsa_verify(PubKey, Msg, Sig, encoding(octet)).`,
query: `verify.`,
wantSuccess: false,
wantError: fmt.Errorf("error(syntax_error([%s]),unknown)",
wantError: fmt.Errorf("error(syntax_error([%s]),eddsa_verify/4)",
strings.Join(strings.Split("ed25519: bad public key length: 33", ""), ",")),
},
{ // Wrong signature
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestXVerify(t *testing.T) {
ecdsa_verify(PubKey, Msg, Sig, encoding(octet)).`,
query: `verify.`,
wantSuccess: false,
wantError: fmt.Errorf("error(syntax_error([%s]),unknown)",
wantError: fmt.Errorf("error(syntax_error([%s]),ecdsa_verify/4)",
strings.Join(strings.Split("failed to parse compressed public key (first 10 bytes): 0213c8426be471e55506", ""), ",")),
},
{ // Unsupported algo
Expand Down
4 changes: 2 additions & 2 deletions x/logic/predicate/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ func Open(vm *engine.VM, sourceSink, mode, stream, options engine.Term, k engine
}

if streamMode != ioModeRead {
return engine.Error(prolog.PermissionError(prolog.AtomOperationInput, prolog.AtomPermissionTypeStream, sourceSink, env))
return engine.Error(engine.PermissionError(prolog.AtomOperationInput, prolog.AtomPermissionTypeStream, sourceSink, env))
}

f, err := vm.FS.Open(name)
if err != nil {
return engine.Error(prolog.ExistenceError(prolog.AtomObjectTypeSourceSink, sourceSink, env))
return engine.Error(engine.ExistenceError(prolog.AtomObjectTypeSourceSink, sourceSink, env))
}
s := engine.NewInputTextStream(f)

Expand Down
6 changes: 3 additions & 3 deletions x/logic/predicate/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestOpen(t *testing.T) {
},
program: "get_first_char(C) :- open(my_file, write, Stream, _), get_char(Stream, C).",
query: `get_first_char(C).`,
wantError: fmt.Errorf("error(permission_error(input,stream,my_file),unknown)"),
wantError: fmt.Errorf("error(permission_error(input,stream,my_file),open/4)"),
wantSuccess: false,
},
{
Expand All @@ -283,7 +283,7 @@ func TestOpen(t *testing.T) {
},
program: "get_first_char(C) :- open(my_file, append, Stream, _), get_char(Stream, C).",
query: `get_first_char(C).`,
wantError: fmt.Errorf("error(permission_error(input,stream,my_file),unknown)"),
wantError: fmt.Errorf("error(permission_error(input,stream,my_file),open/4)"),
wantSuccess: false,
},
{
Expand All @@ -292,7 +292,7 @@ func TestOpen(t *testing.T) {
},
program: "get_first_char(C) :- open(file2, read, Stream, _), get_char(Stream, C).",
query: `get_first_char(C).`,
wantError: fmt.Errorf("error(existence_error(source_sink,file2),unknown)"),
wantError: fmt.Errorf("error(existence_error(source_sink,file2),open/4)"),
wantSuccess: false,
},
{
Expand Down
4 changes: 2 additions & 2 deletions x/logic/predicate/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func ReadString(vm *engine.VM, stream, length, result engine.Term, cont engine.C
if errors.Is(err, io.EOF) || totalLen >= maxLength {
break
}
return engine.Error(prolog.SyntaxError(err, env))
return engine.Error(engine.SyntaxError(prolog.ErrorTerm(err), env))
}
totalLen += uint64(l)
_, err = builder.WriteRune(r)
if err != nil {
return engine.Error(prolog.SyntaxError(err, env))
return engine.Error(engine.SyntaxError(prolog.ErrorTerm(err), env))
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/logic/prolog/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func UnwrapSDKContext(ctx context.Context, env *engine.Env) (sdk.Context, error)
return sdkCtx, nil
}

return sdk.Context{}, ResourceError(ResourceContext(), env)
return sdk.Context{}, engine.ResourceError(ResourceContext(), env)
}
47 changes: 8 additions & 39 deletions x/logic/prolog/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package prolog

import "github.com/ichiban/prolog/engine"
import (
"github.com/ichiban/prolog/engine"
)

var (
// AtomTypeAtom is the term used to represent the atom type.
Expand Down Expand Up @@ -68,15 +70,6 @@ func ValidEmptyList() engine.Term {
return AtomValidEmptyList
}

// ResourceError creates a new resource error exception.
// TODO: to remove once engine.resourceError() is public.
func ResourceError(resource engine.Term, env *engine.Env) engine.Exception {
return engine.NewException(
AtomError.Apply(
engine.NewAtom("resource_error").Apply(resource),
engine.NewAtom("unknown")), env)
}

var (
// AtomResourceContext is the atom denoting the "context" resource.
// The context resource is a contextual data that contains all information needed to
Expand All @@ -96,41 +89,17 @@ func ResourceModule(module string) engine.Term {
return AtomResourceModule.Apply(engine.NewAtom(module))
}

// PermissionError creates a new permission error exception.
// TODO: to remove once engine.permissionError() is public.
func PermissionError(operation, permissionType, culprit engine.Term, env *engine.Env) engine.Exception {
return engine.NewException(
AtomError.Apply(
engine.NewAtom("permission_error").Apply(
operation, permissionType, culprit,
),
engine.NewAtom("unknown")), env)
}

var AtomOperationInput = engine.NewAtom("input")

var AtomPermissionTypeStream = engine.NewAtom("stream")

// ExistenceError creates a new existence error exception.
// TODO: to remove once engine.existenceError() is public.
func ExistenceError(objectType, culprit engine.Term, env *engine.Env) engine.Exception {
return engine.NewException(
AtomError.Apply(
engine.NewAtom("existence_error").Apply(
objectType, culprit,
),
engine.NewAtom("unknown")), env)
}

var AtomObjectTypeSourceSink = engine.NewAtom("source_sink")

// UnexpectedError creates a new unexpected error exception.
// TODO: to remove once engine.syntaxError() is public.
func SyntaxError(err error, env *engine.Env) engine.Exception {
return engine.NewException(
AtomError.Apply(
engine.NewAtom("syntax_error").Apply(StringToCharacterListTerm(err.Error())),
engine.NewAtom("unknown")), env)
// ErrorTerm returns a term representing the given error, suitable for use in the
// syntax_error/2 predicate.
// TODO: to be improved with specific error types.
func ErrorTerm(err error) engine.Term {
return StringToCharacterListTerm(err.Error())
}

// WithError adds the error term to the exception term if possible.
Expand Down

0 comments on commit fd4a231

Please sign in to comment.