{"id":18561035,"url":"https://github.com/utilyre/xmate","last_synced_at":"2026-02-08T22:02:13.377Z","repository":{"id":195332707,"uuid":"692684549","full_name":"utilyre/xmate","owner":"utilyre","description":"Enhanced error handling for the net/http package","archived":false,"fork":false,"pushed_at":"2024-11-11T19:29:11.000Z","size":51,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-08T13:12:10.872Z","etag":null,"topics":["error","error-handling","go","golang","http","httperr","lib","library","middleware","net-http","util","utility","utils"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/utilyre/xmate/v3","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/utilyre.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-17T09:02:06.000Z","updated_at":"2025-03-03T14:13:40.000Z","dependencies_parsed_at":"2023-09-28T10:57:07.446Z","dependency_job_id":"3aacf643-ee5e-4417-95dd-3b2d094e6efb","html_url":"https://github.com/utilyre/xmate","commit_stats":null,"previous_names":["utilyre/xmate"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/utilyre/xmate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utilyre%2Fxmate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utilyre%2Fxmate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utilyre%2Fxmate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utilyre%2Fxmate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/utilyre","download_url":"https://codeload.github.com/utilyre/xmate/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utilyre%2Fxmate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29246428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T21:42:34.334Z","status":"ssl_error","status_checked_at":"2026-02-08T21:41:38.468Z","response_time":57,"last_error":"SSL_read: 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","http","httperr","lib","library","middleware","net-http","util","utility","utils"],"created_at":"2024-11-06T22:05:32.689Z","updated_at":"2026-02-08T22:02:13.340Z","avatar_url":"https://github.com/utilyre.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xmate\n\nxmate is a Go library designed to enhance the standard `net/http` package by \nproviding convenient functionality specifically focused on error handling in \nHTTP handlers.\n\nThe name \"xmate\" signifies \"friend of mux,\" highlighting its role in \nsimplifying the creation of HTTP handlers. While it doesn't enhance routing in \nthe standard library, xmate makes writing handlers less verbose, allowing \ndevelopers to focus on building applications more efficiently and with less \nboilerplate code.\n\n## Features\n\n- **Less Boilerplate Code**: Streamline your HTTP handler development with\n\treduced boilerplate, allowing you to write cleaner and more maintainable code.\n\n- **Error Handling**: Convert HTTP handlers that return errors into standard\n\t`net/http` handlers, simplifying error management and ensuring consistent\n\tresponses.\n\n- **Multiple Error Handlers**: Easily define different error handlers for\n\tvarious types of HTTP endpoints, providing tailored responses based on the\n\tspecific context of each request.\n\n- **Ease of Use**: Enjoy a user-friendly experience that simplifies the process\n\tof creating and managing HTTP handlers, making it accessible for developers\n\tof all skill levels.\n\n## Usage\n\n### Handlers\n\nIn the simplest scenario, wrap HTTP handlers and functional HTTP handlers with\n`xmate.Handle` and `xmate.HandleFunc`, respectively.\n\n\u003e [!NOTE]\n\u003e These top-level functions convert the given HTTP handler into the standard\n\u003e `http.Handler` or `http.HandlerFunc` (depending on the function used) by\n\u003e handling its error.\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/utilyre/xmate/v3\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\tsh := statusHandler{\n\t\tBrief:       \"healthy\",\n\t\tDescription: \"service is ready to accept connections\",\n\t}\n\n\tmux.HandleFunc(\"GET /{$}\", xmate.HandleFunc(handleHelloWorld))\n\tmux.Handle(\"GET /status\", xmate.Handle(sh))\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", mux))\n}\n\nfunc handleHelloWorld(w http.ResponseWriter, r *http.Request) error {\n\treturn xmate.WriteText(w, http.StatusOK, \"hello world\")\n}\n\ntype statusHandler struct {\n\tBrief       string\n\tDescription string\n}\n\nfunc (sh statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {\n\treturn xmate.WriteJSON(w, http.StatusOK, map[string]any{\n\t\t\"brief\":       sh.Brief,\n\t\t\"description\": sh.Description,\n\t})\n}\n```\n\n### Middlewares\n\nInstead of wrapping the middleware itself, wrap the function returned from it\nwith `xmate.HandleFunc`.\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/go-chi/chi/v5\"\n\t\"github.com/utilyre/xmate/v3\"\n)\n\nfunc main() {\n\tr := chi.NewRouter()\n\n\tr.Use(AssertJSON)\n\n\t// ...\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", mux))\n}\n\nfunc AssertJSON(next http.Handler) http.Handler {\n\treturn xmate.HandleFunc(func(w http.ResponseWriter, r *http.Request) error {\n\t\tif r.Header.Get(\"Content-Type\") != \"application/json\" {\n\t\t\treturn xmate.WriteText(w, http.StatusBadRequest, \"Unsupported content type\")\n\t\t}\n\n\t\tnext.ServeHTTP(w, r)\n\t\treturn nil\n\t})\n}\n```\n\n### Multiple error handlers\n\nError handling may need to vary in different contexts. That's why you can\nalways instantiate a non-default error handler and use it on your HTTP handlers.\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/utilyre/xmate/v3\"\n)\n\nfunc main() {\n\tmux := http.NewServeMux()\n\n\txmate.SetDefault(handleError) // changes the error handler used by top-level functions\n\tapiEH := xmate.ErrorHandler(handleAPIError) // custom handler for API routes\n\n\tmux.HandleFunc(\"GET /signup\", xmate.HandleFunc(handleSignUpPage))\n\tmux.HandleFunc(\"GET /login\", xmate.HandleFunc(handleLoginPage))\n\n\tmux.Handle(\"GET /api/v1/auth/signup\", apiEH.HandleFunc(handleSignUp))\n\tmux.Handle(\"GET /api/v1/auth/login\", apiEH.HandleFunc(handleLogin))\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", mux))\n}\n\nfunc handleError(w http.ResponseWriter, r *http.Request, err error) {\n\tlog.Printf(\"Failed to %s %s: %v\", r.Method, r.URL.Path, err)\n\t_ = xmate.WriteText(w, http.StatusInternalServerError, \"Internal Server Error\")\n}\n\nfunc handleAPIError(w http.ResponseWriter, r *http.Request, err error) {\n\tlog.Printf(\"Failed to %s %s: %v\", r.Method, r.URL.Path, err)\n\t_ = xmate.WriteJSON(w, http.StatusInternalServerError, map[string]any{\n\t\t\"message\": \"Internal Server Error\",\n\t})\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futilyre%2Fxmate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Futilyre%2Fxmate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futilyre%2Fxmate/lists"}