{"id":18327213,"url":"https://github.com/dalikewara/typego","last_synced_at":"2025-04-09T16:30:19.791Z","repository":{"id":219291032,"uuid":"695478626","full_name":"dalikewara/typego","owner":"dalikewara","description":"typego provides custom type that can be used to construct information (such as success data, error data, etc)","archived":false,"fork":false,"pushed_at":"2024-09-06T04:04:16.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-15T10:13:34.545Z","etag":null,"topics":["custom","data","golang","helper","type","typego"],"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/dalikewara.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"dalikewara","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2023-09-23T10:04:43.000Z","updated_at":"2024-09-06T04:04:01.000Z","dependencies_parsed_at":"2024-06-26T07:05:39.365Z","dependency_job_id":"2753457c-eb89-4675-b16f-2eea2b85586e","html_url":"https://github.com/dalikewara/typego","commit_stats":null,"previous_names":["dalikewara/typego"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalikewara%2Ftypego","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalikewara%2Ftypego/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalikewara%2Ftypego/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalikewara%2Ftypego/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dalikewara","download_url":"https://codeload.github.com/dalikewara/typego/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248067612,"owners_count":21042325,"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":["custom","data","golang","helper","type","typego"],"created_at":"2024-11-05T19:09:58.059Z","updated_at":"2025-04-09T16:30:19.757Z","avatar_url":"https://github.com/dalikewara.png","language":"Go","funding_links":["https://github.com/sponsors/dalikewara"],"categories":[],"sub_categories":[],"readme":"# typego\n\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/github.com/dalikewara/typego)\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/dalikewara/typego)\n![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/dalikewara/typego)\n![GitHub license](https://img.shields.io/github/license/dalikewara/typego)\n\n**typego** provides custom type that can be used to construct information (such as success data, error data, etc).\n\n## Getting started\n\n### Installation\n\nYou can use the `go get` method:\n\n```bash\ngo get github.com/dalikewara/typego\n```\n\n## Usage\n\n### Error\n\n`typego.Error` compatible with `error` interface, so you can use it as an `error` handler. `typego.Error`\nhas several methods that can be used to construct error information:\n\n```go\ntype Error interface {\n    ChangeCode(code string) Error\n    ChangeMessage(message string) Error\n    AddInfo(info ...any) Error\n    AddDebug(debug ...any) Error\n    SetProcessID(processID string) Error\n    SetProcessName(processName string) Error\n    SetHttpStatus(httpStatus int) Error\n    SetRPCStatus(rpcStatus int) Error\n    GetProcessID() string\n    GetProcessName() string\n    GetCode() string\n    GetMessage() string\n    GetInfo() []string\n    GetDebug() []string\n    GetHttpStatus() int\n    GetRPCStatus() int\n    Log() Error\n    Error() string\n}\n```\n\nand it will generate the error information based on this structure:\n\n```go\ntype errorModel struct {\n    Level       string   `json:\"level\"`\n    ProcessID   string   `json:\"process_id,omitempty\"`\n    ProcessName string   `json:\"process_name,omitempty\"`\n    Code        string   `json:\"code\"`\n    Message     string   `json:\"message\"`\n    Info        []string `json:\"info\"`\n    HttpStatus  int      `json:\"http_status,omitempty\"`\n    RPCStatus   int      `json:\"rpc_status,omitempty\"`\n    Debug       []string `json:\"debug,omitempty\"`\n}\n```\n\nFor example:\n\n```go\nfunc main() {\n    if err := myFunc(); err != nil {\n        fmt.Println(err)\n\t\t\n        // output\n        // {\"level\":\"error\",\"code\":\"01\",\"message\":\"general error\",\"info\":null}\n    }   \n}\n\nfunc myFunc() error {\n    return typego.NewError(\"01\", \"general error\")\n}\n```\n\n```go\ntypego.NewError(\"01\", \"general error\").SetHttpStatus(500).AddInfo(\"raw error 1\", \"raw error 2\").AddInfo(\"raw error 3\")\n\n// output\n// {\"level\":\"error\",\"code\":\"01\",\"message\":\"general error\",\"info\":[\"raw error 1\",\"raw error 2\",\"raw error 3\"],\"http_status\":500}\n```\n\nYou can log the error information by using `Log()` method:\n\n```go\ntypego.NewError(\"01\", \"general error\").Log()\n\n// output\n// {\"level\":\"error\",\"code\":\"01\",\"message\":\"general error\",\"info\":null}\n```\n\nYou can also generate new `typego.Error` from an `error`:\n\n```go\nerr := errors.New(\"{\\\"code\\\":\\\"01\\\",\\\"message\\\":\\\"general error\\\",\\\"http_status\\\":500,\\\"info\\\":[\\\"raw info 1\\\",\\\"raw info 2\\\"],\\\"rpc_status\\\":13}\")\ntypegoError := typego.NewErrorFromError(err)\n\nfmt.Println(typegoError.GetCode()) // 01\nfmt.Println(typegoError.GetMessage()) // general error\nfmt.Println(typegoError.GetInfo()[1]) // raw info 2\n```\n\n\u003e The `error.Error()` must have the same string format as `typego.Error.Error()`, otherwise, `typego.Error` will return incorrect value\n\n#### Custom Error Log\n\nYou can overwrite the default error log handler by using `typego.SetCustomErrorLog(handler ErrorLogHandler)` function:\n\n\u003e The default error log handler is just a simple task to print the information to the std out\n\n```go\nerrGeneral := typego.NewError(\"01\", \"general error\")\n\nerrGeneral.Log()\n\n// output\n// {\"level\":\"error\",\"code\":\"01\",\"message\":\"general error\",\"info\":null}\n\ntypego.SetCustomErrorLog(func(err typego.Error) {\n    fmt.Println(fmt.Sprintf(\"hello i am a custom log! -\u003e %+v\", err))\n\t\n    // or do something special here...\n    // for example: send the log info to the Slack Channel, Kafka, etc\n})\n\nerrGeneral.Log()\n\n// output\n// hello i am a custom log! -\u003e {\"level\":\"error\",\"code\":\"01\",\"message\":\"general error\",\"info\":null}\n```\n\nSo, you can change the behavior of the logging as you want.\n\n## Release\n\n### Changelog\n\nRead at [CHANGELOG.md](https://github.com/dalikewara/typego/blob/master/CHANGELOG.md)\n\n### Credits\n\nCopyright \u0026copy; 2023 [Dali Kewara](https://www.dalikewara.com)\n\n### License\n\n[MIT License](https://github.com/dalikewara/typego/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdalikewara%2Ftypego","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdalikewara%2Ftypego","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdalikewara%2Ftypego/lists"}