Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

gofor-little/xerror

Repository files navigation

A package for formatted error stack traces

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version License: MIT 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.