This package provides a way to initialize possible errors and handle them with ease.
The only requirement is the Go Programming Language.
$ go get -u github.com/kataras/go-errors
New
receives a message format and creates a new Error. Message format, which is created with .New, is never changes.
import "github.com/kataras/go-errors"
var errUserAlreadyJoined = errors.New("User with username: %s was already joined in this room!")
Format
is like fmt.Sprintf
but for specific Error, returns a new error with the formatted message.
import "github.com/kataras/go-errors"
var errUserAlreadyJoined = errors.New("User with username: %s was already joined in this room!")
func anything() error {
return errUserAlreadyJoined.Format("myusername")
// will return an error with message =
// User with username: myusername was already joined in this room!
//
}
Append
and AppendErr
adds a message to existing message and returns a new error.
import "github.com/kataras/go-errors"
var errUserAlreadyJoined = errors.New("User with username: %s was already joined in this room!")
func anything() error {
return errUserAlreadyJoined.Append("Please specify other room.").Format("myusername")
// will return an error with message =
// User with username: myusername was already joined in this room!
// Please specify other room.
//
}
import "github.com/kataras/go-errors"
var errUserAlreadyJoined = errors.New("User with username: %s was already joined in this room!")
var errSpecifyOtherRoom = errors.New("Please specify other room.")
func anything() error {
return errUserAlreadyJoined.AppendErr(errSpecifyOtherRoom).Format("myusername")
// will return an error with message =
// User with username: myusername was already joined in this room!
// Please specify other room.
//
}
Use AppendErr
with go standard error type
import (
"github.com/kataras/go-errors"
"fmt"
)
var errUserAlreadyJoined = errors.New("User with username: %s was already joined in this room!")
func anything() error {
err := fmt.Errorf("Please specify other room") // standard golang error
return errUserAlreadyJoined.AppendErr(err).Format("myusername")
// will return an error with message =
// User with username: myusername was already joined in this room!
// Please specify other room.
//
}
Explore these questions or navigate to the community chat.
Current: v0.0.4
The author of go-errors is @kataras.
If you are interested in contributing to the go-errors project, please make a PR.
This project is licensed under the MIT License.
License can be found here.