errfix

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

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

Go to latest
Published: Oct 10, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

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() {

}

Documentation

Overview

Package errfix declares the types used to replace the original go error with an error with a call stack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DiffWriter

type DiffWriter struct {
	// contains filtered or unexported fields
}

DiffWriter implements the Writer interface, and it can generate diffs of old and new files.

func NewDiffWriter

func NewDiffWriter(write bool) *DiffWriter

NewDiffWriter returns a DiffWriter structure. When write is true and if there is a difference between the old and new files, then the content of the new file will overwrite the content of the old file.

func (*DiffWriter) DiffString

func (w *DiffWriter) DiffString() string

DiffString returns the differences of files currently held in the buffer.

func (*DiffWriter) Write

func (w *DiffWriter) Write(ctx context.Context, f *File, f2 *File) error

Write writes the difference between the contents of two files to the buffer, and overwrites the old file with the new file when needed. If Write is called multiple times, the difference is appended to the buffer without clearing.

type ErrFix

type ErrFix struct {
	// contains filtered or unexported fields
}

ErrFix converts a simple go error into an error carrying contextual information such as the call stack.

func NewErrFix

func NewErrFix(r Reader, p Processor, w Writer) *ErrFix

NewErrFix returns an ErrFix instance.

func (*ErrFix) Process

func (e *ErrFix) Process(ctx context.Context) error

Process will read all the files and then process the files and finally write the files to the buffer (or directly overwrite the original files).

type File

type File struct {
	Name    string
	Content string
	Error   error
}

File represents a go file. The Error field will be set when an error occurs while reading or processing the file.

type Processor

type Processor interface {
	Process(context.Context, *File) (*File, error)
}

Processor is an interface that only contains the Process method. It converts the input file into a new file through built-in rules.

func NewProcessor

func NewProcessor() Processor

NewProcessor returns a default Processor interface.

type Reader

type Reader interface {
	Read(context.Context) (chan *File, error)
}

Reader is an interface that contains only one Read method.

func NewReader

func NewReader(inputs ...interface{}) Reader

NewReader returns a default Reader interface. The parameter inputs can be *os.File, io.Reader, file path, directory path. When the wrong type is entered, an error will be thrown during actual reading.

type Writer

type Writer interface {
	Write(context.Context, *File, *File) error
}

Writer is an interface that contains only one Write method.

Directories

Path Synopsis
cmd
errfix
Package main creates an errfix tool.
Package main creates an errfix tool.

Jump to

Keyboard shortcuts

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