errorizer

command module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

README

Errorizer - A stringer, but for Errors

Go Reference

I really enjoy using Stringer on my constants, it sames me time and prevents all sorts of errors in my code. Plus it's just neat!

This tool builds on that work (it uses most of the same code) but generates an Error function instead. Others have written fantastic posts about the virtues of constant errors, which my summarizing can't do justice. It also allows you to have actual numerical error codes, which are useful for documentation and long term support. Imagine being able to document Error-102 rather than just the plain text.

That's the point of this tool.

I'm not even trying to pretend most of this code is my own, I've just modified the Stringer tool to generate the Error function and change the string output to the form: package-CODE: error message.

So, for a package named something like sql you'd get errors:

sql-1: invalid query
sql-2: missing something somewhere
sql-3: mismatched thing

See:

Documentation

Overview

Errorizer is a tool to automate the creation of methods that satisfy the Error interface. Given the name of a (signed or unsigned) integer type T that has constants defined, errorizer will create a new self contained Go source file implemented

func (t T) Error() string

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

Errorizer works best with constants that are consecutive values such as created using iota, but creates good code regardless. In the future it might also provide custom support for constant sets that are bit patterns.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

errorizer -type=Pill

in the same directory will create the file pill_error.go, in package painkiller, containing a definition of

func (Pill) Error() string

Typically this process would be run using go generate, like this:

//go:generate errorizer -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_string.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

The -linecomment flag tells stringer to generate the text of any line comment, trimmed of leading spaces, instead of the constant name. For instance, if the constants above had a Pill prefix, one could write

PillAspirin // Aspirin

to suppress it in the output.

Directories

Path Synopsis
examples
web

Jump to

Keyboard shortcuts

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