Skip to content

Commit

Permalink
fix(gno.land): refine error message for addpkg operation (#2409)
Browse files Browse the repository at this point in the history
This PR introduces `PkgExistError` to replace `InvalidPkgPathError` for
situations where a package already exists during the `addpkg` operation,
enhancing clarity and specificity.
  • Loading branch information
ltzmaxwell authored Sep 13, 2024
1 parent 0688c65 commit 22ce48c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions gno.land/pkg/sdk/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (abciError) AssertABCIError() {}
// NOTE: these are meant to be used in conjunction with pkgs/errors.
type (
InvalidPkgPathError struct{ abciError }
PkgExistError struct{ abciError }
InvalidStmtError struct{ abciError }
InvalidExprError struct{ abciError }
UnauthorizedUserError struct{ abciError }
Expand All @@ -26,6 +27,7 @@ type (
)

func (e InvalidPkgPathError) Error() string { return "invalid package path" }
func (e PkgExistError) Error() string { return "package already exists" }
func (e InvalidStmtError) Error() string { return "invalid statement" }
func (e InvalidExprError) Error() string { return "invalid expression" }
func (e UnauthorizedUserError) Error() string { return "unauthorized user" }
Expand All @@ -36,6 +38,10 @@ func (e TypeCheckError) Error() string {
return bld.String()
}

func ErrPkgAlreadyExists(msg string) error {
return errors.Wrap(PkgExistError{}, msg)
}

func ErrUnauthorizedUser(msg string) error {
return errors.Wrap(UnauthorizedUserError{}, msg)
}
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
return ErrInvalidPkgPath(err.Error())
}
if pv := gnostore.GetPackage(pkgPath, false); pv != nil {
return ErrInvalidPkgPath("package already exists: " + pkgPath)
return ErrPkgAlreadyExists("package already exists: " + pkgPath)
}
if gno.ReGnoRunPath.MatchString(pkgPath) {
return ErrInvalidPkgPath("reserved package name: " + pkgPath)
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Echo() string {return "hello world"}`,
err = env.vmk.AddPackage(ctx, msg1)

assert.Error(t, err)
assert.True(t, errors.Is(err, InvalidPkgPathError{}))
assert.True(t, errors.Is(err, PkgExistError{}))

// added package is formatted
store := env.vmk.getGnoTransactionStore(ctx)
Expand Down
1 change: 1 addition & 0 deletions gno.land/pkg/sdk/vm/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Package = amino.RegisterPackage(amino.NewPackage(

// errors
InvalidPkgPathError{}, "InvalidPkgPathError",
PkgExistError{}, "PkgExistError",
InvalidStmtError{}, "InvalidStmtError",
InvalidExprError{}, "InvalidExprError",
TypeCheckError{}, "TypeCheckError",
Expand Down
3 changes: 3 additions & 0 deletions gno.land/pkg/sdk/vm/vm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ message m_addpkg {
message InvalidPkgPathError {
}

message PkgExistError {
}

message InvalidStmtError {
}

Expand Down

0 comments on commit 22ce48c

Please sign in to comment.