Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prep for errors v2 #20539

Merged
merged 14 commits into from
Jun 11, 2024
Merged

feat: prep for errors v2 #20539

merged 14 commits into from
Jun 11, 2024

Conversation

tac0turtle
Copy link
Member

@tac0turtle tac0turtle commented Jun 4, 2024

Description

errors v2 is a no dep errors package to be used. It removes helpers that have been replaced by the go standard library

the errors package provides a way to register errors. If you would like to include a stack trace it must be set manually. Automatic stack traces was removed


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Refactor

    • Improved error handling logic for more efficient and standardized error extraction and wrapping.
    • Consolidated test functions for better organization and direct usage of the testing.T interface.
  • Chores

    • Updated CHANGELOG.md to reflect recent changes and migration steps from version 1 to version 2 of the errors package.

Copy link
Contributor

coderabbitai bot commented Jun 4, 2024

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Files selected but had no reviewable changes (1)
  • errors/handle.go

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The recent updates in the errors package involve significant refactoring to enhance error handling and testing. Key changes include removing custom error interfaces, adopting Go's standard errors package for wrapping and comparison, and restructuring test functions for better maintainability. These modifications aim to streamline error propagation and handling while simplifying the codebase.

Changes

Files Change Summary
errors/abci.go Refactored error handling by removing custom interfaces and using errors.As for extracting custom errors. Introduced causer interface and unified error info retrieval into abciInfo function.
errors/abci_test.go Consolidated test suites, updated test functions to use testing.T directly, and adopted fmt.Errorf for error wrapping and comparison.
errors/errors.go Removed custom error wrapping and comparison methods, replaced with Go standard library functions. Removed multiple helper functions and interfaces related to error handling.
errors/CHANGELOG.md Documented the changes in error handling, including the removal of several functions and the adoption of Go standard library methods.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ErrorsPackage
    participant CustomError

    Client->>ErrorsPackage: Call abciInfo(err)
    ErrorsPackage->>ErrorsPackage: Use errors.As to check for CustomError
    ErrorsPackage->>CustomError: Extract code and codespace
    CustomError-->>ErrorsPackage: Return code and codespace
    ErrorsPackage-->>Client: Return error info (code, codespace)
Loading
sequenceDiagram
    participant Test
    participant ErrorsPackage

    Test->>ErrorsPackage: Call ABCIInfo(err, debug)
    ErrorsPackage->>ErrorsPackage: Wrap error with fmt.Errorf
    ErrorsPackage-->>Test: Return formatted error
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

errors/go.mod Outdated Show resolved Hide resolved
@tac0turtle
Copy link
Member Author

marking as r4r, but still need to add docs

@tac0turtle tac0turtle marked this pull request as ready for review June 5, 2024 22:28
@tac0turtle tac0turtle requested a review from a team as a code owner June 5, 2024 22:29
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range and nitpick comments (3)
errors/errors_test.go (1)

Line range hint 19-44: The Register function is undefined in this context. Ensure it is imported or defined within the package.

+ import "path/to/package/where/Register/is/defined"
Tools
golangci-lint

8-8: undefined: ABCIError (typecheck)


11-11: undefined: ABCIError (typecheck)

errors/abci.go (1)

Line range hint 17-17: Undefined UndefinedCodespace. Ensure it is defined or imported correctly.

+ const UndefinedCodespace = "your_defined_codespace"
errors/abci_test.go (1)

Line range hint 18-29: Undefined ErrUnauthorized and testCodespace. Ensure they are defined or imported correctly.

+ var ErrUnauthorized = errors.New("unauthorized")
+ const testCodespace = "your_defined_codespace"
Tools
golangci-lint

22-22: undefined: testCodespace (typecheck)


25-25: undefined: ErrUnauthorized (typecheck)


28-28: undefined: ErrUnauthorized (typecheck)

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fe22e9a and 27f6378.

Files ignored due to path filters (2)
  • errors/go.mod is excluded by !**/*.mod
  • errors/go.sum is excluded by !**/*.sum
