withstackcheck

package module
v0.0.0-...-cb46bc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 4 Imported by: 0

README

withstackcheck

withstackcheck checks error-returning statements are wrapped with errors.WithStack() only when the errors come from external packages.

⭕ Valid
func valid() error {
	var err error

	err = throw() // internal
	if err != nil {
		return err
	}

	_, err = json.Marshal(nil) // external
	if err != nil {
		return errors.WithStack(err)
	}
❌ Invalid
func invalid() error {
	var err error

	err = throw() // internal
	if err != nil {
		return errors.WithStack(err) // need to remove errors.WithStack()
	}

	_, err = json.Marshal(nil) // external
	if err != nil {
		return err // need to wrap with errors.WithStack()
	}

Why withstackcheck?

If we want to log an error and stack trace at the outer of the call stack, we need to memorize it at the package boundary.

Installation

go install github.com/codeout/withstackcheck/cmd/withstackcheck@latest

How to Use

go vet
go vet -vettool=$(go env GOPATH)/bin/withstackcheck ./...
golangci-lint

Not yet supported.

TODO

*ast.IndexExpr support

Cannot check indexed return statements:

func unknown() error {
    var errs []error

    if _, err := json.Marshal(nil); err != nil {
    	errs = append(errs, err)
    }

    if len(errs) > 0 {
    	return errs[0] // cannot check this
    }

Copyright (c) 2023 Shintaro Kojima. Code released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "withstackcheck",
	Doc:  doc,
	Run:  run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL