{"id":16280971,"url":"https://github.com/corebreaker/goerrors","last_synced_at":"2025-10-25T04:02:13.643Z","repository":{"id":57502413,"uuid":"72948051","full_name":"corebreaker/goerrors","owner":"corebreaker","description":"Easy error informations and stack traces for Go with rich informations and a Try/Catch/Finally mechanism.","archived":false,"fork":false,"pushed_at":"2019-06-02T14:09:53.000Z","size":89,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-22T11:29:13.366Z","etag":null,"topics":["error-handling","error-reporting","errors","go","golang","try-catch"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/corebreaker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-05T19:17:46.000Z","updated_at":"2019-05-19T13:38:30.000Z","dependencies_parsed_at":"2022-09-13T07:02:17.380Z","dependency_job_id":null,"html_url":"https://github.com/corebreaker/goerrors","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corebreaker%2Fgoerrors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corebreaker%2Fgoerrors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corebreaker%2Fgoerrors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corebreaker%2Fgoerrors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/corebreaker","download_url":"https://codeload.github.com/corebreaker/goerrors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238878945,"owners_count":19545855,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handling","error-reporting","errors","go","golang","try-catch"],"created_at":"2024-10-10T19:04:16.862Z","updated_at":"2025-10-25T04:02:08.594Z","avatar_url":"https://github.com/corebreaker.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GoErrors - Easy error informations and stack traces for Go with rich informations and a Try/Catch/Finally mechanism.\n[![Report](https://goreportcard.com/badge/github.com/corebreaker/goerrors?style=plastic)](https://goreportcard.com/report/github.com/corebreaker/goerrors)\n[![Build Status](https://img.shields.io/travis/com/corebreaker/goerrors/master.svg?style=plastic)](https://travis-ci.com/corebreaker/goerrors)\n[![Coverage Status](https://img.shields.io/coveralls/github/corebreaker/goerrors/master.svg?style=plastic)](https://coveralls.io/github/corebreaker/goerrors)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=plastic)](https://godoc.org/github.com/corebreaker/goerrors)\n[![GitHub license](https://img.shields.io/github/license/corebreaker/goerrors.svg?color=blue\u0026style=plastic)](https://github.com/corebreaker/goerrors/blob/master/LICENSE)\n[![Release](https://img.shields.io/github/release/corebreaker/goerrors.svg?style=plastic)](https://github.com/corebreaker/goerrors/releases)\n[![Dependency status](https://img.shields.io/librariesio/github/corebreaker/goerrors.svg?style=plastic)](https://libraries.io/github/corebreaker/goerrors)\n\nIt's a package that's allow you to make Go errors more comprehensive, more featured and easier to use.\nThis project is the realisation of the idea introduced by the GIST [try-catch-finally.go](https://gist.github.com/corebreaker/6a93c8210425e96dc1bcbb157f0270b0).\n\n## Features\n- Verbose with stack trace\n- Ready for Try/Catch/Finally paradigm\n- Extensible with your own error\n- Error hierarchy\n- Entirely customizable\n\n\n## Installation\ngo get github.com/corebreaker/goerrors\n\n\n## How is it work ?\nA normal error is just an interface. But here we added an extended interface IError which can transport other infomations.\nTherefore to add informations, the standard error (`error` interface) is \u0026laquo;decorated\u0026raquo; with informations and so\nwe obtain a new error value. These informations are:\n- stack trace informations (files + lines)\n- additionnal customized string\n- optionnal error code\n\nWhen the `Error` method is called, all these informations will be formated in a string.\n\nThis type of error gives you rich informations on an error without using `panic` function (even if with a panic, you\ncould show your additionnal infomations).\n\n\n## How to use it ?\n### Decorate error to add a stack trace\n```go\n// for example, let's open a file\nfunc OpenMyFile() (*os.File, error) {\n    file, err := os.Open(\"MyFile.txt\")\n    if err != nil {\n        // Here, we decorate the error\n        return nil, goerrors.DecorateError(err)\n    }\n\n    return file, nil\n}\n```\n\nThen, we can `panic` this decorated error or simply print it, like this:\n```go\nfunc main() {\n    // First we must enable the debug mode to activate the stacktrace\n    goerrors.SetDebug(true)\n\n    // Use a function that use the `goerrors` package\n    file, err := OpenMyFile()\n\n    // Check the error\n    if err != nil {\n        // Show the error\n        fmt.Println(err)\n\n        // Terminate\n        return\n    }\n\n    // …\n}\n```\n\nYou will see some thing like this:\n```\ngithub.com/corebreaker/goerrors.tStandardError: open MyFile.txt: no such file or directory\n    github.com/corebreaker/goerrors.(*GoError).Init (/home/frederic/go/src/github.com/corebreaker/goerrors/errors.go:219)\n    github.com/corebreaker/goerrors.DecorateError (/home/frederic/go/src/github.com/corebreaker/goerrors/standard.go:51)\n    main.OpenMyFile (/home/frederic/.local/share/data/liteide/liteide/goplay.go:13)\n    main.main (/home/frederic/.local/share/data/liteide/liteide/goplay.go:24)\n------------------------------------------------------------------------------\n```\n\n### A Try/Catch/Finally mechanism\nPlus, this library uses the `panic()` function, the `recover()` function and the `defer` instruction,\nas a Throw and a Try/Catch/Finally mechanisms and can be used like this:\n```go\ngoerrors.Try(func(err goerrors.IError) error {\n    // Try block\n}, func(err goerrors.IError) error {\n    // Catch block\n}, func(err goerrors.IError) error {\n    // Finally block\n})\n```\n\nThe error passed in `Try` block is the error which called the `Try` method:\n```go\ntheError := goerrors.MakeError(\"the error in `Try` block\")\ntheError.Try(func(err goerrors.IError) error {\n\t// Here err === theError\n\t\n\treturn nil\n})\n```\n\nIn the case in using the `Try` function in the GoError package, the error passed as argument is an error created by the\ncall the `Try` function. Then, that error can be customized with the GoError API.\n\n#### An example with a throw, called \"raise\" here\n\nActually, returning an error in the `Try` block is a `Throw`, and a Go `panic` call is too like a throw but there is\na `panic`-like function for keeping Try/Catch formalism, the `Raise` function, used like that:\n```go\ngoerrors.Try(func(err goerrors.IError) error {\n\tif aCondition {\n        // `Raise` call\n        goerrors.Raise(\"an error\")\n    }\n    \n    // Do something\n    \n    return nil\n}, func(err goerrors.IError) error {\n    // Catch block\n}, func(err goerrors.IError) error {\n    // Finally block\n})\n```\n\nAt last, all errors generated by GoError have a `Raise` method. So, you can throw an error like that:\n```go\ngoerrors.Try(func(err goerrors.IError) error {\n    if aCondition {\n        // `Raise` method call\n        goerrors.MakeError(\"an error\").Raise()\n    }\n    \n    // Do something\n    \n    return nil\n}, func(err goerrors.IError) error {\n    // Catch block\n}, func(err goerrors.IError) error {\n    // Finally block\n})\n```\n\n## A simple example\n```go\npackage main\n\nimport (\n    \"fmt\"\n    gerr \"github.com/corebreaker/goerrors\"\n)\n\n// A function which return checked quotient\nfunc my_func(i, j int) (int, error) {\n    if j == 0 {\n        return 0, gerr.MakeError(\"Division by zero\")\n    }\n\n    return i / j, nil\n}\n\n// Main function\nfunc main() {\n    // Activate stack trace\n    gerr.SetDebug(true)\n\n    i, j := 1, 0\n\n    // Call checked function\n    q, err := my_func(i, j)\n    if err != nil {\n        fmt.Println(err)\n\n        return\n    }\n\n    // Here, in this example, this code won't never be executed\n    fmt.Print(i, \"/\", j, \"=\", q)\n}\n```\n\nThis will show:\n```\nStandardError: Division by zero\n    main.my_func (/projects/go/prototype/main.go:11)\n    main.main (/projects/go/prototype/main.go:23)\n------------------------------------------------------------------------------\n```\n\n\n## Another example with existing error\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"os\"\n    gerr \"github.com/corebreaker/goerrors\"\n)\n\n// A function which open a file\nfunc openFile(name string) (*os.File, error) {\n    f, err := os.Open(name)\n\n    // Decorate the opening error\n    if err != nil {\n        return nil, gerr.DecorateError(err)\n    }\n\n    return f, nil\n}\n\n// A function which read one byte in the opened file\nfunc readFile(f *os.File) (byte, error) {\n    var b [1]byte\n\n    n, err := f.Read(b[:])\n\n    // Decorate the read error\n    if err != nil {\n        return 0, gerr.DecorateError(err)\n    }\n\n    // Return custom error\n    if n == 0 {\n        return 0, gerr.MakeError(\"No data to read\")\n    }\n\n    return b[0], nil\n}\n\n// Main function\nfunc main() {\n    // Activate stack trace\n    gerr.SetDebug(true)\n\n    // Call the checked open function\n    f, err := openFile(\"a_file.txt\")\n    if err != nil {\n        fmt.Println(err)\n\n        return\n    }\n\n    // Here, in this example, this code won't never be executed if the file can't be opened\n    defer f.Close()\n\n    _, err = readFile(f)\n}\n```\n\nThis will show:\n```\nStandardError: open a_file.txt: no such file or directory\n    main.open_file (/projects/go/src/github.com/corebreaker/goerrors.go:15)\n    main.main (/projects/go/src/github.com/corebreaker/goerrors.go:46)\n------------------------------------------------------------------------------\n```\n\n\n### A Try/Catch example with error inheritance\n```go\npackage main\n\nimport (\n    \"fmt\"\n    gerr \"github.com/corebreaker/goerrors\"\n)\n\ntype ErrorBase struct{ gerr.GoError }\n\ntype ErrorA struct{ ErrorBase }\ntype ErrorB struct{ ErrorBase }\n\ntype ChildError struct{ ErrorA }\n\nfunc newErrorBase() gerr.IError {\n    err := \u0026ErrorBase{}\n\n    return err.Init(err, \"message from Error base\", nil, nil, 0)\n}\n\nfunc newErrorB() gerr.IError {\n    err := \u0026ErrorB{}\n\n    return err.Init(err, \"message from Error B\", nil, nil, 0)\n}\n\nfunc newChildError() gerr.IError {\n    err := \u0026ChildError{}\n\n    return err.Init(err, \"message from Child Error\", nil, nil, 0)\n}\n\n// A function which raise and try to catch the error which is not in the same hierarchy\nfunc myFunc() () {\n    _ = newErrorB().Try(func(err gerr.IError) error {\n        newChildError().Raise()\n\n        return nil\n    }, func(err gerr.IError) error {\n        // This catch block will not called because ErrorB is not in the same error hierarchy of ChildError\n        return nil\n    }, nil)\n}\n\n// Main function\nfunc main() {\n    _ = newErrorBase().Try(func(err gerr.IError) error {\n        myFunc()\n\n        return nil\n    }, func(err gerr.IError) error {\n        fmt.Println(\"Catched error:\", err)\n\n        return nil\n    }, nil)\n}\n```\n\nThis will show:\n```\nCatched error: main.ChildError: message from Child Error\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorebreaker%2Fgoerrors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorebreaker%2Fgoerrors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorebreaker%2Fgoerrors/lists"}