Skip to content

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

License

Notifications You must be signed in to change notification settings

ap-dev-at-home/Results

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

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<LogEntry> The logs

Modifications - API

Method Parameter Return
Result<TValue>.WhenNull(TValue value) value - value to set if the current result value is null

No effect on a failed result.
The Result (itself)
Result<TValue>.Assert(Func<TValue, bool>, 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<TValue>.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<Result<TResult>>)
- static method
Func - function to be called A Result of type TResult returned from Func
Result.DoAsync(Func<Result<TResult>>)
- static method
Func - function to be called A Task<Result> of type TResult
Result.DoInterlocked(Func<Result<TResult>>, 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<Result<TResult>>, Action<Exception>?)
- 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.TryAsync(Func<Result<TResult>>, Action<Exception>?)
- static method
Func - function to be called
Surrounded by a try catch

Action - Optional - called on exception
The Exception is passed
A Task<Result> of type TResult

If an exception occurs - A failed Result containing the exception
Result.Try(Action, Action<Exception>?)
- 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<TResult>.Then(Func<T1..T4, TResult>) 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<Result>[])
- 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<Result>[])
- 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<T>(string jsonString) Deserializes a JSON string into a Result object.
Json.Load<T>(string path) Deserializes a JSON file into a Result object.
Json.Save<T>(string path, T obj) Serializes an object to a JSON file.
Json.From<T>(Stream stream) Deserializes a JSON stream into a Result object.
Json.From<T>(ReadOnlySpan utf8Json) Deserializes a JSON byte span into a Result object.

Releases

No releases published

Packages

No packages published

Languages