diff --git a/cosmos/errors.go b/cosmos/errors.go new file mode 100644 index 000000000..5df27462e --- /dev/null +++ b/cosmos/errors.go @@ -0,0 +1,26 @@ +package cosmos + +import ( + "github.com/cosmos/cosmos-sdk/types" +) + +const ( + // CodespaceMesg is a cosmos codespace for all mesg errors. + CodespaceMesg types.CodespaceType = "mesg" +) + +// Base mesg codes. +const ( + CodeInternal types.CodeType = 1000 + CodeValidation types.CodeType = 2000 +) + +// NewMesgErrorf creates error with given code type and mesg codespace. +func NewMesgErrorf(ct types.CodeType, format string, a ...interface{}) types.Error { + return types.NewError(CodespaceMesg, ct, format, a...) +} + +// NewMesgWrapError creates error with given code type and mesg codespace. +func NewMesgWrapError(ct types.CodeType, err error) types.Error { + return types.NewError(CodespaceMesg, ct, err.Error()) +} diff --git a/cosmos/module.go b/cosmos/module.go index 99b0dd533..cd9006bc4 100644 --- a/cosmos/module.go +++ b/cosmos/module.go @@ -149,11 +149,11 @@ func (m AppModule) NewQuerierHandler() cosmostypes.Querier { if errsdk, ok := err.(cosmostypes.Error); ok { return nil, errsdk } - return nil, cosmostypes.ErrInternal(err.Error()) + return nil, NewMesgWrapError(CodeInternal, err) } res, err := codec.MarshalBinaryBare(data) if err != nil { - return nil, cosmostypes.ErrInternal(err.Error()) + return nil, NewMesgWrapError(CodeInternal, err) } return res, nil } diff --git a/sdk/runner/backend.go b/sdk/runner/backend.go index 338b21f9b..3ff66b0b9 100644 --- a/sdk/runner/backend.go +++ b/sdk/runner/backend.go @@ -40,12 +40,12 @@ func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (has case msgCreateRunner: run, err := s.Create(request, &msg) if err != nil { - return nil, cosmostypes.ErrInternal(err.Error()) + return nil, cosmos.NewMesgWrapError(cosmos.CodeInternal, err) } return run.Hash, nil case msgDeleteRunner: if err := s.Delete(request, &msg); err != nil { - return nil, err + return nil, cosmos.NewMesgWrapError(cosmos.CodeInternal, err) } return nil, nil default: diff --git a/sdk/runner/msgs.go b/sdk/runner/msgs.go index 19d0c3582..418e01099 100644 --- a/sdk/runner/msgs.go +++ b/sdk/runner/msgs.go @@ -3,6 +3,7 @@ package runnersdk import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/mesg-foundation/engine/codec" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/x/xvalidator" ) @@ -39,10 +40,10 @@ func (msg msgCreateRunner) ValidateBasic() cosmostypes.Error { return cosmostypes.ErrInternal(err.Error()) } if msg.ServiceHash.IsZero() { - return cosmostypes.ErrInternal("serviceHash is missing") + return cosmos.NewMesgErrorf(cosmos.CodeValidation, "serviceHash is missing") } if msg.EnvHash.IsZero() { - return cosmostypes.ErrInternal("envHash is missing") + return cosmos.NewMesgErrorf(cosmos.CodeValidation, "envHash is missing") } if msg.Address.Empty() { return cosmostypes.ErrInvalidAddress("address is missing") @@ -90,7 +91,7 @@ func (msg msgDeleteRunner) ValidateBasic() cosmostypes.Error { return cosmostypes.ErrInternal(err.Error()) } if msg.RunnerHash.IsZero() { - return cosmostypes.ErrInternal("runnerHash is missing") + return cosmos.NewMesgErrorf(cosmos.CodeValidation, "runnerHash is missing") } if msg.Address.Empty() { return cosmostypes.ErrInvalidAddress("address is missing") diff --git a/sdk/service/backend.go b/sdk/service/backend.go index 19988ced5..2beb912f4 100644 --- a/sdk/service/backend.go +++ b/sdk/service/backend.go @@ -42,7 +42,7 @@ func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (has case msgCreateService: srv, err := s.Create(request, &msg) if err != nil { - return nil, err + return nil, cosmos.NewMesgWrapError(cosmos.CodeInternal, err) } return srv.Hash, nil default: