xerror

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 4 Imported by: 6

README

A package for formatted error stack traces

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version GitHub Workflow Status Go Report Card PkgGoDev

Introduction

  • Formatted error stack traces
  • Supports JSON marshaling
  • No dependencies outside the standard library

Example

package main

import (
	"fmt"
	"os"

	"github.com/gofor-little/xerror"
)

func main() {
	if err := RunApplication(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println("application successfully started")
}

func RunApplication() error {
	if err := Initialize(); err != nil {
		return xerror.Wrap("failed to run application", err)
	}

	return nil
}

func Initialize() error {
	if err := LoadConfig(); err != nil {
		return xerror.Wrap("failed to initialize application", err)
	}

	return nil
}

func LoadConfig() error {
	_, err := os.Open("config.json")
	return xerror.Wrap("failed to load config", err)
}

Running the above will output...

main.RunApplication
        /home/ubuntu/xerror/main.go:21: failed to run application
main.Initialize
        /home/ubuntu/xerror/main.go:29: failed to initialize application
main.LoadConfig
        /home/ubuntu/xerror/main.go:37: failed to load config: open config.json: no such file or directory
exit status 1

Or can be marshaled into JSON and output...

{
    "error": {
        "error": {
            "error": "open config.json: no such file or directory",
            "functionName": "main.LoadConfig",
            "fileName": "/home/ubuntu/xerror/cmd/main.go",
            "lineNumber": "39",
            "message": "failed to load config"
        },
        "functionName": "main.Initialize",
        "fileName": "/home/ubuntu/xerror/cmd/main.go",
        "lineNumber": "31",
        "message": "failed to initialize application"
    },
    "functionName": "main.RunApplication",
    "fileName": "/home/ubuntu/xerror/cmd/main.go",
    "lineNumber": "23",
    "message": "failed to run application"
}

Testing

Run go test -v ./... in the root directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Err          error  `json:"error"`
	FunctionName string `json:"functionName"`
	FileName     string `json:"fileName"`
	LineNumber   int    `json:"lineNumber"`
	Message      string `json:"message"`
}

Error wraps an error with additional data that is used to create a stack strace.

func New

func New(message string) *Error

New is a helper function to create a new Error.

func Newf added in v0.2.0

func Newf(message string, args ...interface{}) *Error

Newf is a helper function to create a new Error with formatting.

func Wrap added in v0.4.0

func Wrap(message string, err error) *Error

Wrap is a helper function to wrap another Error.

func Wrapf added in v0.5.0

func Wrapf(message string, err error, args ...interface{}) *Error

Wrap is a helper function to wrap another Error with formatting.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface to provide a formatted stack trace.

func (*Error) MarshalJSON added in v0.2.2

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface to provide a valid JSON output. This is required because errors created from errors.New() do not implement the fmt.Stringer interface and therefore will not print anything.

func (*Error) Unwrap added in v0.3.0

func (e *Error) Unwrap() error

Unwrap implements the Unwrap interface to allow unwrapping of nested errors with errors.Unwrap().

Jump to

Keyboard shortcuts

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