Files selected for processing (4)
  • errors/abci.go (5 hunks)
  • errors/abci_test.go (4 hunks)
  • errors/errors.go (3 hunks)
  • errors/errors_test.go (1 hunks)
Additional context used
Path-based instructions (4)
errors/errors_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

errors/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

errors/abci_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

errors/errors.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

golangci-lint
errors/errors_test.go

19-19: undefined: Register (typecheck)


20-20: undefined: Register (typecheck)


21-21: undefined: Register (typecheck)


22-22: undefined: Register (typecheck)


23-23: undefined: Register (typecheck)


24-24: undefined: Register (typecheck)


25-25: undefined: Register (typecheck)


26-26: undefined: Register (typecheck)


27-27: undefined: Register (typecheck)


28-28: undefined: Register (typecheck)


29-29: undefined: Register (typecheck)


30-30: undefined: Register (typecheck)


31-31: undefined: Register (typecheck)


32-32: undefined: Register (typecheck)


33-33: undefined: Register (typecheck)


34-34: undefined: Register (typecheck)


35-35: undefined: Register (typecheck)


36-36: undefined: Register (typecheck)


37-37: undefined: Register (typecheck)


38-38: undefined: Register (typecheck)


39-39: undefined: Register (typecheck)


40-40: undefined: Register (typecheck)


41-41: undefined: Register (typecheck)


42-42: undefined: Register (typecheck)


43-43: undefined: Register (typecheck)


44-44: undefined: Register (typecheck)


8-8: undefined: ABCIError (typecheck)


11-11: undefined: ABCIError (typecheck)

errors/abci.go

17-17: undefined: UndefinedCodespace (typecheck)


59-59: undefined: Error (typecheck)


77-77: undefined: Error (typecheck)

errors/abci_test.go

18-18: undefined: ErrUnauthorized (typecheck)


21-21: undefined: ErrUnauthorized (typecheck)


22-22: undefined: testCodespace (typecheck)


25-25: undefined: ErrUnauthorized (typecheck)


28-28: undefined: ErrUnauthorized (typecheck)


29-29: undefined: testCodespace (typecheck)


39-39: undefined: Error (typecheck)


50-50: undefined: UndefinedCodespace (typecheck)


56-56: undefined: ABCIInfo (typecheck)


71-71: undefined: ErrUnauthorized (typecheck)


72-72: undefined: ABCIInfo (typecheck)


81-81: undefined: ErrTxDecode (typecheck)


82-82: undefined: ErrInvalidAddress (typecheck)


83-83: undefined: ErrPanic (typecheck)


114-114: undefined: ABCIInfo (typecheck)

Additional comments not posted (2)
errors/errors_test.go (1)

7-12: Ensure ABCIError is defined and accessible in the test context.

Verification successful

The ABCIError function is defined and accessible in the test context. Now, let's verify if the error messages generated by ABCIError match the expected output in the test cases.


The previous attempt to verify the error messages generated by ABCIError failed because the go command was not found in the environment. Instead, let's analyze the ABCIError function and its dependencies to ensure the error messages match the expected output.


The ABCIError function, along with its dependencies (getUsed function and Error struct), has been verified. The getUsed function returns a specific error message for testCodespace and code 2, which is "tx parse error". For other cases, ABCIError returns a default "unknown" error message.

The test cases in errors/errors_test.go are correctly checking for these expected error messages.

  • For ABCIError(testCodespace, 2, "custom"), the expected error message is "custom: tx parse error".
  • For ABCIError("unknown", 1, "custom"), the expected error message is "custom: unknown".

These expectations align with the implementation of the ABCIError function.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the definition and accessibility of `ABCIError` in the test context.

# Test: Search for the definition of `ABCIError`. Expect: Definition found.
ast-grep --lang go --pattern $'func ABCIError($_, $_, $_) $_ { $$$ }'

Length of output: 587



Script:

