{"id":22888375,"url":"https://github.com/gravity00/simpleexceptionhandling","last_synced_at":"2025-05-07T11:45:30.395Z","repository":{"id":75304771,"uuid":"58537582","full_name":"gravity00/SimpleExceptionHandling","owner":"gravity00","description":"Library that helps developers to handle exceptions outside catch blocks. Typical usages are global exception handlers.","archived":false,"fork":false,"pushed_at":"2017-06-21T09:37:12.000Z","size":162,"stargazers_count":14,"open_issues_count":3,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-25T06:39:13.903Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/gravity00.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,"zenodo":null}},"created_at":"2016-05-11T11:02:53.000Z","updated_at":"2023-11-08T17:48:39.000Z","dependencies_parsed_at":"2023-06-03T03:30:26.815Z","dependency_job_id":null,"html_url":"https://github.com/gravity00/SimpleExceptionHandling","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gravity00%2FSimpleExceptionHandling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gravity00%2FSimpleExceptionHandling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gravity00%2FSimpleExceptionHandling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gravity00%2FSimpleExceptionHandling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gravity00","download_url":"https://codeload.github.com/gravity00/SimpleExceptionHandling/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252873936,"owners_count":21817708,"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-12-13T20:47:55.004Z","updated_at":"2025-05-07T11:45:30.388Z","avatar_url":"https://github.com/gravity00.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Exception Handling\nLibrary that helps developers to handle exceptions outside catch blocks.\nTypical usages are global exception handlers.\n\n## Installation \nThis library can be installed via [NuGet](https://www.nuget.org/packages/SimpleExceptionHandling/) package. Just run the following command:\n\n```powershell\nInstall-Package SimpleExceptionHandling\n```\n\n## Compatibility\n\nThis library is compatible with the folowing frameworks:\n\n* MonoAndroid 1.0;\n* MonoTouch 1.0;\n* .NET Framework 2.0\n* .NET Framework 3.5\n* .NET Framework 4.0\n* .NET Framework 4.5\n* .NET Core 5.0;\n* .NET Standard 1.0;\n* Portable Class Library (.NETFramework 4.0, Silverlight 5.0, Windows 8.0, WindowsPhone 8.0, WindowsPhoneApp 8.1);\n* Portable Class Library (.NETFramework 4.5, Windows 8.0, WindowsPhoneApp 8.1);\n* WindowsPhoneApp 8.1;\n* Xamarin.iOS 1.0;\n* Xamarin.TVOS 1.0;\n\n## Tipical usage\n\nThis is a usage example for Web API 2:\n\n```csharp\nusing SimpleExceptionHandling;\n\npublic class GlobalExceptionHandler : ExceptionHandler\n{\n\tprivate static readonly IHandlingConfiguration\u003cExceptionHandlerContext, ResponseMessageResult\u003e\n\t\tHandlingConfiguration =\n\t\t\tHandling.Prepare\u003cExceptionHandlerContext, ResponseMessageResult\u003e()\n\t\t\t\t.On\u003cValidationException\u003e((ex, i) =\u003e\n\t\t\t\t{\n\t\t\t\t\treturn Handling.Handled(\n\t\t\t\t\t\ti.Parameter.Request.CreateBadRequestResult(\n\t\t\t\t\t\t\tex.ValidationErrors.Select(\n\t\t\t\t\t\t\t\te =\u003e new KeyValuePair\u003cstring, string[]\u003e(\n\t\t\t\t\t\t\t\t\te.Key, e.Messages.ToArray()))));\n\t\t\t\t})\n\t\t\t\t.On\u003cBusinessException\u003e((ex, i) =\u003e\n\t\t\t\t{\n\t\t\t\t\treturn Handling.Handled(\n\t\t\t\t\t\ti.Parameter.Request.CreateConflictResult(ex.Message));\n\t\t\t\t})\n\t\t\t\t.On\u003cGenericException\u003e((ex, i) =\u003e\n\t\t\t\t{\n\t\t\t\t\treturn Handling.Handled(\n\t\t\t\t\t\ti.Parameter.Request.CreateInternalServerErrorResult(ex.Message));\n\t\t\t\t})\n\t\t\t\t.On\u003cExternalServiceException\u003e((ex, i) =\u003e\n\t\t\t\t{\n\t\t\t\t\treturn Handling.Handled(\n\t\t\t\t\t\ti.Parameter.Request.CreateBadGatewayResult());\n\t\t\t\t})\n\t\t\t\t.On\u003cTimeoutException\u003e((ex, i) =\u003e\n\t\t\t\t{\n\t\t\t\t\treturn Handling.Handled(\n\t\t\t\t\t\ti.Parameter.Request.CreateGatewayTimeoutResult());\n\t\t\t\t})\n\t\t\t\t.On\u003cNotImplementedException\u003e((ex, i) =\u003e\n\t\t\t\t{\n\t\t\t\t\treturn Handling.Handled(\n\t\t\t\t\t\ti.Parameter.Request.CreateNotImplementedResult());\n\t\t\t\t});\n\n\tpublic override bool ShouldHandle(ExceptionHandlerContext context)\n\t{\n\t\treturn true;\n\t}\n\n\tpublic override void Handle(ExceptionHandlerContext context)\n\t{\n\t\tvar result = HandlingConfiguration.Catch(context.Exception, context, false);\n\t\tif (result.Handled)\n\t\t{\n\t\t\tcontext.Result = result.Result;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbase.Handle(context);\n\t\t}\n\t}\n}\n```\n\n### Basic usage\n\nHere is a simple example of handling exceptions by their types:\n\n```csharp\npublic static void BasicExceptionHandling(string param01)\n{\n\tstring handlerName = null;\n\tvar configuration =\n\t\tHandling.Prepare()\n\t\t\t.On\u003cArgumentNullException\u003e(ex =\u003e\n\t\t\t{\n\t\t\t\thandlerName = $\"ArgumentNullException[ParamName={ex.ParamName}]\";\n\t\t\t})\n\t\t\t.On\u003cArgumentException\u003e(ex =\u003e\n\t\t\t{\n\t\t\t\thandlerName = $\"ArgumentException[ParamName={ex.ParamName}]\";\n\t\t\t});\n\n\tvar result = configuration.Catch(new ArgumentNullException(nameof(param01)));\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{handlerName}'\");\n\t//  Handler[Handled=True] -\u003e 'ArgumentNullException[ParamName=param01]'\n\n\thandlerName = null;\n\tresult = configuration.Catch(new ArgumentOutOfRangeException(nameof(param01)));\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{handlerName}'\");\n\t//  Handler[Handled=True] -\u003e 'ArgumentException[ParamName=param01]'\n\n\thandlerName = null;\n\tresult = configuration.Catch(new Exception(), throwIfNotHandled: false);\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{handlerName}'\");\n\t//  Handler[Handled=False] -\u003e ''\n}\n```\n\n### Input Parameter and Result\n\nIn this example, a parameter is passed as an argument of the `Catch` method, and a result is returned by handlers. One of the handlers will be invoked but won't be considered to handle the exception:\n\n```csharp\npublic static void InputAndResultExceptionHandling(string param01)\n{\n\tvar configuration =\n\t\tHandling.Prepare\u003cint, string\u003e()\n\t\t\t.On\u003cArgumentNullException\u003e((ex, i) =\u003e\n\t\t\t{\n\t\t\t\t//  this handler will be invoked, but says to be ignored\n\n\t\t\t\t//return new HandlingResult\u003cstring\u003e(false);\n\t\t\t\t//return Handling.Ignore\u003cstring\u003e();\n\t\t\t\treturn false;\n\t\t\t})\n\t\t\t.On\u003cArgumentException\u003e((ex, i) =\u003e\n\t\t\t{\n\t\t\t\tvar ret = $\"ArgumentException[ParamName={ex.ParamName}, InputParameter={i.Parameter}]\";\n\t\t\t\t\n\t\t\t\t//return new HandlingResult\u003cstring\u003e(true, ret);\n\t\t\t\treturn Handling.Handled(ret);\n\t\t\t})\n\t\t\t.On\u003cException\u003e((ex, i) =\u003e\n\t\t\t{\n\t\t\t\tvar ret = $\"Exception[InputParameter={i.Parameter}]\";\n\n\t\t\t\t//return new HandlingResult\u003cstring\u003e(true, ret);\n\t\t\t\treturn Handling.Handled(ret);\n\t\t\t});\n\n\tvar result = \n\t\tconfiguration.Catch(\n\t\t\tnew ArgumentNullException(nameof(param01)), 987987);\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{result.Result}'\");\n\t//  Handler[Handled=True] -\u003e 'ArgumentException[ParamName=param01, InputParameter=987987]'\n\n\tresult = \n\t\tconfiguration.Catch(\n\t\t\tnew ArgumentOutOfRangeException(nameof(param01)), 123123);\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{result.Result}'\");\n\t//  Handler[Handled=True] -\u003e 'ArgumentException[ParamName=param01, InputParameter=123123]'\n\n\tresult =\n\t\tconfiguration.Catch(new Exception(), 54321);\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{result.Result}'\");\n\t//  Handler[Handled=True] -\u003e 'Exception[InputParameter=54321]'\n}\n```\n\n## Conditions\n\nIn this example, some handlers are conditionally invoked, even if the exception type match:\n\n```csharp\npublic static void ConditionalExceptionHandling(string param01, string param02, string param03)\n{\n\tstring handlerName = null;\n\tvar configuration =\n\t\tHandling.Prepare()\n\t\t\t.On\u003cArgumentNullException\u003e(ex =\u003e\n\t\t\t{\n\t\t\t\thandlerName = \"ArgumentNullException[ParamName=param01]\";\n\t\t\t}, (ex, i) =\u003e ex.ParamName == nameof(param01))\n\t\t\t.On\u003cArgumentNullException\u003e(ex =\u003e\n\t\t\t{\n\t\t\t\thandlerName = \"ArgumentNullException[ParamName=param02]\";\n\t\t\t}, (ex, i) =\u003e ex.ParamName == nameof(param02))\n\t\t\t.On\u003cArgumentNullException\u003e(ex =\u003e\n\t\t\t{\n\t\t\t\thandlerName = $\"ArgumentNullException[ParamName={ex.ParamName}]\";\n\t\t\t});\n\n\tvar result = configuration.Catch(new ArgumentNullException(nameof(param01)));\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{handlerName}'\");\n\t//  Handler[Handled=True] -\u003e 'ArgumentException[ParamName=param01]'\n\n\tresult = configuration.Catch(new ArgumentNullException(nameof(param03)));\n\tConsole.WriteLine($\"Handler[Handled={result.Handled}] -\u003e '{handlerName}'\");\n\t//  Handler[Handled=True] -\u003e 'ArgumentException[ParamName=param03]'\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgravity00%2Fsimpleexceptionhandling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgravity00%2Fsimpleexceptionhandling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgravity00%2Fsimpleexceptionhandling/lists"}