An open API service indexing awesome lists of open source software.

https://github.com/ap-dev-at-home/results

C# - Fluent/Chainable Error, Result and Exception Handling
https://github.com/ap-dev-at-home/results

chainable-methods csharp dotnet error-handling exception-handling fluent result result-handling

Last synced: 6 months ago
JSON representation

C# - Fluent/Chainable Error, Result and Exception Handling

Awesome Lists containing this project

README

          

# Results - Result and Exception Handling

**Current Status**: In Development/Trial Phase - final behaviour not determined yet

**Goal**: Simplify result and exception handling using chainable method calls.

## Table of Contents

- [Result - API](#result---api)
- [Modifications - API](#modifications---api)
- [Execute - API](#execute---api)
- [ResultsJson - API](#resultsjson---exception-safe-json)

### Result - API
| Method | Parameter | Return |
|----------|----------|----------|
| Result.Ok()
*- static method*| | A successful Result |
| Result.Ok(T `value`)
*- static method*| `value` - the value contained by the result | A successful Result of value type T |
| Result.Fail(string `message`)
*- static method*| `message` - the error message | A failed Result |
| Result.Fail(Error `error`)
*- static method*| `error` - the error | A failed Result |
| Result.Fail()
*- static method*| | A failed Result |
| Result.NotNull(T? `value`, string `message`)
*- static method* | `value`- a result value

`message` - the error message, set if `value` is null | If `value` is not null - A successful Result containing the value

If `value` is null - A failed Result containing error `message` |
| Result.Handover(params object?[] `value`)
*- static method* | `value` - array of obejcts to pass over to the next **Then** call.

Results will be unwrapped to their containing value before passed over to the next **Then** call

Any other object will simply be passed over | A Handover ResultCollection |
| Result.`Value` | Property of genric type TValue | The results internal value |
| Result.`Success` | Property of type Bool | The result status |
| Result.`Failed` | Property of type Bool | The result inverted status |
| Result.`Error`| Property of type Error | The error |
| Result.`Logs`| Property of type List\ | The logs |

### Modifications - API
| Method | Parameter | Return |
|----------|----------|----------|
|Result\.WhenNull(TValue `value`) | `value` - value to set if the current result value is null

No effect on a failed result. | The Result (itself) |
|Result\.Assert(`Func`, string `message`)| `Func` - function receiving the value, returning a bool expression

If the expression evaluates to false the result will be set to fail - `message` will be set

No effect on a failed result. | The Result (itself) |
|Result\.Recover(TValue `value`) | `value` - value to set to a new Result object if the current result is in a failed status

Only affects a failed result. | The Result (itself) - if it was in a success status

A new success Result containing `value` - if it was failed status |

### Execute - API
| Method | Parameter | Return |
|----------|----------|----------|
| Result.Do(`Func>`)
*- static method* | `Func` - function to be called | A `Result` of type `TResult` returned from `Func` |
| Result.DoAsync(`Func>`)
*- static method* | `Func` - function to be called | A `Task` of type `TResult` |
| Result.DoInterlocked(`Func>`, `object`, `wait`)
*- static method* | `Func` - function to be called

`object` - object to acquire the lock on

`wait` - optional (default true) - if false the method will immediately return if the lock can not be aquired| A `Result` of type `TResult` returned from `Func`

A failed `Result` with `Error` of type `InterlockError`, if the lock can not be acquired. |
| Result.Try(`Func>`, `Action?`)
*- static method* | `Func` - function to be called
Surrounded by a try catch

`Action` - Optional - called on exception
The Exception is passed| A `Result` of type `TResult` returned from `Func`

If an exception occurs - A failed `Result` containing the exception |
| Result.Try(`Action`, `Action?`)
*- static method* | `Action` - action to be called
Surrounded by a try catch

`Action` - Optional - called on exception
The Exception is passed| A success `Result`

If an exception occurs - A failed `Result` containing the exception |
| Result.TryAsync(`Func>`, `Action?`)
*- static method* | `Func` - function to be called
Surrounded by a try catch

`Action` - Optional - called on exception
The Exception is passed | A `Task` of type `TResult`

If an exception occurs - A failed `Result` containing the exception |
| Result.TryInterlocked(`Func>`, `object`, `wait`, `Action?`)
*- static method* | `Func` - function to be called

`object` - object to acquire the lock on

`wait` - optional (default true) - if false the method will immediately return if the lock can not be aquired

`Action` - Optional - called on exception
The Exception is passed | A `Result` of type `TResult` returned from `Func`

A failed `Result` with `Error` of type `InterlockError`, if the lock can not be acquired. |
| Result\.Then(`Func`)| `Func` - function to be called
Up to 4 handover parameters

If a failed Result is passed from the previous call - **Then** will not be called

If a Handover object is passed from the previous call containing any failed result - **Then** will not be called | The Result returned from Func |
| Result.FailFast(params `Func[]`)
*- static method* | `Func[]` - functions to be called

Every call must return a Result.

A failing call will stop further calls.| Success ResultCollection - if no call failed

Failed ResultCollection - if any call failed |
| Result.FailSafe(params `Func[]`)
*- static method* | `Func[]` - functions to be called

Every call must return a Result.

A failing call will not stop further calls.| Success ResultCollection - if no call failed

Failed ResultCollection - if any call failed |
| More to come - Development in Progress...|||

### ResultsJson - Exception Safe Json
| Method Signature | Description |
|------------------|-------------|
| Json.From\(string jsonString) | Deserializes a JSON string into a Result object. |
| Json.Load\(string path) | Deserializes a JSON file into a Result object. |
| Json.Save\(string path, T obj) | Serializes an object to a JSON file. |
| Json.From\(Stream stream) | Deserializes a JSON stream into a Result object. |
| Json.From\(ReadOnlySpan utf8Json) | Deserializes a JSON byte span into a Result object. |