#!/bin/bash
# Description: Verify the error messages generated by `ABCIError` for the given inputs.

# Test: Check the error message for `ABCIError(testCodespace, 2, "custom")`.
# Expect: "custom: tx parse error"
echo 'package main

import (
	"fmt"
)

type Error struct {
	codespace string
	code      uint32
	desc      string
}

func getUsed(codespace string, code uint32) error {
	if codespace == "testCodespace" && code == 2 {
		return fmt.Errorf("tx parse error")
	}
	return nil
}

func ABCIError(codespace string, code uint32, log string) error {
	if e := getUsed(codespace, code); e != nil {
		return fmt.Errorf("%s: %w", log, e)
	}
	return fmt.Errorf("%s: %w", log, &Error{codespace: codespace, code: code, desc: "unknown"})
}

func main() {
	err := ABCIError("testCodespace", 2, "custom")
	fmt.Println(err.Error())
	err = ABCIError("unknown", 1, "custom")
	fmt.Println(err.Error())
}' > verify_abcierror.go

# Run the Go script to verify the error messages.
go run verify_abcierror.go

Length of output: 780



Script:

#!/bin/bash
# Description: Analyze the `ABCIError` function and its dependencies to verify the error messages.

# Search for the `getUsed` function definition.
ast-grep --lang go --pattern $'func getUsed($_, $_) $_ { $$$ }'

# Search for the `Error` struct definition.
ast-grep --lang go --pattern $'type Error struct { $$$ }'

Length of output: 977

Tools
golangci-lint

8-8: undefined: ABCIError (typecheck)


11-11: undefined: ABCIError (typecheck)

errors/errors.go (1)

63-67: Ensure the Error type is defined and that fmt.Errorf is used correctly for error wrapping.

Comment on lines 56 to +64
space, code, log := ABCIInfo(tc.err, tc.debug)
s.Require().Equal(tc.wantSpace, space, testName)
s.Require().Equal(tc.wantCode, code, testName)
s.Require().Equal(tc.wantLog, log, testName)
})
}
}

