Categorygithub.com/yaoguais/errfix
repositorypackage
0.0.0-20221010111158-ec5134220cab
Repository: https://github.com/yaoguais/errfix.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

errfix

Build Status codecov Go Report Card GoDoc

errfix is a command-line tool that replaces go's native error with the call-stacked error from github.com/pkg/errors.

Install

go install github.com/yaoguais/errfix/cmd/errfix@latest

Usage

usage: errfix [-w] [-q] [-e] [path ...]
  -e    set exit status to 1 if any changes are found
  -q    quiet (no output)
  -w    write result to (source) file instead of stdout

Replaces

From

package main

import (
	"errors"
	"fmt"
)

var ErrNotFound = errors.New("not found")

func foo() (int, error) {
	err := bar()
	if err != nil {
		return 0, err
	}
	if err := notFound(); err != nil {
		return 0, err
	}
	if err := isExist(); err != ErrNotFound {
		return 0, nil
	}
	return 0, nil
}

func bar() error {
	return errors.New("uncompleted")
}

func notFound() error {
	err := ErrNotFound
	return err
}

func isExist() error {
	err := ErrNotFound
	return fmt.Errorf("Check for existence, %v", err)
}

func main() {

}

To

package main

import (
	"fmt"
	"github.com/pkg/errors"
)

var ErrNotFound = errors.New("not found")

func foo() (int, error) {
	err := bar()
	if err != nil {
		return 0, errors.WithStack(err)
	}
	if err := notFound(); err != nil {
		return 0, errors.WithStack(err)
	}
	if err := isExist(); errors.Cause(err) != ErrNotFound {
		return 0, nil
	}
	return 0, nil
}

func bar() error {
	return errors.New("uncompleted")
}

func notFound() error {
	err := ErrNotFound
	return errors.WithStack(err)
}

func isExist() error {
	err := ErrNotFound
	return errors.Wrapf(err, "Check for existence")
}

func main() {

}