From fd4a231685a98a7c90f889a981179c6d25acc796 Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 19 Feb 2024 18:31:59 +0100 Subject: [PATCH] refactor(logic)!: adopt new exposed error terms from ichiban/prolog --- x/logic/predicate/bank.go | 4 +-- x/logic/predicate/bank_test.go | 6 ++-- x/logic/predicate/crypto.go | 4 +-- x/logic/predicate/crypto_test.go | 4 +-- x/logic/predicate/file.go | 4 +-- x/logic/predicate/file_test.go | 6 ++-- x/logic/predicate/string.go | 4 +-- x/logic/prolog/context.go | 2 +- x/logic/prolog/error.go | 47 ++++++-------------------------- 9 files changed, 25 insertions(+), 56 deletions(-) diff --git a/x/logic/predicate/bank.go b/x/logic/predicate/bank.go index 8cccfc33..d8cc1717 100644 --- a/x/logic/predicate/bank.go +++ b/x/logic/predicate/bank.go @@ -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: @@ -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) diff --git a/x/logic/predicate/bank_test.go b/x/logic/predicate/bank_test.go index 6e131ad9..f77b359a 100644 --- a/x/logic/predicate/bank_test.go +++ b/x/logic/predicate/bank_test.go @@ -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", ""), ",")), }, { @@ -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", ""), ",")), }, @@ -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", ""), ",")), }, } diff --git a/x/logic/predicate/crypto.go b/x/logic/predicate/crypto.go index f5a0edf2..d907ee52 100644 --- a/x/logic/predicate/crypto.go +++ b/x/logic/predicate/crypto.go @@ -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) @@ -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 { diff --git a/x/logic/predicate/crypto_test.go b/x/logic/predicate/crypto_test.go index c90d9666..d526de16 100644 --- a/x/logic/predicate/crypto_test.go +++ b/x/logic/predicate/crypto_test.go @@ -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 @@ -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 diff --git a/x/logic/predicate/file.go b/x/logic/predicate/file.go index a4a344e0..8052fa47 100644 --- a/x/logic/predicate/file.go +++ b/x/logic/predicate/file.go @@ -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) diff --git a/x/logic/predicate/file_test.go b/x/logic/predicate/file_test.go index 2402f999..17e73924 100644 --- a/x/logic/predicate/file_test.go +++ b/x/logic/predicate/file_test.go @@ -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, }, { @@ -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, }, { @@ -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, }, { diff --git a/x/logic/predicate/string.go b/x/logic/predicate/string.go index dc811eee..bb0dfae3 100644 --- a/x/logic/predicate/string.go +++ b/x/logic/predicate/string.go @@ -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)) } } diff --git a/x/logic/prolog/context.go b/x/logic/prolog/context.go index ac2ad02b..c804a81d 100644 --- a/x/logic/prolog/context.go +++ b/x/logic/prolog/context.go @@ -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) } diff --git a/x/logic/prolog/error.go b/x/logic/prolog/error.go index 78e1a453..768919ea 100644 --- a/x/logic/prolog/error.go +++ b/x/logic/prolog/error.go @@ -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. @@ -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 @@ -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.