func (s *abciTestSuite) TestABCIInfoStacktrace() {
cases := map[string]struct {
err error
debug bool
wantStacktrace bool
wantErrMsg string
}{
"wrapped SDK error in debug mode provides stacktrace": {
err: Wrap(ErrUnauthorized, "wrapped"),
debug: true,
wantStacktrace: true,
wantErrMsg: "wrapped: unauthorized",
},
"wrapped SDK error in non-debug mode does not have stacktrace": {
err: Wrap(ErrUnauthorized, "wrapped"),
debug: false,
wantStacktrace: false,
wantErrMsg: "wrapped: unauthorized",
},
"wrapped stdlib error in debug mode provides stacktrace": {
err: Wrap(fmt.Errorf("stdlib"), "wrapped"),
debug: true,
wantStacktrace: true,
wantErrMsg: "wrapped: stdlib",
},
}

const thisTestSrc = "cosmossdk.io/errors.(*abciTestSuite).TestABCIInfoStacktrace"

for testName, tc := range cases {
s.T().Run(testName, func(t *testing.T) {
_, _, log := ABCIInfo(tc.err, tc.debug)
if !tc.wantStacktrace {
s.Require().Equal(tc.wantErrMsg, log, testName)
} else {
s.Require().True(strings.Contains(log, thisTestSrc), testName)
s.Require().True(strings.Contains(log, tc.wantErrMsg), testName)
if space != tc.wantSpace {
t.Errorf("%s: expected space %s, got %s", testName, tc.wantSpace, space)
}
if code != tc.wantCode {
t.Errorf("%s: expected code %d, got %d", testName, tc.wantCode, code)
}
if log != tc.wantLog {
t.Errorf("%s: expected log %s, got %s", testName, tc.wantLog, log)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The ABCIInfo function is not accessible in the test context because the package containing it is not imported in errors/abci_test.go. Please add the necessary import statement to ensure ABCIInfo is available for testing.

  • errors/abci_test.go does not import the package containing ABCIInfo.
Analysis chain

Ensure ABCIInfo is defined and accessible in the test context.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the definition and accessibility of `ABCIInfo` in the test context.

# Test: Search for the definition of `ABCIInfo`. Expect: Definition found.
ast-grep --lang go --pattern $'func ABCIInfo($_, $_) ($_,$_,$_){ $$$ }'

Length of output: 581



Script:

#!/bin/bash
# Description: Verify if `errors/abci_test.go` imports the package containing `ABCIInfo`.

# Test: Search for import statements in `errors/abci_test.go`. Expect: Import statement for `errors/abci.go` found.
rg --lang go --pattern 'import' errors/abci_test.go

Length of output: 460



Script:

#!/bin/bash
# Description: Verify if `errors/abci_test.go` imports the package containing `ABCIInfo`.

# Test: Search for import statements in `errors/abci_test.go`. Expect: Import statement for `errors/abci.go` found.
rg 'import' errors/abci_test.go

Length of output: 40



Script:

#!/bin/bash
# Description: List all import statements in `errors/abci_test.go` to verify if the package containing `ABCIInfo` is imported.

# Test: List all import statements in `errors/abci_test.go`. Expect: Import statement for `errors/abci.go` or its package found.
rg '^import' -A 10 errors/abci_test.go

Length of output: 193

Tools
golangci-lint

56-56: undefined: ABCIInfo (typecheck)

This comment has been minimized.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 27f6378 and d3bf0d2.

Files selected for processing (2)
  • errors/abci.go (4 hunks)
  • errors/abci_test.go (4 hunks)
Additional context used
Path-based instructions (2)
errors/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

errors/abci_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

golangci-lint
errors/abci.go

17-17: undefined: UndefinedCodespace (typecheck)


59-59: undefined: Error (typecheck)


77-77: undefined: Error (typecheck)

errors/abci_test.go

18-18: undefined: ErrUnauthorized (typecheck)


21-21: undefined: ErrUnauthorized (typecheck)


22-22: undefined: testCodespace (typecheck)


25-25: undefined: ErrUnauthorized (typecheck)


28-28: undefined: ErrUnauthorized (typecheck)


29-29: undefined: testCodespace (typecheck)


39-39: undefined: Error (typecheck)


50-50: undefined: UndefinedCodespace (typecheck)


56-56: undefined: ABCIInfo (typecheck)


71-71: undefined: ErrUnauthorized (typecheck)


72-72: undefined: ABCIInfo (typecheck)


81-81: undefined: ErrTxDecode (typecheck)


82-82: undefined: ErrInvalidAddress (typecheck)


83-83: undefined: ErrPanic (typecheck)


114-114: undefined: ABCIInfo (typecheck)

Additional comments not posted (2)
errors/abci.go (1)

4-4: Ensure the new errors import is utilized effectively throughout the file.

errors/abci_test.go (1)

56-64: Ensure the ABCIInfo function is accessible and correctly used in the test context. Verify that all test cases are correctly invoking this function and handling its outputs.

Also applies to: 70-75, 78-117

Verification successful

The ABCIInfo function is defined in errors/abci.go and is correctly invoked in the test cases within errors/abci_test.go. The test cases handle its outputs appropriately.

  • errors/abci_test.go lines:
    • space, code, log := ABCIInfo(tc.err, tc.debug)
    • _, _, log := ABCIInfo(err, false)
    • _, _, log := ABCIInfo(spec.src, spec.debug)
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the definition and accessibility of `ABCIInfo` in the test context.

# Test: Search for the definition of `ABCIInfo`. Expect: Definition found.
ast-grep --lang go --pattern $'func ABCIInfo($_, $_) ($_,$_,$_){ $$$ }'

Length of output: 581



Script:

#!/bin/bash
# Description: Verify that `ABCIInfo` is correctly invoked in the test cases within `errors/abci_test.go`.

# Test: Search for invocations of `ABCIInfo` in `errors/abci_test.go`.
rg 'ABCIInfo' errors/abci_test.go

Length of output: 259

Tools
golangci-lint

56-56: undefined: ABCIInfo (typecheck)

errors/abci.go Outdated
Comment on lines 59 to 61
var customErr *Error
if errors.As(err, &customErr) {
return customErr.ABCICode()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Error type is undefined in this context. Ensure it is imported or defined within the package.

+ type Error struct {
+     codespace string
+     code uint32
+     desc string
+ }

Also applies to: 77-79

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
var customErr *Error
if errors.As(err, &customErr) {
return customErr.ABCICode()
var customErr *Error
if errors.As(err, &customErr) {
return customErr.ABCICode()
type Error struct {
codespace string
code uint32
desc string
}
Tools
golangci-lint

59-59: undefined: Error (typecheck)

Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work 💪
Some minor nits on the code. Bonus points would be available if you can restore some of the public methods in the package to support a better dev UX. errors.Wrap was quite popular in modules

errors/abci.go Outdated Show resolved Hide resolved
errors/abci.go Outdated Show resolved Hide resolved
errors/abci.go Outdated Show resolved Hide resolved
errors/abci_test.go Show resolved Hide resolved
errors/abci_test.go Show resolved Hide resolved
errors/abci_test.go Outdated Show resolved Hide resolved
errors/errors.go Outdated Show resolved Hide resolved
errors/go.mod Show resolved Hide resolved
Copy link
Contributor

@cool-develope cool-develope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like I am missing some context, don't we need the stacktrace further more?

@tac0turtle
Copy link
Member Author

seems like I am missing some context, don't we need the stacktrace further more?

stack trace can be added manually, but it wont be done automatically in errors/v2. Most of the stacktraces are dropped in client errors anyways

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d3bf0d2 and a065237.

Files selected for processing (2)
  • errors/abci_test.go (4 hunks)
  • errors/errors.go (5 hunks)
Additional context used
Path-based instructions (2)
errors/abci_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

errors/errors.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

golangci-lint
errors/abci_test.go

18-18: undefined: ErrUnauthorized (typecheck)


21-21: undefined: ErrUnauthorized (typecheck)


22-22: undefined: testCodespace (typecheck)


25-25: undefined: ErrUnauthorized (typecheck)


28-28: undefined: ErrUnauthorized (typecheck)


29-29: undefined: testCodespace (typecheck)


39-39: undefined: Error (typecheck)


50-50: undefined: UndefinedCodespace (typecheck)


56-56: undefined: ABCIInfo (typecheck)


73-73: undefined: ErrTxDecode (typecheck)


74-74: undefined: ErrInvalidAddress (typecheck)


75-75: undefined: ErrPanic (typecheck)


106-106: undefined: ABCIInfo (typecheck)

Additional comments not posted (2)
errors/errors.go (2)

63-63: Ensure proper error wrapping in ABCIError function.

This change correctly wraps the error, maintaining the original error context.


114-114: Refactor Wrap function to handle nil errors gracefully.

This implementation correctly returns nil when the error is nil, avoiding unnecessary wrapping.

Comment on lines +73 to 75
myErrDecode = fmt.Errorf("test: %w", ErrTxDecode)
myErrAddr = fmt.Errorf("tester: %w", ErrInvalidAddress)
myPanic = ErrPanic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure error constants like ErrTxDecode, ErrInvalidAddress, and ErrPanic are defined or imported.

+ import (
+     "path/to/errors/constants" // Adjust the import path as necessary
+ )

Committable suggestion was skipped due to low confidence.

Tools
golangci-lint

73-73: undefined: ErrTxDecode (typecheck)


74-74: undefined: ErrInvalidAddress (typecheck)


75-75: undefined: ErrPanic (typecheck)

@@ -37,7 +22,7 @@
wantSpace: testCodespace,
},
"wrapped SDK error": {
err: Wrap(Wrap(ErrUnauthorized, "foo"), "bar"),
err: fmt.Errorf("bar: %w", fmt.Errorf("foo: %w", ErrUnauthorized)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined error constant ErrUnauthorized needs to be defined or imported.

+ import (
+     "path/to/errors/constants" // Adjust the import path as necessary
+ )
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
err: fmt.Errorf("bar: %w", fmt.Errorf("foo: %w", ErrUnauthorized)),
import (
"path/to/errors/constants" // Adjust the import path as necessary
)
func TestErrors(t *testing.T) {
tests := []struct {
name string
err error
}{
{
name: "unauthorized error",
err: fmt.Errorf("bar: %w", fmt.Errorf("foo: %w", ErrUnauthorized)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err == nil {
t.Errorf("expected error, got nil")
}
})
}
}
Tools
golangci-lint

25-25: undefined: ErrUnauthorized (typecheck)

Comment on lines +55 to +64
t.Run(testName, func(t *testing.T) {
space, code, log := ABCIInfo(tc.err, tc.debug)
s.Require().Equal(tc.wantSpace, space, testName)
s.Require().Equal(tc.wantCode, code, testName)
s.Require().Equal(tc.wantLog, log, testName)
})
}
}

func (s *abciTestSuite) TestABCIInfoStacktrace() {
cases := map[string]struct {
err error
debug bool
wantStacktrace bool
wantErrMsg string
}{
"wrapped SDK error in debug mode provides stacktrace": {
err: Wrap(ErrUnauthorized, "wrapped"),
debug: true,
wantStacktrace: true,
wantErrMsg: "wrapped: unauthorized",
},
"wrapped SDK error in non-debug mode does not have stacktrace": {
err: Wrap(ErrUnauthorized, "wrapped"),
debug: false,
wantStacktrace: false,
wantErrMsg: "wrapped: unauthorized",
},
"wrapped stdlib error in debug mode provides stacktrace": {
err: Wrap(fmt.Errorf("stdlib"), "wrapped"),
debug: true,
wantStacktrace: true,
wantErrMsg: "wrapped: stdlib",
},
}

const thisTestSrc = "cosmossdk.io/errors.(*abciTestSuite).TestABCIInfoStacktrace"

for testName, tc := range cases {
s.T().Run(testName, func(t *testing.T) {
_, _, log := ABCIInfo(tc.err, tc.debug)
if !tc.wantStacktrace {
s.Require().Equal(tc.wantErrMsg, log, testName)
} else {
s.Require().True(strings.Contains(log, thisTestSrc), testName)
s.Require().True(strings.Contains(log, tc.wantErrMsg), testName)
if space != tc.wantSpace {
t.Errorf("%s: expected space %s, got %s", testName, tc.wantSpace, space)
}
if code != tc.wantCode {
t.Errorf("%s: expected code %d, got %d", testName, tc.wantCode, code)
}
if log != tc.wantLog {
t.Errorf("%s: expected log %s, got %s", testName, tc.wantLog, log)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor test cases to use assertions for improved readability and maintainability.

- if space != tc.wantSpace {
-   t.Errorf("%s: expected space %s, got %s", testName, tc.wantSpace, space)
- }
- if code != tc.wantCode {
-   t.Errorf("%s: expected code %d, got %d", testName, tc.wantCode, code)
- }
- if log != tc.wantLog {
-   t.Errorf("%s: expected log %s, got %s", testName, tc.wantLog, log)
- }
+ assert.Equal(t, tc.wantSpace, space, "%s: expected space %s, got %s", testName, tc.wantSpace, space)
+ assert.Equal(t, tc.wantCode, code, "%s: expected code %d, got %d", testName, tc.wantCode, code)
+ assert.Equal(t, tc.wantLog, log, "%s: expected log %s, got %s", testName, tc.wantLog, log)

Committable suggestion was skipped due to low confidence.

Tools
golangci-lint

56-56: undefined: ABCIInfo (typecheck)

}

func (s *abciTestSuite) TestABCInfo() {
func TestABCInfo(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The search results indicate that there are no direct references to the additional error handling functions (debugErrEncoder, defaultErrEncoder, abciCode, abciCodespace, errIsNil) in the errors/abci_test.go file. This suggests that these functions might not be explicitly tested.

To ensure comprehensive test coverage, it is important to add test cases that directly test these functions. Here are the specific locations in the codebase that need attention:

  • errors/abci.go: Functions debugErrEncoder, defaultErrEncoder, abciCode, abciCodespace, errIsNil need direct test cases.
  • errors/abci_test.go: Add new test functions to cover the above-mentioned functions.

Please add the necessary test cases to ensure all error handling logic is thoroughly tested.

Analysis chain

Ensure comprehensive test coverage for new error handling logic.


The output shows that the errors/abci.go file contains several functions related to error handling, such as ABCIInfo, debugErrEncoder, defaultErrEncoder, abciCode, abciCodespace, and errIsNil. The errors/abci_test.go file includes two test functions: TestABCInfo and TestABCIInfoSerializeErr.

Both test functions seem to cover various scenarios for the ABCIInfo function, including different types of errors and debug modes. However, there is no explicit indication that the other error handling functions (debugErrEncoder, defaultErrEncoder, abciCode, abciCodespace, errIsNil) are directly tested.

To ensure comprehensive test coverage, we need to verify if these additional functions are tested either directly or indirectly through the existing test cases. Let's generate a script to search for references to these functions within the test file.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all new error handling logic in `errors/abci.go` is covered by tests in `errors/abci_test.go`.

# Test: Search for test cases covering new logic. Expect: Test cases found.
rg --type go 'Test' errors/abci_test.go

Length of output: 118



Script:

#!/bin/bash
# Description: Identify error handling logic in `errors/abci.go` and check if the test functions in `errors/abci_test.go` cover them.

# Step 1: Search for error handling logic in `errors/abci.go`.
rg --type go 'error' errors/abci.go

# Step 2: Display the content of `errors/abci_test.go` to cross-reference the test functions.
cat errors/abci_test.go

Length of output: 3763



Script:

#!/bin/bash
# Description: Verify if additional error handling functions in `errors/abci.go` are covered by tests in `errors/abci_test.go`.

# Step 1: Search for references to `debugErrEncoder` in `errors/abci_test.go`.
rg --type go 'debugErrEncoder' errors/abci_test.go

# Step 2: Search for references to `defaultErrEncoder` in `errors/abci_test.go`.
rg --type go 'defaultErrEncoder' errors/abci_test.go

# Step 3: Search for references to `abciCode` in `errors/abci_test.go`.
rg --type go 'abciCode' errors/abci_test.go

# Step 4: Search for references to `abciCodespace` in `errors/abci_test.go`.
rg --type go 'abciCodespace' errors/abci_test.go

# Step 5: Search for references to `errIsNil` in `errors/abci_test.go`.
rg --type go 'errIsNil' errors/abci_test.go

Length of output: 240

@tac0turtle tac0turtle requested review from alpe and cool-develope June 6, 2024 17:30
Copy link
Contributor

@cool-develope cool-develope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@tac0turtle
Copy link
Member Author

Do we want to keep v1 around? Don't think we need it?

@julienrbrt
Copy link
Member

Do we want to keep v1 around? Don't think we need it?

Depends if we stop maintaining v1

@tac0turtle
Copy link
Member Author

Do we want to keep v1 around? Don't think we need it?

Depends if we stop maintaining v1

Don't think we need to since we haven't touched the pkg much in the past few months

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a065237 and 5afda00.

Files selected for processing (2)
  • errors/CHANGELOG.md (1 hunks)
  • errors/abci.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • errors/abci.go
Additional context used
Path-based instructions (1)
errors/CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

LanguageTool
errors/CHANGELOG.md

[style] ~36-~36: Consider a shorter alternative to avoid wordiness. (IN_ORDER_TO_PREMIUM)
Context: ...es from the error package. This is done in order to keep the dependency graph of errors min...

Markdownlint
errors/CHANGELOG.md

37-37: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces

@@ -34,6 +34,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### API Breaking

* [#20402](https://github.com/cosmos/cosmos-sdk/pull/20402) Remove Grpc error codes from the error package. This is done in order to keep the dependency graph of errors minimal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider rephrasing for conciseness.

- This is done in order to keep the dependency graph of errors minimal
+ This simplifies the error dependency graph
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
* [#20402](https://github.com/cosmos/cosmos-sdk/pull/20402) Remove Grpc error codes from the error package. This is done in order to keep the dependency graph of errors minimal
* [#20402](https://github.com/cosmos/cosmos-sdk/pull/20402) Remove Grpc error codes from the error package. This simplifies the error dependency graph
Tools
LanguageTool

[style] ~36-~36: Consider a shorter alternative to avoid wordiness. (IN_ORDER_TO_PREMIUM)
Context: ...es from the error package. This is done in order to keep the dependency graph of errors min...

@@ -34,6 +34,7 @@
### API Breaking

* [#20402](https://github.com/cosmos/cosmos-sdk/pull/20402) Remove Grpc error codes from the error package. This is done in order to keep the dependency graph of errors minimal
* [#20539](https://github.com/cosmos/cosmos-sdk/pull/20539) v2 errors removes `IsOf`, `Recover`, `WithType` and wrapped error. The errors package uses the go std library errors. It provides a `Wrap` and `Wrapf` to help in the migration from v1 to v2.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the trailing space at the end of the line.

- v2 errors removes `IsOf`, `Recover`, `WithType` and wrapped error. The errors package uses the go std library errors. It provides a `Wrap` and `Wrapf` to help in the migration from v1 to v2. 
+ v2 errors removes `IsOf`, `Recover`, `WithType` and wrapped error. The errors package uses the go std library errors. It provides a `Wrap` and `Wrapf` to help in the migration from v1 to v2.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
* [#20539](https://github.com/cosmos/cosmos-sdk/pull/20539) v2 errors removes `IsOf`, `Recover`, `WithType` and wrapped error. The errors package uses the go std library errors. It provides a `Wrap` and `Wrapf` to help in the migration from v1 to v2.
* [#20539](https://github.com/cosmos/cosmos-sdk/pull/20539) v2 errors removes `IsOf`, `Recover`, `WithType` and wrapped error. The errors package uses the go std library errors. It provides a `Wrap` and `Wrapf` to help in the migration from v1 to v2.
Tools
Markdownlint

37-37: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces

Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
I think you can remove handle.go as well.

@tac0turtle tac0turtle enabled auto-merge June 11, 2024 09:54
@tac0turtle tac0turtle added this pull request to the merge queue Jun 11, 2024
Merged via the queue into main with commit 62aa118 Jun 11, 2024
63 of 65 checks passed
@tac0turtle tac0turtle deleted the marko/errorsv2 branch June 11, 2024 10:07
alpe added a commit that referenced this pull request Jun 12, 2024
* main:
  refactor(x/auth): Fix system test (#20531)
  feat(crypto): add blst (#20296)
  docs: Update high level overview and introduction (#20535)
  refactor(x/core): remove test (#20624)
  feat(x/mint)!: Replace InflationCalculationFn with MintFn + simple epoch minting (#20363)
  docs: remove duplicate words (#20622)
  feat: prep for errors v2 (#20539)
  chore: reduce default inflation (#20606)
  refactor(store): add miss defer (#20602)
  chore: use comet api pkg instead of comet alias (#20614)
  chore: write gentx info to cmd.ErrOrStderr (#20616)
  docs: ADR 073: update to accepted and add to README.md (#20619)
  chore(proto): change future extracted modules version from v1.0.0 to v0.2.0 (#20600)
  fix: remove some duplicate words (#20605)
  feat(stf): port simappv2 changes (#20587)
  chore: bring patch changelogs to main (#20599)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants