Skip to content

chris-sean/esg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ESG

ESG is short for Error Struct Generator.

It only generates Go source code recently.

Scene

Sometimes developer need to respond a JSON to the client. There are error code and error description within the payload.

{
  "error": {
    "code": "InvalidPhone",
    "desc": "abc123 is not valid phone number."
  }
}

It is cool if the error object that function returns contains an error code.

Even better if it contains a http response code. Because error can be caused by either server-side or client-side. For example, if error is caused by wrong parameters. You may want to respond a 400 instead of 500.

Or you can always respond 200 if your client doesn't prefer response status code. Surely the error object in the JSON is already informative.

ESG can generate an error type source code with both Code and Description. And response status code if you interested.

Install

Install binary to GOPATH.

go install github.com/SimpleFelix/esg/cmd/esg@v0.3.5

Or build it from source.

cd cmd/esg
go build -o esg

Usage

esg language arguments

Go Usage

esg go [-sc statu_code] output_dir pkg_name error_code formatted_message [name_of_arguments..]

Example

esg go . errors InvalidPhone '%v is not a valid phone number.' phone

If you want to use StatusCode function which returns a http response code which is 500 by default.

You need to use -sc argument to specify a http status code.

esg go -sc 400 . errors InvalidPhone '%v is not a valid phone number.' phone

Notice that -sc must be right next to go command. Otherwise, it won't work.

The generated file is at ./InvalidPhone.go

The source code looks like below.

// Generated by ESG at 2006-01-02 15:04:05. github.com/SimpleFelix/esg

package errors

import "fmt"

type InvalidPhone struct {
	_extra_ interface{}
	phone   interface{}
}

// ErrorCode change it as you prefer.
func (e InvalidPhone) ErrorCode() interface{} {
	return "InvalidPhone"
}

// StatusCode refers to http response status code.
// Developer may want to set response status code based on error.
// For example, if the error is caused by bad request, then change the return value to 400.
// Ignore this function if no need for your project.
func (e InvalidPhone) StatusCode() int {
	return 500
}

// Extra returns _extra_ which can be set by user. Usage of _extra_ is determined by user.
func (e InvalidPhone) Extra() interface{} {
	return e._extra_
}

// SetExtra sets _extra_ with a value by user. Usage of _extra_ is determined by user.
func (e *InvalidPhone) SetExtra(extra interface{}) {
	e._extra_ = extra
}

// Error implementation to error interface.
func (e InvalidPhone) Error() string {
	return fmt.Sprintf(`%v is not a valid phone number.`, e.phone)
}

// ErrInvalidPhone is convenient constructor.
func ErrInvalidPhone(phone interface{}) InvalidPhone {
	return InvalidPhone{
		phone: phone,
	}
}

Example of Using Error Struct

package errors

func brilliantlyValidate(phone string) error {
	if len(phone) > 100 {
		return ErrInvalidPhone(phone)
	}
	return nil
}

Interface

ESG provides a common interface ErrorType developer may find useful.

Go

Import ESG module to your project. ESG module requires no other dependency so far.

Go Example

Psuedo code below.

package awesome

import "github.com/SimpleFelix/esg"

type ResponsePayload struct {
	Error interface{} `json:"error"`
	Data  interface{} `json:"data,omitempty"`
}

func Handle(data interface{}, err esg.ErrorType) {
	payload := ResponsePayload {
		Error: err,
		Data: data,
	}
	if err == nil {
		respond(200, payload)
	} else {
		respond(err.ErrorCode(), payload)
	}
}

If request succeeds, client will receive below.

{
  "error": null,
  "data": {
    "foo": "bar"
  }
}

Otherwise, client will receive below.

{
  "error": {
    "code": "FooNotFound",
    "desc": "'foo' is not cute enough."
  }
}