errors

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: Apache-2.0 Imports: 0 Imported by: 13

README

Errors

go get github.com/a-novel/errors

A simple package for better errors handling. It assigns a string ID to each error for better parsing. It is also duck typed with Go error for interoperability.

package myPackage

import
import (
	"fmt"
	"github.com/a-novel/errors"
)

func main() {
    err := errors.New("uniq_id", "something happened")
    
    // ... 
    
    fmt.Print(err.Error()) // something happened
    fmt.Print(err.ID == "uniq_id") // true
    
    switch err.ID {
    case "uniq_id":
        // Do something.
    case "uniq_id_2":
        // Do something else.
    default:
        // Unknown/Unhandled error.
    }
}

Special data

For better readability and handling, you can add some optional data to an error, that will not appear in the error message, but can be handled specifically by some party.

This additional data field is an interface, so you can assign any Go type to it, and retrieve it easily with type assertion.

package myPackage

import
import (
	"fmt"
	"github.com/a-novel/errors"
	"time"
)

func main() {
	err := errors.NewWithData("uniq_id", "something happened", time.Now().UnixNano())

	fmt.Printf("error %s occured at %v timestamp", err.ID, err.Data.(int64)) // true
}

License

Distributed under Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	ID      string      `json:"id"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func New

func New(id, message string) *Error

func NewWithData added in v1.1.0

func NewWithData(id, message string, data interface{}) *Error

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

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