Skip to content

Implementation of PotatoBeans generic error code payload.

License

Notifications You must be signed in to change notification settings

potatobeansco/go-ec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ec

ec (stands for error code) is a generic package providing the Error struct implementing Go error interface. Almost all methods and handlers that are made by PotatoBeans returns error in form of ec.Error. The Error object carries more information such as error code, cause, and extra data.

All HTTP handlers should return error to the caller as error code, that is a JSON payload containing code, cause, message, and data. Cause and data are optional.

It is published as open source for anyone to use, particularly systems made by PotatoBeans for PotatoBeans clients. It is actively maintained by PotatoBeans.

Usage

The ec.Error was developed with a philosophy that all errors should be represented with error codes, with underlying cause and optionally, custom data, accompanying it. Error object can be marshaled into JSON and returned as HTTP responses to 400 or 500 codes.

Everywhere in the code, whenever you are trying to return an error type, you can return an *ec.Error object using ec.NewError or ec.NewErrorBasic. The Basic version basically omits the "cause" error.

func myFunction() error {
	fmt.Println("do something")
	_, err := os.Open("nonexistentfile")
	if err != nil {
		return ec.NewError(12345, "unable to open file", err)
    }
	
	return nil
}

To read and match errors, typically you would want to match the error code instead. The Is method has also been written to also support comparison to another ec.Error object, in which only the error code will be checked.

err := myFunction()
if err != nil {
	ver e *ec.Error
	if errors.As(err, &e); e.Code == 12345 {
	    // Do something	
    }
}

Typically, you would want to list supported error codes somewhere in the file as constants that are well documented. You can develop your own error code scheme from that to be used.

About

Implementation of PotatoBeans generic error code payload.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages