{"id":18687699,"url":"https://github.com/marcos-venicius/csharperrorhandlingpackage","last_synced_at":"2025-10-17T22:31:34.797Z","repository":{"id":64910191,"uuid":"579357720","full_name":"marcos-venicius/CSharpErrorHandlingPackage","owner":"marcos-venicius","description":"A CSharp ErrorHandling lib","archived":false,"fork":false,"pushed_at":"2023-03-01T12:40:01.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-28T11:11:15.475Z","etag":null,"topics":["csharp","error-handling","nuget","package"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/DevOne.Utils.ErrorHandling","language":"C#","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/marcos-venicius.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":"2022-12-17T12:31:06.000Z","updated_at":"2023-03-01T12:15:19.000Z","dependencies_parsed_at":"2024-11-07T10:36:35.231Z","dependency_job_id":null,"html_url":"https://github.com/marcos-venicius/CSharpErrorHandlingPackage","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":"0.23529411764705888","last_synced_commit":"53676da3a98470e8ea3e07cd58df2c027590d55f"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos-venicius%2FCSharpErrorHandlingPackage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos-venicius%2FCSharpErrorHandlingPackage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos-venicius%2FCSharpErrorHandlingPackage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos-venicius%2FCSharpErrorHandlingPackage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcos-venicius","download_url":"https://codeload.github.com/marcos-venicius/CSharpErrorHandlingPackage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239545937,"owners_count":19656873,"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":["csharp","error-handling","nuget","package"],"created_at":"2024-11-07T10:33:50.811Z","updated_at":"2025-10-17T22:31:34.735Z","avatar_url":"https://github.com/marcos-venicius.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ErrorHandling\r\n\r\n\r\n[![Tests with .NET 7 and 6](https://github.com/marcos-venicius/CSharpErrorHandlingPackage/actions/workflows/lib-tests.yml/badge.svg)](https://github.com/marcos-venicius/CSharpErrorHandlingPackage/actions/workflows/lib-tests.yml)\r\n---------------\r\n\r\n## Installation\r\n\r\n```sh\r\ndotnet add package DevOne.Utils.ErrorHandling --version 2.0.0\r\n```\r\n\r\n### Usage\r\n\r\n- ErrorHandler with value as string\r\n\r\n```cs\r\n{\r\n    string name = \"marcos test\";\r\n\r\n    using (var eh = new ErrorHandler\u003cstring\u003e())\r\n    {\r\n        switch (name.Length)\r\n        {\r\n            case \u003c 3: // with indexer\r\n                eh[nameof(name)] = \"name length must be greater than 2\";\r\n                break;\r\n            case \u003e 25: // with method\r\n                eh.Add(nameof(name), \"name length must be less than 26\");\r\n                break;\r\n            default:\r\n                break;\r\n        }\r\n\r\n        var regex = new Regex(@\"^[a-z0-9]+$\");\r\n\r\n        if (!regex.IsMatch(name))\r\n        {\r\n            eh.Add(nameof(name), \"name format is invalid\");\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nwhen the program get out of the `using` scope, if exists some error an exception of type\r\n`new ErrorHandlerException\u003cError\u003cstring\u003e\u003e` will throws.\r\n\r\nif you want to verify errors in a specific part of your code, you can avoid `using` and use like that:\r\n\r\n```cs\r\n{\r\n    string name = \"marcos test\";\r\n\r\n    var eh = new ErrorHandler\u003cstring\u003e();\r\n\r\n    switch (name.Length)\r\n    {\r\n        case \u003c 3:\r\n            eh[nameof(name)] = \"name length must be greater than 2\";\r\n            break;\r\n        case \u003e 25:\r\n            eh[nameof(name)] = \"name length must be less than 26\";\r\n            break;\r\n        default:\r\n            break;\r\n    }\r\n\r\n    var regex = new Regex(@\"^[a-z0-9]+$\");\r\n\r\n    if (!regex.IsMatch(name))\r\n    {\r\n        eh[nameof(name)] = \"name format is invalid\";\r\n    }\r\n\r\n    ...\r\n\r\n    eh.Dispose(); // verify the errors\r\n}\r\n```\r\n\r\nif you want a diferent error value type use can do this like that:\r\n\r\n* create a custom error value\r\n```cs\r\n{\r\n    record CustomErrorValue(string Message, string Id, string Location);\r\n}\r\n```\r\n\r\n* use this error value on error handler instance\r\n```cs\r\n{\r\n    using (var eh = new ErrorHandler\u003cCustomErrorValue\u003e()) {}\r\n}\r\n```\r\n\r\n* and now you need pass this type as the value\r\n```cs\r\n{\r\n    eh[\"my key\"] = new CustomErrorValue(\"message\", \"id\", \"location\");\r\n}\r\n```\r\n\r\nyou can catch any exceptions from error handler like that:\r\n\r\n```cs\r\ntry {\r\n    ...\r\n}\r\ncatch (ErrorHandlerBaseException exception)\r\n{\r\n    Console.WriteLine(exception.Message);\r\n\r\n    foreach (var error in exception.Errors)\r\n    {\r\n        Console.WriteLine(error);\r\n    }\r\n}\r\n```\r\n\r\nthis way will catch any type of exception that the ErrorHandler throws\r\nif you want to catch a specific exception type you can:\r\n\r\n```cs\r\ntry {\r\n    ...\r\n}\r\ncatch (ErrorHandlerException\u003cError\u003cstring\u003e\u003e exception)\r\n{\r\n    Console.WriteLine(exception.Message);\r\n\r\n    foreach (var error in exception.Errors)\r\n    {\r\n        Console.WriteLine($\"{error.Key}: {error.Value}\"); // you have intellisense\r\n    }\r\n}\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcos-venicius%2Fcsharperrorhandlingpackage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcos-venicius%2Fcsharperrorhandlingpackage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcos-venicius%2Fcsharperrorhandlingpackage/lists"}