errto

command module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

errto CircleCI

This is a command to rewrite Go error handling code between the following packages:

  • errors (1.13+)
  • golang.org/x/xerrors
  • github.com/pkg/errors

It rewrites the package imports and function calls using AST transformation. All whitespaces and comments are kept.

Getting Started

Install the latest release.

go get github.com/int128/errto

To rewrite the packages in the current working directory:

# rewrite with Go 1.13+ errors
errto go-errors ./...

# rewrite with golang.org/x/xerrors
errto xerrors ./...

# rewrite with github.com/pkg/errors
errto pkg-errors ./...
Example: rewrite with golang.org/x/xerrors

To rewrite package ./acceptance_test/testdata/pkgerrors with golang.org/x/xerrors:

% errto xerrors ./acceptance_test/testdata/pkgerrors
acceptance_test/testdata/pkgerrors/main.go:14:10: errors.Wrapf() -> xerrors.Errorf()
acceptance_test/testdata/pkgerrors/main.go:18:10: errors.Errorf() -> xerrors.Errorf()
acceptance_test/testdata/pkgerrors/main.go:21:10: errors.New() -> xerrors.New()
acceptance_test/testdata/pkgerrors/main.go: + import golang.org/x/xerrors
acceptance_test/testdata/pkgerrors/main.go: - import github.com/pkg/errors
--- writing 5 change(s) to acceptance_test/testdata/pkgerrors/main.go

You can see the following diff:

@@ -1,7 +1,7 @@
 package main

 import (
-       "github.com/pkg/errors"
+       "golang.org/x/xerrors"
        "log"
        "os"
        "strconv"
@@ -11,14 +11,14 @@ import (
 func check(s string) error {
        n, err := strconv.Atoi(s)
        if err != nil {
-               return errors.Wrapf(err, "invalid number")
+               return xerrors.Errorf("invalid number: %w", err)
        }
        if n < 0 {
                // comment should be kept
-               return errors.Errorf("number is negative: %d", n)
+               return xerrors.Errorf("number is negative: %d", n)
        }
        if n == 0 {
-               return errors.New("number is zero")
+               return xerrors.New("number is zero")
        }
        return nil
 }

You can check changes without actually writing files by --dry-run flag.

errto go-errors --dry-run ./...

It is recommended to commit files into a Git repository before running the command.

Usage

Usage:
  errto [command]

Available Commands:
  dump        Dump AST of packages
  go-errors   Rewrite the packages with Go errors (fmt, errors)
  help        Help about any command
  pkg-errors  Rewrite the packages with github.com/pkg/errors
  xerrors     Rewrite the packages with golang.org/x/xerrors
Rewrite commands

The following syntax is supported.

go-errors xerrors pkg-errors
New("MESSAGE") New("MESSAGE") New("MESSAGE")
Errorf("FORMAT", ...) Errorf("FORMAT", ...) Errorf("FORMAT", ...)
Errorf("FORMAT: %w", ..., err) Errorf("FORMAT: %w", ..., err) Wrapf(err, "FORMAT", ...)
Unwrap(err) Unwrap(err) Unwrap(err) 1
Cause(err) 2
As(err, v) As(err, v) As(err, v) 1
Is(err, v) Is(err, v) Is(err, v) 1
Errorf("%s: %w", "MSG", err) Errorf("%s: %w", "MSG", err) Wrap(err, "MSG")
Errorf("%w", err) Errorf("%w", err) WithStack(err)
Errorf("%s: %s", "MSG", err) Errorf("%s: %s", "MSG", err) WithMessage(err, "MSG")
Errorf("FORMAT: %s", ..., err) Errorf("FORMAT: %s", ..., err) WithMessagef(err, "FORMAT", ...)

1 Available in github.com/pkg/errors@v0.9.0 or later. See the release note for details.

2 Cause is replaced with Unwrap. Note that it may be incompatible.

Contributions

This is an open source software. Feel free to open issues and pull requests.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg
cmd
log

Jump to

Keyboard shortcuts

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