{"id":13492908,"url":"https://github.com/darahayes/go-boom","last_synced_at":"2025-03-28T11:31:01.944Z","repository":{"id":53308614,"uuid":"119587147","full_name":"darahayes/go-boom","owner":"darahayes","description":"HTTP Error Generation/Handling Made Simple","archived":false,"fork":false,"pushed_at":"2021-03-31T17:23:28.000Z","size":18,"stargazers_count":32,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-31T07:34:07.009Z","etag":null,"topics":["error-handling","golang","golang-library","http","json"],"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/darahayes.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":"2018-01-30T19:58:04.000Z","updated_at":"2023-04-19T07:43:36.000Z","dependencies_parsed_at":"2022-09-02T03:13:01.769Z","dependency_job_id":null,"html_url":"https://github.com/darahayes/go-boom","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darahayes%2Fgo-boom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darahayes%2Fgo-boom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darahayes%2Fgo-boom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darahayes%2Fgo-boom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darahayes","download_url":"https://codeload.github.com/darahayes/go-boom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246020823,"owners_count":20710826,"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","golang","golang-library","http","json"],"created_at":"2024-07-31T19:01:10.362Z","updated_at":"2025-03-28T11:31:01.697Z","avatar_url":"https://github.com/darahayes.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Boom\n\n[![Documentation](https://godoc.org/github.com/darahayes/go-boom?status.svg)](http://godoc.org/github.com/darahayes/go-boom)\n[![Coverage Status](https://coveralls.io/repos/github/darahayes/go-boom/badge.svg?branch=master)](https://coveralls.io/github/darahayes/go-boom?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/darahayes/go-boom)](https://goreportcard.com/report/github.com/darahayes/go-boom)\n[![CircleCI](https://circleci.com/gh/darahayes/go-boom.svg?style=svg)](https://circleci.com/gh/darahayes/go-boom)\n\n**boom** provides a set of functions for returning HTTP errors. Each function responds with a JSON object which includes the following properties:\n\n- `statusCode` - the HTTP status code.\n- `error`- the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from `statusCode`.\n- `message` - the error message which can be optionally set by the developer.\n\nAll **boom** functions take a `http.ResponseWriter` as an argument which means **boom** should be compatible with any Golang http frameworks that also use `http.ResponseWriter`.\n\nTo see the full list of **boom** functions, check out the [documentation on godoc.org](https://godoc.org/github.com/darahayes/go-boom)\n\nThis library is inspired by the wonderful JavaScript library https://www.npmjs.com/package/boom.\n\n### Install\n\n```bash\ngo get github.com/darahayes/go-boom\n```\n\n### Import\n\n```go\nimport (\n\t\"github.com/darahayes/go-boom\"\n)\n```\n\n## Example Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\t\"github.com/darahayes/go-boom\"\n)\n\nfunc myHandler(w http.ResponseWriter, r *http.Request) {\n\tboom.NotFound(w, \"Sorry, there's nothing here.\")\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", myHandler)\n\thttp.ListenAndServe(\":8080\", nil)\n}\n```\n\nWith this example, the response from the `/` endpoint would be:\n\n```json\n{\n  \"error\": \"Not Found\",\n  \"message\": \"Sorry, there's nothing here.\",\n  \"statusCode\": 404\n}\n```\n\n**boom** also accepts arguments of type `error`, or any `struct` that implements the built in `error` interface. (i.e. has an `Error()` function which returns a `string`)\n\n```go\nfunc myHandler(w http.ResponseWriter, r *http.Request) {\n\terr := errors.New(\"You shall not pass!\")\n\tboom.Unathorized(w, err)\n}\n```\n\nand the response:\n\n```json\n{\n  \"error\": \"Unathorized\",\n  \"message\": \"You shall not pass!\",\n  \"statusCode\": 401\n}\n```\nIt's also possible to provide no error message at all:\n\n```go\nfunc myHandler(w http.ResponseWriter, r *http.Request) {\n\tboom.BadRequest(w)\n}\n```\n\nand the response:\n\n```json\n{\n  \"error\": \"Bad Request\",\n  \"message\": \"Bad Request\",\n  \"statusCode\": 400\n}\n```\n\n## boom.RecoverHandler\n\n**boom** also comes with a `RecoverHandler` middleware function that can be used to recover from unexpected panics.\nIt can be used directly or with any router library that deals with the `http.Handler` type. (most of them do!)\n\n`boom.RecoverHandler` does three things:\n\n1. Recovers from unexpected panics.\n2. Logs a stack trace in the server logs.\n3. Uses `boom.Internal()` to return a generic `500 Internal Server Error`. This prevents accidental leakage of sensitive info in the response.\n\n#### Example usage with plain `http` library\n\n```go\nfunc myHandler(w http.ResponseWriter, r *http.Request) {\n\tpanic(\"Uh oh, something happened\")\n}\n\nfunc main() {\n\thttp.Handle(\"/\", boom.RecoverHandler(http.HandlerFunc(myHandler)))\n\thttp.ListenAndServe(\":8080\", nil)\n}\n```\n\n#### Example usage with the `mux` router\n\n```go\nfunc myHandler(w http.ResponseWriter, r *http.Request) {\n\tpanic(\"Uh oh, something happened\")\n}\n\nfunc main() {\n\trouter := mux.NewRouter().StrictSlash(true)\n\trouter.HandleFunc(\"/\", myHandler)\n\t\n\trouter.Use(boom.RecoverHandler)\n\t\n\thttp.ListenAndServe(\":8080\", router)\n}\n```\n\nIn both examples above, a stack trace is printed in the server logs and the response is the following:\n\n```\n{\n  \"error\": \"Internal Server Error\",\n  \"message\": \"Internal Server Error\",\n  \"statusCode\": 500\n}\n```\n\n## API Methods\n\nTo see the full list of API methods check out the [documentation on godoc.org](https://godoc.org/github.com/darahayes/go-boom)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarahayes%2Fgo-boom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarahayes%2Fgo-boom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarahayes%2Fgo-boom/lists"}