errors

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 2 Imported by: 97

README

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 gopkg.in/kataras/go-errors.v0

Docs

New

New receives a message format and creates a new Error. Message format, which is created with .New, is never changes.

import "gopkg.in/kataras/go-errors.v0"

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 "gopkg.in/kataras/go-errors.v0"

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 "gopkg.in/kataras/go-errors.v0"

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 "gopkg.in/kataras/go-errors.v0"

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 (
  "gopkg.in/kataras/go-errors.v0"
  "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.
  //
}

go-* packages

Name Description
go-fs FileSystem utils and common net/http static files handlers
go-events EventEmmiter for Go
go-websocket A websocket server and ,optionally, client side lib for Go
go-ssh SSH Server, build ssh interfaces, remote commands and remote cli with ease
go-gzipwriter Write gzip data to a io.Writer
go-mailer E-mail Sender, send rich mails with one call
rizla Monitor and live-reload of your Go App
Q HTTP2 Web Framework, 100% compatible with net/http
Iris The fastest web framework. Built on top of fasthttp

FAQ

Explore these questions or navigate to the community chat.

Versioning

Current: v0.0.3

People

The author of go-errors is @kataras.

If you're willing to donate, feel free to send any amount through paypal

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.

Documentation

Overview

Package errors helps you to write and design your own pre-defined errors, useful when you have a known list of errors

Index

Examples

Constants

View Source
const (
	// Version current version number
	Version = "0.0.3"
)

Variables

View Source
var (
	// Prefix the error prefix, applies to each error's message
	// defaults to Error:_
	Prefix = "Error: "
	// NewLine adds a new line to the end of each error's message
	// defaults to true
	NewLine = true
)

Functions

This section is empty.

Types

type Error

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

Error holds the error message, this message never really changes

Example
fmt.Print(errUserAlreadyExists.Format(userMail))
// first output first Output line
fmt.Print(errUserAlreadyExists.Format(userMail).Append("Please change your mail addr"))
// second output second and third Output lines
Output:

Error: User with mail: user1@mail.go already exists
Error: User with mail: user1@mail.go already exists
Please change your mail addr

func New

func New(errMsg string) *Error

New creates and returns an Error with a pre-defined user output message all methods below that doesn't accept a pointer receiver because actualy they are not changing the original message

func (Error) Append

func (e Error) Append(format string, a ...interface{}) Error

Append adds a message to the predefined error message and returns a new error it does NOT change the original error's message

func (Error) AppendErr

func (e Error) AppendErr(err error) Error

AppendErr adds an error's message to the predefined error message and returns a new error it does NOT change the original error's message

func (Error) Error

func (e Error) Error() string

Error returns the message of the actual error implements the error

func (Error) Format

func (e Error) Format(a ...interface{}) Error

Format returns a formatted new error based on the arguments it does NOT change the original error's message

func (Error) IsAppended

func (e Error) IsAppended() bool

IsAppended returns true if the Error instance is created using original's Error.Append/AppendErr func

func (Error) Panic

func (e Error) Panic()

Panic output the message and after panics

func (Error) Panicf

func (e Error) Panicf(args ...interface{})

Panicf output the formatted message and after panics

func (Error) String

func (e Error) String() string

String returns the error message

func (Error) With

func (e Error) With(err error) error

With does the same thing as Format but it receives an error type which if it's nil it returns a nil error

Jump to

Keyboard shortcuts

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