errors

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: MIT Imports: 5 Imported by: 0

README

Golage errors handling

Simple and useful error handling package in Golang

Features

  • Fundamental
  • Support stacktrace
  • Wrap existing errors
  • Easy and pretty error handling
  • Support serialization (for transports in grpc, api, etc...)
  • Support stacktrace errors in github.com/pkg/errors
  • Handling error types with code numbers (you can extends with constants)

Installation

Get from Github:

go get github.com/golage/errors

How to use

Import into your code:

import "github.com/golage/errors"

Create error instance:

errors.New(errors.CodeNotFound, "somethings")
errors.New(errors.CodeNotFound, "somethings %v", 123)

Wrap existing error:

errors.Wrap(err, errors.CodeInternal, "somethings")
errors.Wrap(err, errors.CodeInternal, "somethings %v", 123)

Handle error:

switch err, code := errors.Parse(err); code {
case errors.CodeNil:
case errors.CodeNotFound:
    log.Fatalf("not found: %v", err)
default:
    log.Fatalf("others: %v\n%v", err, err.StackTrace())
}

For more see example

Documentation

Overview

Package errors implements for error handling simple and useful

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(err error) (Fundamental, Code)

Parse returns fundamental error and code from all of error types

Types

type Code

type Code uint

Code implements error code number, you can extends this with define constants with this type

const (
	// CodeNil means no error exists
	CodeNil Code = iota

	// CodeUnimplemented means code does not implemented
	CodeUnimplemented

	// CodeUnknown means unknown error raised
	CodeUnknown

	// CodeInternal means internal module returns error
	CodeInternal

	// CodeUnauthenticated means user does not authenticated
	CodeUnauthenticated

	// CodePermissionDenied means user does not have access permissions
	CodePermissionDenied

	// CodeInvalidData means input parameters are invalid
	CodeInvalidData

	// CodeNotFound means there is no data found
	CodeNotFound

	// CodeAlreadyExists means there is same data is already exists
	CodeAlreadyExists
)

type Fundamental

type Fundamental interface {
	error

	// Code returns error code
	Code() Code

	// Message returns error message
	Message() string

	// Stacktrace returns error stacktrace
	StackTrace() stacktrace.Stacktrace
}

Fundamental interface of fundamental error

func Cast added in v1.2.1

func Cast(err error, code Code) Fundamental

Cast returns new instance fundamental error with error cause and code

func New

func New(code Code, message string, args ...interface{}) Fundamental

New returns new instance fundamental error with args

func Wrap

func Wrap(cause error, code Code, message string, args ...interface{}) Fundamental

Wrap returns new instance fundamental error with args from error cause

Directories

Path Synopsis
Package stacktrace is implements for capture errors stacktrace
Package stacktrace is implements for capture errors stacktrace

Jump to

Keyboard shortcuts

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