{"id":31359370,"url":"https://github.com/perbu/httperrorfmt","last_synced_at":"2025-09-26T23:25:45.480Z","repository":{"id":311442754,"uuid":"1043712353","full_name":"perbu/httperrorfmt","owner":"perbu","description":"Formatting for github.com/perbu/httperror","archived":false,"fork":false,"pushed_at":"2025-08-24T13:24:27.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-24T18:47:32.650Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/perbu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null}},"created_at":"2025-08-24T13:12:10.000Z","updated_at":"2025-08-24T13:24:30.000Z","dependencies_parsed_at":"2025-08-24T18:47:35.172Z","dependency_job_id":"f2c234bc-88cc-4d26-970d-c883e1c4468a","html_url":"https://github.com/perbu/httperrorfmt","commit_stats":null,"previous_names":["perbu/httperrorfmt"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/perbu/httperrorfmt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perbu%2Fhttperrorfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perbu%2Fhttperrorfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perbu%2Fhttperrorfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perbu%2Fhttperrorfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/perbu","download_url":"https://codeload.github.com/perbu/httperrorfmt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perbu%2Fhttperrorfmt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":277156984,"owners_count":25770730,"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","status":"online","status_checked_at":"2025-09-26T02:00:09.010Z","response_time":78,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-09-26T23:25:40.459Z","updated_at":"2025-09-26T23:25:45.473Z","avatar_url":"https://github.com/perbu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# httperrorfmt\n\nA Go package for formatting HTTP errors with content negotiation support.\n\n## Overview\n\nhttperrorfmt provides formatters for HTTP errors that support multiple content types including JSON, HTML, XML, and plain text. It includes automatic content negotiation based on the client's Accept header.\n\nCan be used with [github.com/perbu/httperror](github.com/perbu/httperror).\n\n## Installation\n\n```bash\ngo get github.com/perbu/httperrorfmt\n```\n\n## Usage\n\n### Basic Formatting\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n    \"github.com/perbu/httperrorfmt\"\n)\n\n// Your HTTPError implementation\ntype MyError struct {\n    code    int\n    message string\n    headers map[string]string\n}\n\nfunc (e MyError) Error() string              { return e.message }\nfunc (e MyError) StatusCode() int            { return e.code }\nfunc (e MyError) Message() string            { return e.message }\nfunc (e MyError) Headers() map[string]string { return e.headers }\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n    err := MyError{\n        code:    http.StatusNotFound,\n        message: \"Resource not found\",\n        headers: make(map[string]string),\n    }\n    \n    formatter := \u0026httperrorfmt.JSONFormatter{PrettyPrint: true}\n    formatter.Format(w, r, err)\n}\n```\n\n### Content Negotiation\n\n```go\nfunc handler(w http.ResponseWriter, r *http.Request) {\n    err := MyError{code: 404, message: \"Not found\"}\n    \n    formatter := httperrorfmt.NewContentNegotiatingFormatter()\n    formatter.Format(w, r, err)\n    // Returns JSON for Accept: application/json\n    // Returns HTML for Accept: text/html\n    // Returns plain text otherwise\n}\n```\n\n### Individual Formatters\n\n#### JSON Formatter\n\n```go\nformatter := \u0026httperrorfmt.JSONFormatter{\n    PrettyPrint:  true,\n    IncludeStack: false,\n}\nformatter.Format(w, r, err)\n```\n\n#### HTML Formatter\n\n```go\nformatter := httperrorfmt.NewHTMLFormatter()\nformatter.Format(w, r, err)\n```\n\n#### XML Formatter\n\n```go\nformatter := \u0026httperrorfmt.XMLFormatter{}\nformatter.Format(w, r, err)\n```\n\n#### Text Formatter\n\n```go\nformatter := \u0026httperrorfmt.TextFormatter{}\nformatter.Format(w, r, err)\n```\n\n### Custom Content Negotiation\n\n```go\nnegotiator := httperrorfmt.NewContentNegotiator().\n    Register(\"application/json\", \u0026httperrorfmt.JSONFormatter{PrettyPrint: true}).\n    Register(\"text/html\", httperrorfmt.NewHTMLFormatter()).\n    Register(\"application/xml\", \u0026httperrorfmt.XMLFormatter{}).\n    SetDefault(\u0026httperrorfmt.TextFormatter{})\n\nnegotiator.Format(w, r, err)\n```\n\n## HTTPError Interface\n\nYour error types must implement the HTTPError interface:\n\n```go\ntype HTTPError interface {\n    error\n    StatusCode() int\n    Message() string\n    Headers() map[string]string\n}\n```\n\n## License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperbu%2Fhttperrorfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperbu%2Fhttperrorfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperbu%2Fhttperrorfmt/lists"}