generr
generate error utilities for golang.
Inspired by BANZAI CLOUD's Error Handling Practices in Go,
this command generate custom error utilities from simple interface
declaration.
Getting Started
Prerequisites
Installing
$ go get -u github.com/akito0107/generr/cmd/generr
How to use
- You declare
interface
which must have a single function and named return values (if needs).
type userNotFound interface {
UserNotFound() (id int64)
}
- Generate implementation with passing the type name.
$ generr -t userNotFound
- Then, you can get implementation file which named
userNotFound_check.go
.
This file contains the function which identifies whether given error is the one that we are defined before.
func IsUserNotFound(err error) (bool, int64) {
var id int64
if e, ok := err.(userNotFound); ok {
id = e.UserNotFound()
return true, id
}
return false, id
}
- You can also generate struct which implements
error
and the interface
with -i
option.
$ generr -t userNotFound -i -it userNotFound -o ../otherpackage
You get generated file named userNotFound_impl.go
.
package otherpackage
type userNotFound struct {
Id int64
}
func (e *userNotFound) UserNotFound() int64 {
return e.Id
}
func (e *userNotFound) Error() string {
return fmt.Sprintf("userNotFound Id: %v", e.Id)
}
You can pass struct name with -it
option (default case is capitalized name given with -t
).
-o
option (default location is current directory and package).
- You can unify
check
and impl
files with -u
option.
$ generr -t userNotFound -i -u
func IsUserNotFound(err error) (bool, int64) {
var id int64
if e, ok := err.(userNotFound); ok {
id = e.UserNotFound()
return true, id
}
return false, id
}
type UserNotFound struct {
Id int64
}
func (e *UserNotFound) UserNotFound() int64 {
return e.Id
}
func (e *UserNotFound) Error() string {
return fmt.Sprintf("userNotFound Id: %v", e.Id)
}
- You can also use
go generate
//go:generate generr -t notFound -i -u
type userNotFound interface {
UserNotFound() (id int64)
}
$ go generate // you can get same results.
- You can pass custom error message with
-m
flag.
//go:generate generr -t emailNotFound -m "email %s not found" -i -u
type emailNotFound interface{
EmailNotFound() (email string)
}
and run go generate
$ go generate
then, you get custom error utilities with passed error message.
// Code generated by "generr"; DO NOT EDIT.
package e2e
import "fmt"
func IsEmailNotFound(err error) (bool, string) {
var email string
if e, ok := err.(emailNotFound); ok {
email = e.EmailNotFound()
return true, email
}
return false, email
}
type EmailNotFound struct {
Email string
}
func (e *EmailNotFound) EmailNotFound() string {
return e.Email
}
func (e *EmailNotFound) Error() string {
return fmt.Sprintf("email %s is not found", e.Email)
}
Options
$ generr -h
NAME:
generr - generate custom error from interface
USAGE:
generr [OPTIONS]
VERSION:
0.0.0
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--type value, -t value error interface name (required)
--dryrun dryrun (default=false)
--implementation, -i generate error implementation (default=false)
--unify, -u (only affects with --implementation option) unify implementation with checking function (default=false)
--implementation-output-path value, -o value (only affects with --implementation option) implementation output path (default=current directory)
--implementation-type value, --it value (only affects with --implementation option) implementation type name (default=capitalized given type name)
--message value, -m value custom error message (optional)
--cause, -c append cause check (default=false)
--help, -h show help
--version, -v print the version
License
This project is licensed under the Apache License 2.0 License - see the LICENSE file for details