{"id":21347094,"url":"https://github.com/kybernetwork/kyberswap-error","last_synced_at":"2025-07-12T17:31:31.838Z","repository":{"id":57708214,"uuid":"503686337","full_name":"KyberNetwork/kyberswap-error","owner":"KyberNetwork","description":null,"archived":false,"fork":false,"pushed_at":"2023-08-22T04:36:46.000Z","size":240,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-06T23:09:41.360Z","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":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KyberNetwork.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}},"created_at":"2022-06-15T08:48:54.000Z","updated_at":"2022-11-21T14:32:27.000Z","dependencies_parsed_at":"2024-06-21T20:24:18.783Z","dependency_job_id":"1db41aa5-d02f-4d7f-acf5-1d3b52c34439","html_url":"https://github.com/KyberNetwork/kyberswap-error","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/KyberNetwork/kyberswap-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyberNetwork%2Fkyberswap-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyberNetwork%2Fkyberswap-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyberNetwork%2Fkyberswap-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyberNetwork%2Fkyberswap-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KyberNetwork","download_url":"https://codeload.github.com/KyberNetwork/kyberswap-error/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyberNetwork%2Fkyberswap-error/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265025407,"owners_count":23699740,"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":[],"created_at":"2024-11-22T02:12:38.305Z","updated_at":"2025-07-12T17:31:31.528Z","avatar_url":"https://github.com/KyberNetwork.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KyberSwap Error Lib\n\n## Overview\nThis is the library that defines errors and error handlers for KyberSwap\n\n## Add this lib to your project\n- Step 1: \n```\n$ export GOPRIVATE=github.com/KyberNetwork/kyberswap-error\n```\n- Step 2: Add file `tools/tools.go` with content:\n```\npackage tools\n\nimport (\n\t_ \"github.com/KyberNetwork/kyberswap-error/tools\"\n)\n```\n- Step 3: \n```\n$ go mod tidy\n$ go mod vendor\n```\n\n## Update to latest version\n```\n$ go get -u github.com/KyberNetwork/kyberswap-error\n$ go mod vendor\n```\n\n## How to use\n\n### `InfraError`\nThese errors should be used in the infra layer (repository) of your service\n\n### `DomainError`\nThese errors should be used in the domain layer of your service\n\n### `RestAPIError`\nThese errors should be used in the application interface of your service\n\n### Transforms DomainError to RestAPIError\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/KyberNetwork/kyberswap-error/pkg/errors\"\n\tt \"github.com/KyberNetwork/kyberswap-error/pkg/transformer\"\n)\n\nfunc main() {\n\tdomainErr := errors.NewDomainErrorNotFound(nil)\n\ttransformer := t.RestTransformerInstance()\n\tapiErr := transformer.DomainErrToRestAPIErr(domainErr)\n\tfmt.Println(apiErr.Error())\n}\n```\n- NOTE\n  - Transforming `InfraError` to `DomainError` is done in a similar way\n  - Transform errors flow: `InfraError`(if exists) =\u003e `DomainError` =\u003e `ClientError (RestAPIError, RPCError)`\n\n### Register new DomainError and new RestAPIError\n- There are functions can be used to create customized errors. With `DomainError`, it's `errors.NewDomainError(code string, message string, entities []string, rootCause error)` and it's `errors.NewRestAPIError(httpStatus int, code int, message string, entities []string, rootCause error)` with `RestAPIError`.\n- You can use these above functions to create your customized errors directly. But you should define new constructors for your custom errors, so it can be reused. For example:\n```\npackage main\n\nimport (\n\t\"net/http\"\n\t\n\t\"github.com/KyberNetwork/kyberswap-error/pkg/errors\"\n)\n\nconst (\n\tDomainErrCodeCustomized = \"DOMAIN:CUSTOMIZED\"\n\tDomainErrMsgCustomized  = \"Customized domain error\"\n\n\tClientErrCodeCustomized = 40099\n\tClientErrMsgCustomized  = \"Customized client error\"\n) \n\nfunc NewDomainErrCustomized(rootCause error, entities ...string) *errors.DomainError {\n\treturn errors.NewDomainError(DomainErrCodeCustomized, DomainErrMsgCustomized, entities, rootCause)\n}\n\nfunc NewRestAPIErrCustomized(rootCause error, entities ...string) *errors.RestAPIError {\n\tmessage := errors.AppendEntitiesToErrMsg(ClientErrMsgCustomized, entities)\n\treturn errors.NewRestAPIError(http.StatusBadRequest, ClientErrCodeCustomized, message, entities, rootCause)\n}\n```\n- After defining your custom errors, you have to register the function used to transform your custom `DomainError` to your custom `RestAPIError`. For example:\n```\npackage main\n\nimport (\n\t\"net/http\"\n\t\n\t\"github.com/KyberNetwork/kyberswap-error/pkg/errors\"\n)\n\nconst (\n\tDomainErrCodeCustomized = \"DOMAIN:CUSTOMIZED\"\n\tDomainErrMsgCustomized  = \"Customized domain error\"\n\n\tClientErrCodeCustomized = 40099\n\tClientErrMsgCustomized  = \"Customized client error\"\n) \n\nfunc NewDomainErrCustomized(rootCause error, entities ...string) *errors.DomainError {\n\treturn errors.NewDomainError(DomainErrCodeCustomized, DomainErrMsgCustomized, entities, rootCause)\n}\n\nfunc NewRestAPIErrCustomized(rootCause error, entities ...string) *errors.RestAPIError {\n\tmessage := errors.AppendEntitiesToErrMsg(ClientErrMsgCustomized, entities)\n\treturn errors.NewRestAPIError(http.StatusBadRequest, ClientErrCodeCustomized, message, entities, rootCause)\n}\n\nfunc main() {\n\ttransformer := transformers.RestTransformerInstance()\n\ttransformer.RegisterTransformFunc(DomainErrCodeCustomized, NewRestAPIErrCustomized)\n}\n```\n- NOTE: \n  - Each error should have a unique error code. Otherwise, it can lead to unexpected results when transforming. So You should not define your custom error code as one of the predefined error codes in \"kyberswap-error\". The list of those predefined error codes can be found at https://www.notion.so/kybernetwork/API-Standards-proposal-draft-e8d8bf2dc5f647e89d2bf1b5f0ef8bdf\n  - The `ClientErrorCode` should contain information about HTTP Status. It makes the error code more meaningful\n  - Registering new `InfraError` is done in a similar way\n\n### For gin framework\n- This lib provides the function `ValidationErrToRestAPIErr(err error)` which can be used when binding and validating the request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkybernetwork%2Fkyberswap-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkybernetwork%2Fkyberswap-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkybernetwork%2Fkyberswap-error/lists"}