go-errors icon indicating copy to clipboard operation
go-errors copied to clipboard

:warning: Better GoLang error handling.

Build Status License Releases Read me docs Build Status Built with GoLang Platforms

This package provides a way to initialize possible errors and handle them with ease.

Installation

The only requirement is the Go Programming Language.

$ go get -u github.com/kataras/go-errors

Docs

New

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

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

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.
  //
}

FAQ

Explore these questions or navigate to the community chat.

Versioning

Current: v0.0.4

People

The author of go-errors is @kataras.

Contributing

If you are interested in contributing to the go-errors project, please make a PR.

License

This project is licensed under the MIT License.

License can be found here.