Skip to content
Rewrite Go error handling between errors, xerrors and pkg/errors using AST transformation
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

transerr CircleCI

This is a command line tool to transform Go error handling between errors, xerrors and

Status: Proof of Concept.

Getting Started

Install the latest release.

go get

Run the transerr.

% transerr transform ./testdata
testdata/hello.go:4:2: rewriting the import with
testdata/hello.go:11:9: rewriting the function call with xerrors.New()
testdata/hello.go:12:6: rewriting the function call with xerrors.Errorf()
testdata/hello.go:13:6: rewriting the function call with xerrors.Errorf()
testdata/hello.go: total 4 change(s)

You can see the following changes.

index eb32e7e..5ed2504 100644
--- a/testdata/hello.go
+++ b/testdata/hello.go
@@ -1,15 +1,15 @@
 package testdata

 import (
-       ""
+       ""

 var msg = "foo"

 // Hello says hello world!
 func Hello() error {
-       err := errors.New("message")
-       _ = errors.Wrapf(err, "message %s", msg)
-       _ = errors.Errorf("message %s", msg)
+       err := xerrors.New("message")
+       _ = xerrors.Errorf("message %s: %w", msg, err)
+       _ = xerrors.Errorf("message %s", msg)
        return nil

Transformation rules

Currently the following rules are supported.

  • to
    • errors.Errorf to xerrors.Errorf
    • errors.New to xerrors.New
    • errors.Wrapf to xerrors.Errorf

1. to

From To
import "" import ""
errors.Errorf("message %s", msg) xerrors.Errorf("message %s", msg)
errors.New("message") xerrors.New("message")
errors.Wrapf(err, "message %s", msg) xerrors.Errorf("message %s: %w", msg, err)
errors.Wrap() TODO
errors.Cause() TODO
errors.WithStack() TODO
errors.WithMessage() TODO
errors.WithMessagef() TODO

2. xerrors to errors (Go 1.13)



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

You can’t perform that action at this time.