{"id":36669000,"url":"https://github.com/morikuni/failure","last_synced_at":"2026-01-12T10:38:17.664Z","repository":{"id":33912256,"uuid":"137718411","full_name":"morikuni/failure","owner":"morikuni","description":"failure is a utility package for handling application errors.","archived":false,"fork":false,"pushed_at":"2024-04-19T00:40:59.000Z","size":201,"stargazers_count":363,"open_issues_count":1,"forks_count":4,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-11-17T12:11:10.363Z","etag":null,"topics":["error","error-handling","go","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/morikuni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"morikuni"}},"created_at":"2018-06-18T06:46:34.000Z","updated_at":"2024-10-04T03:46:41.000Z","dependencies_parsed_at":"2024-06-18T17:00:31.901Z","dependency_job_id":null,"html_url":"https://github.com/morikuni/failure","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/morikuni/failure","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Ffailure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Ffailure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Ffailure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Ffailure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morikuni","download_url":"https://codeload.github.com/morikuni/failure/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Ffailure/sbom","scorecard":{"id":660155,"data":{"date":"2025-08-11","repo":{"name":"github.com/morikuni/failure","commit":"2551069d1c86207036f771de71a293e08c5e47a5"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.8,"checks":[{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/morikuni/failure/test.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/morikuni/failure/test.yml/main?enable=pin","Info:   0 out of   1 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/test.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/1 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 30 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-21T15:53:34.379Z","repository_id":33912256,"created_at":"2025-08-21T15:53:34.379Z","updated_at":"2025-08-21T15:53:34.379Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","error-handling","go","golang"],"created_at":"2026-01-12T10:38:17.556Z","updated_at":"2026-01-12T10:38:17.641Z","avatar_url":"https://github.com/morikuni.png","language":"Go","funding_links":["https://github.com/sponsors/morikuni"],"categories":[],"sub_categories":[],"readme":"# failure\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/morikuni/failure/v2.svg)](https://pkg.go.dev/github.com/morikuni/failure/v2)\n\nPackage `failure` is an error handling library for Go. It allows you to create, wrap, and handle errors with additional context and features.\n\n## Features\n\n- Create errors with error codes to easily classify and handle errors.\n- Wrap errors with additional context such as function parameters and key-value data.\n- Automatically capture call stack information for debugging.\n- Flexible error formatting for both developers and end users.\n- Utility functions to extract error codes, messages, and other metadata from errors.\n\n## Installation\n\nTo install `failure`, use the following command:\n\n```\ngo get github.com/morikuni/failure/v2\n```\n\n## Usage Examples\n\nFirst, define your application's error codes:\n\n```go\ntype ErrorCode string\n\nconst (\n    ErrNotFound ErrorCode = \"NotFound\"\n    ErrInvalidArgument ErrorCode = \"InvalidArgument\"\n)\n```\n\nUse `failure.New` to create a new error with an error code:\n\n```go\nerr := failure.New(ErrNotFound, failure.Message(\"Resource not found\"))\n```\n\nUse `failure.Wrap` to wrap an existing error with additional context:\n\n```go\nif err != nil {\n    return failure.Wrap(err, failure.Context{\"parameter\": \"value\"})\n}\n```\n\nUse `failure.Is` to check for a specific error code and handle the error:\n\n```go\nif failure.Is(err, ErrNotFound) {\n    // Handle ErrNotFound error\n}\n```\n\nUse utility functions to extract metadata from the error:\n\n```go\ncode := failure.CodeOf(err)\nmessage := failure.MessageOf(err)\ncallStack := failure.CallStackOf(err)\n```\n\nExample error outputs:\n\n```go\nerr := failure.New(ErrInvalidArgument, failure.Message(\"Invalid argument\"), failure.Context{\"userId\": \"123\"})\nfmt.Println(err) \n// Output: GetUser[InvalidArgument](Invalid argument, {userId=123})\n\nfmt.Printf(\"%+v\\n\", err)\n// Output:\n// [main.GetUser] /path/to/file.go:123\n//     InvalidArgument\n//     Invalid argument\n//     {userId=123}\n// [CallStack]\n//     [main.GetUser] /path/to/file.go:123\n//     [main.main] /path/to/main.go:456\n```\n\nFor more detailed usage and examples, refer to the [Go Reference](https://pkg.go.dev/github.com/morikuni/failure).\n\n## Full Example of Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"net/http/httptest\"\n\t\"net/http/httputil\"\n\n\t\"github.com/morikuni/failure/v2\"\n)\n\ntype ErrorCode string\n\nconst (\n\tNotFound  ErrorCode = \"NotFound\"\n\tForbidden ErrorCode = \"Forbidden\"\n)\n\nfunc GetACL(projectID, userID string) (acl interface{}, e error) {\n\tnotFound := true\n\tif notFound {\n\t\treturn nil, failure.New(NotFound,\n\t\t\tfailure.Context{\"project_id\": projectID, \"user_id\": userID},\n\t\t)\n\t}\n\treturn nil, failure.Unexpected(\"unexpected error\")\n}\n\nfunc GetProject(projectID, userID string) (project interface{}, e error) {\n\t_, err := GetACL(projectID, userID)\n\tif err != nil {\n\t\tif failure.Is(err, NotFound) {\n\t\t\treturn nil, failure.Translate(err, Forbidden,\n\t\t\t\tfailure.Message(\"no acl exists\"),\n\t\t\t\tfailure.Context{\"additional_info\": \"hello\"},\n\t\t\t)\n\t\t}\n\t\treturn nil, failure.Wrap(err)\n\t}\n\treturn nil, nil\n}\n\nfunc Handler(w http.ResponseWriter, r *http.Request) {\n\t_, err := GetProject(r.FormValue(\"project_id\"), r.FormValue(\"user_id\"))\n\tif err != nil {\n\t\tHandleError(w, err)\n\t\treturn\n\t}\n}\n\nfunc getHTTPStatus(err error) int {\n\tswitch failure.CodeOf(err) {\n\tcase NotFound:\n\t\treturn http.StatusNotFound\n\tcase Forbidden:\n\t\treturn http.StatusForbidden\n\tdefault:\n\t\treturn http.StatusInternalServerError\n\t}\n}\n\nfunc getMessage(err error) string {\n\tmsg := failure.MessageOf(err)\n\tif msg != \"\" {\n\t\treturn string(msg)\n\t}\n\treturn \"Error\"\n}\n\nfunc HandleError(w http.ResponseWriter, err error) {\n\tw.WriteHeader(getHTTPStatus(err))\n\tio.WriteString(w, getMessage(err))\n\n\tfmt.Println(\"============ Error ============\")\n\tfmt.Printf(\"Error = %v\\n\", err)\n\t// Error = main.GetProject[Forbidden](no acl exists, {additional_info=hello}): main.GetACL[NotFound]({project_id=aaa,user_id=111})\n\n\tcode := failure.CodeOf(err)\n\tfmt.Printf(\"Code = %v\\n\", code)\n\t// Code = Forbidden\n\n\tmsg := failure.MessageOf(err)\n\tfmt.Printf(\"Message = %v\\n\", msg)\n\t// Message = no acl exists\n\n\tcs := failure.CallStackOf(err)\n\tfmt.Printf(\"CallStack = %v\\n\", cs)\n\t// CallStack = main.GetACL: main.GetProject: main.Handler: main.main: runtime.main: goexit\n\n\tfmt.Printf(\"Cause = %v\\n\", failure.CauseOf(err))\n\t// Cause = main.GetACL[NotFound]({project_id=aaa,user_id=111})\n\n\tfmt.Println()\n\tfmt.Println(\"============ Detail ============\")\n\tfmt.Printf(\"%+v\\n\", err)\n\t// [main.GetProject] /go/src/github.com/morikuni/failure/example/main.go:34\n\t//     Forbidden\n\t//     no acl exists\n\t//     {additional_info=hello}\n\t// [main.GetACL] /go/src/github.com/morikuni/failure/example/main.go:23\n\t//     NotFound\n\t//     {user_id=111,project_id=aaa}\n\t// [CallStack]\n\t//     [main.GetACL] /go/src/github.com/morikuni/failure/example/main.go:23\n\t//     [main.GetProject] /go/src/github.com/morikuni/failure/example/main.go:31\n\t//     [main.Handler] /go/src/github.com/morikuni/failure/example/main.go:45\n\t//     [main.main] /go/src/github.com/morikuni/failure/example/main.go:119\n\t//     [runtime.main] /opt/homebrew/opt/go/libexec/src/runtime/proc.go:271\n\t//     [runtime.goexit] /opt/homebrew/opt/go/libexec/src/runtime/asm_arm64.s:1222\t\n}\n\nfunc main() {\n\treq := httptest.NewRequest(http.MethodGet, \"/?project_id=aaa\u0026user_id=111\", nil)\n\trec := httptest.NewRecorder()\n\tHandler(rec, req)\n\n\tres, _ := httputil.DumpResponse(rec.Result(), true)\n\tfmt.Println(\"============ Dump ============\")\n\tfmt.Println(string(res))\n}\n```\n\n## Migration from v1 to v2\n\nSee [docs/v1-to-v2.md](docs/v1-to-v2.md) for migration guide.\n\n## Contributing\n\nContributions are welcome! Feel free to send issues or pull requests.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorikuni%2Ffailure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorikuni%2Ffailure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorikuni%2Ffailure/lists"}