go-rewrap-errors

command module
v0.0.0-...-bfce32d Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-rewrap-errors

GoDoc Build Status codecov Go Report Card License

Rewrite Go source files to replace pkg/errors with Go 1.13 error wrapping.

Written to accompany the article Rewriting Go with AST transformation. (Not actively developed.)

This program reads a Go source file and rewraps errors:

errors.Wrap(err, "text")        -> fmt.Errorf("text: %w", err)
errors.Wrapf(err, "text %s", s) -> fmt.Errorf("text %s: %w", s, err)

If the string argument to Wrap or Wrapf is fmt.Sprintf, it will be unwrapped:

errors.Wrap(err, fmt.Sprintf("text %s", s)) -> fmt.Errorf("text %s: %w", s, err)

Non-literal error/format strings will be concatenated with : %w:

const errFmt = "text %s"
errors.Wrapf(err, errFmt, s) -> fmt.Errorf(errFmt+": %w", s, err)

Any use of errors.Errorf is replaced with fmt.Errorf:

errors.Errorf("text %s", s) -> fmt.Errorf("text %s", s)

Currently, only Wrap, Wrapf, and Errorf are supported.

Any import of github.com/pkg/errors will be rewritten to errors. You should run the resulting source through goimports to clean up imports you don't need anymore.

Output defaults to stdout or the original file can be overwritten with the -w option.

Installation

NOTE: Do this outside your project directory so you don't modify your go.mod file.

go get github.com/xdg-go/go-rewrap-errors
Usage

For a single file, with output to stdout:

go-rewrap-errors source.go > new-source.go

For all Go files in a directory, recursively, overwriting in place, including goimports cleanup:

for f in $(find . -iname "*.go"); do go-rewrap-errors -w $f; goimports -w $f; done

Copyright 2019 by David A. Golden. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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