oops

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: MIT Imports: 4 Imported by: 7

README

oops 🙊 GoDoc Go Report Card

oops makes errors in Go traceable. It provides traceback function to get more information when you return error from the function

The convention says that an error should either be handled (whatever that means) or returned to the caller. But with more abstraction, it is difficult to find where the error got originated so Traceable function returns error with more information

Example

package main

import (
	"errors"

	"github.com/5anthosh/oops"
)

func main() {
	err := func1().(*oops.Error)
	println(err.Skip(1).Error())
}

func func1() error {
	return func2()
}

func func2() error {
	return func3()
}

func func3() error {
	return oops.T(errors.New("dummy one")).Info("this is just testing")
}

Run the program
$ go run test.go
🔴  Error : dummy one
ℹ️   Info  : this is just testing
         at main.func3 line 23 Desktop/Files/test.go
         at main.func2 line 19 Desktop/Files/test.go
         at main.func1 line 15 Desktop/Files/test.go
         at main.main line 10 Desktop/Files/test.go

Documentation

Overview

Package oops makes errors in Go traceable. It provides traceback function to get more information when you return error from the function

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func T

func T(err error) error

T add error with more information like stacktrace corresponding to the where function got called

Types

type Error

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

Error is a error with more information

func (*Error) Error

func (err *Error) Error() string

func (*Error) Format added in v1.0.1

func (err *Error) Format(f ErrorFormat) *Error

Format registers format for error strings

func (*Error) Func

func (err *Error) Func(value string) *Error

Func sets function name where error occurred

func (*Error) Info

func (err *Error) Info(value string) *Error

Info lets you add more information about the error

func (*Error) JSON

func (err *Error) JSON() map[string]interface{}

JSON converts Error into json format to for structured logging

func (*Error) Line

func (err *Error) Line(value int) *Error

Line sets line no where error occurred

func (*Error) Origin

func (err *Error) Origin() string

Origin prints where error got originated, not the trace

func (*Error) Skip

func (err *Error) Skip(n int) *Error

Skip skips n functions from bottom of the stack

type ErrorFormat added in v1.0.1

type ErrorFormat func(err string, info string, stacktrace []Stack) string

ErrorFormat let us specify format for error and stacktrace

type Stack added in v1.0.1

type Stack struct {
	File     string `json:"file,omitempty"`
	Line     int    `json:"line,omitempty"`
	FuncName string `json:"func_name,omitempty"`
}

Stack stores single Stack information

Jump to

Keyboard shortcuts

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