generr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2018 License: Apache-2.0 Imports: 12 Imported by: 1

README

generr

CircleCI MaintainabilityTest Coverage

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
  • Go 1.11+
  • make
Installing
$ go get -u github.com/akito0107/generr/cmd/generr
How to use
  1. You declare interface which must have a single function and named return values (if needs).
type userNotFound interface {
	UserNotFound() (id int64)
}
  1. Generate implementation with passing the type name.
$ generr -t userNotFound
  1. 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
}
  1. 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).

  1. 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)
}

  1. 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.
  1. 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

Documentation

Overview

Code generated by "generr"; DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTypeNotFound

func IsTypeNotFound(err error) bool

func Parse

func Parse(r io.Reader, tp string) (string, *ast.TypeSpec, error)

Types

type Generator

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

func NewGenerator

func NewGenerator(pkgName string, ts *ast.TypeSpec) *Generator

func (*Generator) AppendCheckFunction

func (g *Generator) AppendCheckFunction(withCause bool) error

func (*Generator) AppendErrorImplementation

func (g *Generator) AppendErrorImplementation(typename, message string) error

func (*Generator) AppendPackage

func (g *Generator) AppendPackage()

func (*Generator) AppendPkgErrorImportSpec

func (g *Generator) AppendPkgErrorImportSpec()

func (*Generator) Generate

func (g *Generator) Generate() error

func (*Generator) Out

func (g *Generator) Out(w io.Writer) error

type TypeNotFound

type TypeNotFound struct {
}

func (*TypeNotFound) Error

func (e *TypeNotFound) Error() string

func (*TypeNotFound) TypeNotFound

func (e *TypeNotFound) TypeNotFound()

Directories

Path Synopsis
cmd
e2e
Code generated by "generr"; DO NOT EDIT.
Code generated by "generr"; DO NOT EDIT.
subpackage
Code generated by "generr"; DO NOT EDIT.
Code generated by "generr"; DO NOT EDIT.

Jump to

Keyboard shortcuts

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