{"id":22322461,"url":"https://github.com/josestg/problemdetail","last_synced_at":"2025-03-26T05:11:25.720Z","repository":{"id":200281021,"uuid":"704592355","full_name":"josestg/problemdetail","owner":"josestg","description":"An idiomatic implementation of Problem Details for HTTP APIs (RFC 7807) for Go.","archived":false,"fork":false,"pushed_at":"2023-10-15T08:26:58.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T06:47:03.759Z","etag":null,"topics":["http-error-handling","problem-details","problem-details-for-http-apis"],"latest_commit_sha":null,"homepage":"https://github.com/josestg/problemdetail","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/josestg.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,"governance":null}},"created_at":"2023-10-13T15:35:40.000Z","updated_at":"2023-10-17T02:37:42.000Z","dependencies_parsed_at":"2023-10-16T12:09:02.085Z","dependency_job_id":null,"html_url":"https://github.com/josestg/problemdetail","commit_stats":null,"previous_names":["josestg/problemdetail"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josestg%2Fproblemdetail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josestg%2Fproblemdetail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josestg%2Fproblemdetail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josestg%2Fproblemdetail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josestg","download_url":"https://codeload.github.com/josestg/problemdetail/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245591624,"owners_count":20640692,"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":["http-error-handling","problem-details","problem-details-for-http-apis"],"created_at":"2024-12-04T01:07:28.214Z","updated_at":"2025-03-26T05:11:25.700Z","avatar_url":"https://github.com/josestg.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Problem Details\n\nThis package implements [RFC 7807](https://datatracker.ietf.org/doc/html/rfc7807) (Problem Details for HTTP APIs) for Go. \nIt provides an idiomatic way to use RFC 7807 in Go and offers both JSON and XML writers.\n\n## Installation\n\n```bash\ngo get github.com/josestg/problemdetail\n```\n\n## Examples\n\n### Problem Details as an Error\n\n```go\nconst (\n\tTypOutOfCredit     = \"https://example.com/probs/out-of-credit\"\n\tTypProductNotFound = \"https://example.com/probs/product-not-found\"\n)\n\nfunc service() error {\n\t// do something...\n\n\t// simulate 30% error rate for each type of error.\n\tn := rand.Intn(9)\n\tif n \u003c 3 {\n\t\treturn problemdetail.New(TypOutOfCredit,\n\t\t\tproblemdetail.WithValidateLevel(problemdetail.LStandard),\n\t\t\tproblemdetail.WithTitle(\"You do not have enough credit.\"),\n\t\t\tproblemdetail.WithDetail(\"Your current balance is 30, but that costs 50.\"),\n\t\t)\n\t}\n\n\tif n \u003c 6 {\n\t\treturn problemdetail.New(TypProductNotFound,\n\t\t\tproblemdetail.WithValidateLevel(problemdetail.LStandard),\n\t\t\tproblemdetail.WithTitle(\"The product was not found.\"),\n\t\t\tproblemdetail.WithDetail(\"The product you requested was not found in the system.\"),\n\t\t)\n\t}\n\n\treturn errors.New(\"unknown error\")\n}\n\n// handler is a sample handler for HTTP server.\n// you can make this as a centralized error handler middleware.\nfunc handler(w http.ResponseWriter, _ *http.Request) {\n\terr := service()\n\tif err != nil {\n\t\t// read the error as problemdetail.ProblemDetailer.\n\t\tvar pd problemdetail.ProblemDetailer\n\t\tif !errors.As(err, \u0026pd) {\n\t\t\t// untyped error for generic error handling.\n\t\t\tuntyped := problemdetail.New(\n\t\t\t\tproblemdetail.Untyped,\n\t\t\t\tproblemdetail.WithValidateLevel(problemdetail.LStandard),\n\t\t\t)\n\t\t\t_ = problemdetail.WriteJSON(w, untyped, http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\n\t\t// typed error for specific error handling.\n\t\tswitch pd.Kind() {\n\t\tcase TypOutOfCredit:\n\t\t\t_ = problemdetail.WriteJSON(w, pd, http.StatusForbidden)\n\t\t\t// or problemdetail.WriteXML(w, pd, http.StatusForbidden) for XML\n\t\tcase TypProductNotFound:\n\t\t\t_ = problemdetail.WriteJSON(w, pd, http.StatusNotFound)\n\t\t}\n\t\treturn\n\t}\n\tw.WriteHeader(http.StatusOK)\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", handler)\n\tlog.Fatal(http.ListenAndServe(\":8080\", nil))\n}\n```\n\n### Problem Details with Extensions\n\n```go\nconst (\n\tTypOutOfCredit     = \"https://example.com/probs/out-of-credit\"\n\tTypProductNotFound = \"https://example.com/probs/product-not-found\"\n)\n\n// BalanceProblemDetail is a sample problem detail with extension by embedding ProblemDetail.\ntype BalanceProblemDetail struct {\n\t*problemdetail.ProblemDetail\n\tBalance  int64    `json:\"balance\" xml:\"balance\"`\n\tAccounts []string `json:\"accounts\" xml:\"accounts\"`\n}\n\nfunc service() error {\n\t// do something...\n\n\t// simulate 30% error rate for each type of error.\n\tn := rand.Intn(9)\n\tif n \u003c 3 {\n\t\tpd := problemdetail.New(TypOutOfCredit,\n\t\t\tproblemdetail.WithValidateLevel(problemdetail.LStandard),\n\t\t\tproblemdetail.WithTitle(\"You do not have enough credit.\"),\n\t\t\tproblemdetail.WithDetail(\"Your current balance is 30, but that costs 50.\"),\n\t\t)\n\t\treturn \u0026BalanceProblemDetail{\n\t\t\tProblemDetail: pd,\n\t\t\tBalance:       30,\n\t\t\tAccounts:      []string{\"/account/12345\", \"/account/67890\"},\n\t\t}\n\t}\n\n\tif n \u003c 6 {\n\t\treturn problemdetail.New(TypProductNotFound,\n\t\t\tproblemdetail.WithValidateLevel(problemdetail.LStandard),\n\t\t\tproblemdetail.WithTitle(\"The product was not found.\"),\n\t\t\tproblemdetail.WithDetail(\"The product you requested was not found in the system.\"),\n\t\t)\n\t}\n\n\treturn errors.New(\"unknown error\")\n}\n\n// handler is a sample handler for HTTP server.\n// you can make this as a centralized error handler middleware.\nfunc handler(w http.ResponseWriter, _ *http.Request) {\n\terr := service()\n\tif err != nil {\n\t\t// read the error as problemdetail.ProblemDetailer.\n\t\tvar pd problemdetail.ProblemDetailer\n\t\tif !errors.As(err, \u0026pd) {\n\t\t\t// untyped error for generic error handling.\n\t\t\tuntyped := problemdetail.New(\n\t\t\t\tproblemdetail.Untyped,\n\t\t\t\tproblemdetail.WithValidateLevel(problemdetail.LStandard),\n\t\t\t)\n\t\t\t_ = problemdetail.WriteJSON(w, untyped, http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\n\t\t// typed error for specific error handling.\n\t\tswitch pd.Kind() {\n\t\tcase TypOutOfCredit:\n\t\t\t_ = problemdetail.WriteJSON(w, pd, http.StatusForbidden)\n\t\t\t// or problemdetail.WriteXML(w, pd, http.StatusForbidden) for XML\n\t\tcase TypProductNotFound:\n\t\t\t_ = problemdetail.WriteJSON(w, pd, http.StatusNotFound)\n\t\t}\n\t\treturn\n\t}\n\tw.WriteHeader(http.StatusOK)\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", handler)\n\tlog.Fatal(http.ListenAndServe(\":8080\", nil))\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosestg%2Fproblemdetail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosestg%2Fproblemdetail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosestg%2Fproblemdetail/lists"}