{"id":24211490,"url":"https://github.com/go-api-libs/api","last_synced_at":"2025-08-20T04:26:00.920Z","repository":{"id":264411802,"uuid":"892123371","full_name":"go-api-libs/api","owner":"go-api-libs","description":"Provides a set of common error definitions used across various API interactions (including all libraries in this GitHub organization). Consistent error handling to facilitate easier debugging, maintenance, and integration with different services.","archived":false,"fork":false,"pushed_at":"2024-12-22T14:58:13.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-03T16:22:01.024Z","etag":null,"topics":["api","api-error","golang"],"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/go-api-libs.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":"2024-11-21T14:50:47.000Z","updated_at":"2024-12-23T11:14:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"131e01ce-98da-4187-a2b2-3c0c04264bbb","html_url":"https://github.com/go-api-libs/api","commit_stats":null,"previous_names":["go-api-libs/api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/go-api-libs/api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-api-libs%2Fapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-api-libs%2Fapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-api-libs%2Fapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-api-libs%2Fapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-api-libs","download_url":"https://codeload.github.com/go-api-libs/api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-api-libs%2Fapi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271263169,"owners_count":24728982,"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-08-20T02:00:09.606Z","response_time":69,"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":["api","api-error","golang"],"created_at":"2025-01-14T02:35:25.978Z","updated_at":"2025-08-20T04:26:00.874Z","avatar_url":"https://github.com/go-api-libs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Standardized API Error Handling for Go\n[![Go Reference](https://pkg.go.dev/badge/github.com/go-api-libs/api.svg)](https://pkg.go.dev/github.com/go-api-libs/api)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-api-libs/api)](https://goreportcard.com/report/github.com/go-api-libs/api)\n![Code Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)\n\nThis library provides a set of common error definitions used across various API interactions. By using this library, you can achieve consistent error handling in your applications, making it easier to debug, maintain, and integrate with different services.\n\nIt is utilized by all API libraries within the [go-api-libs](https://github.com/go-api-libs) organization.\n\n## Features\n\n- **Wrap Errors**: Easily wrap underlying errors with API-specific errors to provide more context.\n- **Error Identification**: Check and identify the nature of errors using Go's `errors.Is` and `errors.As` functions.\n- **Custom Errors**: Define and use custom errors specific to your API needs.\n- **Status Code Handling**: Handle and identify errors based on HTTP status codes.\n- **Content Type Handling**: Handle errors based on content type mismatches.\n\n## Installation\n\nTo install the library, run:\n```sh\ngo get github.com/go-api-libs/api\n```\n\n## Usage\n\nHere is a basic example of how to use the api library for error handling:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/go-api-libs/api\"\n\t\"github.com/go-api-libs/toggl/pkg/toggl\"\n)\n\nfunc main() {\n\tc, err := toggl.NewClientWithAPIToken(os.Getenv(\"TOGGL_TOKEN\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tctx := context.Background()\n\tnow := time.Now()\n\tentries, err := c.ListTimeEntriesInRange(ctx, now.Add(-24*time.Hour), now)\n\tif err != nil {\n\t\tdecErr := \u0026api.DecodingError{}\n\t\tif errors.As(err, \u0026decErr) {\n\t\t\tfmt.Println(\"Could not decode response:\", decErr.Err)\n\t\t}\n\n\t\tapiErr := \u0026api.Error{}\n\t\tif errors.As(err, \u0026apiErr) {\n\t\t\tfmt.Println(\"Response Status Code:\", apiErr.StatusCode())\n\t\t\tfmt.Println(\"Response Content Type:\", apiErr.ContentType())\n\t\t\tif apiErr.IsCustom {\n\t\t\t\tfmt.Println(\"API sent back the error:\", apiErr.Err)\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\n\t\tif errors.Is(err, api.ErrStatusCode) {\n\t\t\tfmt.Println(\"The response status code indicates an error (but is properly documented).\")\n\t\t\t// NOTE: Some APIs have custom error responses that don't contain api.ErrStatusCode.\n\t\t\t// If the status code indicates an error and the API returns a JSON,\n\t\t\t// we unmarshal it and return it as an object that fulfils the error interface.\n\t\t\t// If this happens, `IsCustom` above is set to true.\n\t\t} else if errors.Is(err, api.ErrUnknownStatusCode) {\n\t\t\tfmt.Println(\"The returned status code is not documented in the API specification.\")\n\t\t}\n\n\t\tif errors.Is(err, api.ErrUnknownContentType) {\n\t\t\tfmt.Println(\"The response content type is not documented in the API specification.\")\n\t\t}\n\n\t\tpanic(err)\n\t}\n\n\t// Use entries slice\n}\n```\n\n## Contributing\n\nIf you have any contributions to make, please submit a pull request or open an issue on the [GitHub repository](https://github.com/go-api-libs/api).\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-api-libs%2Fapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-api-libs%2Fapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-api-libs%2Fapi/lists"}