{"id":17321831,"url":"https://github.com/earthboundkid/resperr","last_synced_at":"2025-04-14T16:06:01.992Z","repository":{"id":38325261,"uuid":"274118457","full_name":"earthboundkid/resperr","owner":"earthboundkid","description":"Go package to associate status codes and messages with errors","archived":false,"fork":false,"pushed_at":"2022-06-09T17:51:22.000Z","size":29,"stargazers_count":23,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-06T17:21:54.682Z","etag":null,"topics":["error-handling","go","http"],"latest_commit_sha":null,"homepage":"https://blog.carlmjohnson.net/post/2020/working-with-errors-as/","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/earthboundkid.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":"2020-06-22T11:26:57.000Z","updated_at":"2024-04-22T04:14:47.000Z","dependencies_parsed_at":"2022-08-24T06:30:32.506Z","dependency_job_id":null,"html_url":"https://github.com/earthboundkid/resperr","commit_stats":null,"previous_names":["earthboundkid/resperr","carlmjohnson/resperr"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Fresperr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Fresperr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Fresperr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Fresperr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/earthboundkid","download_url":"https://codeload.github.com/earthboundkid/resperr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240226363,"owners_count":19768068,"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","go","http"],"created_at":"2024-10-15T13:39:53.250Z","updated_at":"2025-04-14T16:06:01.968Z","avatar_url":"https://github.com/earthboundkid.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# resperr [![GoDoc](https://godoc.org/github.com/earthboundkid/resperr?status.svg)](https://godoc.org/github.com/earthboundkid/resperr/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/earthboundkid/resperr/v2)](https://goreportcard.com/report/github.com/earthboundkid/resperr/v2) [![Calver v2.YY.Minor](https://img.shields.io/badge/calver-v2.YY.Minor-22bfda.svg)](https://calver.org)\n\nResperr is a Go package to associate status codes and messages with errors.\n\n## Example usage\n\nSee [blog post](https://blog.carlana.net/post/2020/working-with-errors-as/) for a full description or [read the test code](https://github.com/earthboundkid/resperr/blob/master/example_test.go) for more context:\n\n```go\n// write a simple handler that just checks for errors\n// and replies with an error object if it gets one\n\nfunc myHandler(w http.ResponseWriter, r *http.Request) {\n\t// ... check user permissions...\n\tif err := hasPermissions(r); err != nil {\n\t\treplyError(w, r, err)\n\t\treturn\n\t}\n\t// ...validate request...\n\tn, err := getItemNoFromRequest(r)\n\tif err != nil {\n\t\treplyError(w, r, err)\n\t\treturn\n\t}\n\t// ...get the data ...\n\titem, err := getItemByNumber(n)\n\tif err != nil {\n\t\treplyError(w, r, err)\n\t\treturn\n\t}\n\treplyJSON(w, r, http.StatusOK, item)\n}\n\n// in the functions that your handler calls\n// use resp err to associate different error conditions\n// with appropriate HTTP status codes\n\nfunc getItemByNumber(n int) (item *Item, err error) {\n\titem, err = dbCall(\"...\", n)\n\tif err == sql.ErrNoRows {\n\t\t// this is an anticipated 404\n\t\treturn nil, resperr.New(\n\t\t\thttp.StatusNotFound,\n\t\t\t\"%d not found\", n)\n\t}\n\tif err != nil {\n\t\t// this is an unexpected 500!\n\t\treturn nil, err\n\t}\n\t// ...\n\treturn\n}\n\n// you can also return specific messages for users as needed\n\nfunc getItemNoFromRequest(r *http.Request) (int, error) {\n\tvar v resperr.Validator\n\tns := r.URL.Query().Get(\"n\")\n\tv.AddIf(\"n\", ns == \"\", \"Please enter a number.\")\n\tn, err := strconv.Atoi(ns)\n\tv.AddIfUnset(\"n\", err != nil, \"Input is not a number.\")\n\treturn n, v.Err()\n}\n\n\nfunc hasPermissions(r *http.Request) error {\n\t// lol, don't do this!\n\tuser := r.URL.Query().Get(\"user\")\n\tif user == \"admin\" {\n\t\treturn nil\n\t}\n\treturn resperr.New(http.StatusForbidden,\n\t\t\"bad user %q\", user)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fearthboundkid%2Fresperr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fearthboundkid%2Fresperr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fearthboundkid%2Fresperr/lists"}