项目作者: gofor-little

项目描述 :
Formatted error stack traces.
高级语言: Go
项目地址: git://github.com/gofor-little/xerror.git
创建时间: 2021-02-19T21:41:15Z
项目社区:https://github.com/gofor-little/xerror

开源协议:MIT License

下载


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

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/gofor-little/xerror"
  6. )
  7. func main() {
  8. if err := RunApplication(); err != nil {
  9. fmt.Println(err)
  10. os.Exit(1)
  11. }
  12. fmt.Println("application successfully started")
  13. }
  14. func RunApplication() error {
  15. if err := Initialize(); err != nil {
  16. return xerror.Wrap("failed to run application", err)
  17. }
  18. return nil
  19. }
  20. func Initialize() error {
  21. if err := LoadConfig(); err != nil {
  22. return xerror.Wrap("failed to initialize application", err)
  23. }
  24. return nil
  25. }
  26. func LoadConfig() error {
  27. _, err := os.Open("config.json")
  28. return xerror.Wrap("failed to load config", err)
  29. }

Running the above will output…

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

Or can be marshaled into JSON and output…

  1. {
  2. "error": {
  3. "error": {
  4. "error": "open config.json: no such file or directory",
  5. "functionName": "main.LoadConfig",
  6. "fileName": "/home/ubuntu/xerror/cmd/main.go",
  7. "lineNumber": "39",
  8. "message": "failed to load config"
  9. },
  10. "functionName": "main.Initialize",
  11. "fileName": "/home/ubuntu/xerror/cmd/main.go",
  12. "lineNumber": "31",
  13. "message": "failed to initialize application"
  14. },
  15. "functionName": "main.RunApplication",
  16. "fileName": "/home/ubuntu/xerror/cmd/main.go",
  17. "lineNumber": "23",
  18. "message": "failed to run application"
  19. }

Testing

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