From 9035b12c2012c90d59ba79c18e7f3d9fc8237060 Mon Sep 17 00:00:00 2001 From: Matthew Watt Date: Fri, 21 Feb 2025 09:09:40 -0600 Subject: [PATCH 1/2] Rename returnError to error; add documentation --- gitbook/SUMMARY.md | 15 +++---- gitbook/asyncResult/error.md | 22 ++++++++++ gitbook/asyncValidation/error.md | 22 ++++++++++ gitbook/cancellableTaskValidation/error.md | 22 ++++++++++ gitbook/jobResult/error.md | 22 ++++++++++ gitbook/taskResult/error.md | 22 ++++++++++ gitbook/validation/error.md | 22 ++++++++++ .../JobResult.fs | 4 +- src/FsToolkit.ErrorHandling.JobResult/List.fs | 6 +-- src/FsToolkit.ErrorHandling/Array.fs | 10 ++--- src/FsToolkit.ErrorHandling/AsyncResult.fs | 6 +-- .../AsyncValidation.fs | 4 -- src/FsToolkit.ErrorHandling/List.fs | 7 ++-- src/FsToolkit.ErrorHandling/TaskResult.fs | 4 +- src/FsToolkit.ErrorHandling/Validation.fs | 3 -- .../JobResult.fs | 40 ------------------- .../TaskResult.fs | 2 +- .../AsyncResult.fs | 2 +- 18 files changed, 153 insertions(+), 82 deletions(-) create mode 100644 gitbook/asyncResult/error.md create mode 100644 gitbook/asyncValidation/error.md create mode 100644 gitbook/cancellableTaskValidation/error.md create mode 100644 gitbook/jobResult/error.md create mode 100644 gitbook/taskResult/error.md create mode 100644 gitbook/validation/error.md diff --git a/gitbook/SUMMARY.md b/gitbook/SUMMARY.md index e8f4084d..e636fd0e 100644 --- a/gitbook/SUMMARY.md +++ b/gitbook/SUMMARY.md @@ -87,7 +87,7 @@ * [bind](asyncResult/bind.md) * [check](asyncResult/check.md) * [Computation Expression](asyncResult/ce.md) - * [error](pr.md) + * [error](asyncResult/error.md) * [foldResult](asyncResult/foldResult.md) * [ignore](asyncResult/ignore.md) * [map](asyncResult/map.md) @@ -176,7 +176,7 @@ * [mapError](taskResult/mapError.md) * [Operators](taskResult/operators.md) * [Other Functions](taskResult/others.md) - * [returnError](pr.md) + * [error](taskResult/error.md) * [zip](pr.md) * [zipError](pr.md) * Lists @@ -202,14 +202,13 @@ * [Validation](validation/index.md) * [apply](validation/apply.md) * [Computation Expression](validation/ce.md) - * [error](pr.md) + * [error](validation/error.md) * [map](pr.md) * [map2](validation/map2.md) * [map3](validation/map3.md) * [mapError](pr.md) * [mapErrors](pr.md) * [Operators](validation/operators.md) - * [returnError](pr.md) * [zip](pr.md) * Transforms * [ofChoice](validation/ofChoice.md) @@ -218,14 +217,13 @@ * [AsyncValidation](asyncValidation/index.md) * [apply](asyncValidation/apply.md) * [Computation Expression](asyncValidation/ce.md) - * [error](pr.md) + * [error](asyncValidation/error.md) * [map](pr.md) * [map2](asyncValidation/map2.md) * [map3](asyncValidation/map3.md) * [mapError](pr.md) * [mapErrors](pr.md) * [Operators](asyncValidation/operators.md) - * [returnError](pr.md) * [zip](pr.md) * Transforms * [ofChoice](asyncValidation/ofChoice.md) @@ -251,7 +249,7 @@ * [apply](cancellableTaskValidation/apply.md) * [bind](pr.md) * [Computation Expression](cancellableTaskValidation/ce.md) - * [error](pr.md) + * [error](cancellableTaskValidation/error.md) * [getCancellationToken](pr.md) * [map](pr.md) * [map2](cancellableTaskValidation/map2.md) @@ -285,7 +283,7 @@ * [bind](jobResult/bind.md) * [catch](pr.md) * [Computation Expression](jobResult/ce.md) - * [error](pr.md) + * [error](jobResult/error.md) * [fromTask](pr.md) * [fromUnitTask](pr.md) * [ignore](jobResult/ignore.md) @@ -295,7 +293,6 @@ * [mapError](jobResult/mapError.md) * [Operators](jobResult/operators.md) * [Other Functions](jobResult/others.md) - * [returnError](pr.md) * [zip](pr.md) * [zipError](pr.md) * Lists diff --git a/gitbook/asyncResult/error.md b/gitbook/asyncResult/error.md new file mode 100644 index 00000000..09cfd13e --- /dev/null +++ b/gitbook/asyncResult/error.md @@ -0,0 +1,22 @@ +## AsyncResult.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an 'error value into an Async> + +## Function Signature: + +```fsharp +'error -> Async> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Async> = + AsyncResult.error "Something bad happened" +``` + diff --git a/gitbook/asyncValidation/error.md b/gitbook/asyncValidation/error.md new file mode 100644 index 00000000..48084b44 --- /dev/null +++ b/gitbook/asyncValidation/error.md @@ -0,0 +1,22 @@ +## CancellableTaskValidation + +Namespace: `FsToolkit.ErrorHandling` + +Lift an 'error value into an CancellableTaskValidation<'ok, 'error> + +## Function Signature: + +```fsharp +'error -> CancellableTaskValidation<'ok, 'error> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : CancellableTaskValidation = + CancellableTaskValidation.error "Something bad happened" +``` + diff --git a/gitbook/cancellableTaskValidation/error.md b/gitbook/cancellableTaskValidation/error.md new file mode 100644 index 00000000..d7c10a4c --- /dev/null +++ b/gitbook/cancellableTaskValidation/error.md @@ -0,0 +1,22 @@ +## CancellableTaskValidation.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an 'error value into an Async> + +## Function Signature: + +```fsharp +'error -> Async> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Async> = + AsyncValidation.error "Something bad happened" +``` + diff --git a/gitbook/jobResult/error.md b/gitbook/jobResult/error.md new file mode 100644 index 00000000..f140b108 --- /dev/null +++ b/gitbook/jobResult/error.md @@ -0,0 +1,22 @@ +## JobResult.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an 'error value into an Job> + +## Function Signature: + +```fsharp +'error -> Job> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Job> = + JobResult.error "Something bad happened" +``` + diff --git a/gitbook/taskResult/error.md b/gitbook/taskResult/error.md new file mode 100644 index 00000000..02c9ca40 --- /dev/null +++ b/gitbook/taskResult/error.md @@ -0,0 +1,22 @@ +## TaskResult.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an 'error value into an Task> + +## Function Signature: + +```fsharp +'error -> Task> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Task> = + TaskResult.error "Something bad happened" +``` + diff --git a/gitbook/validation/error.md b/gitbook/validation/error.md new file mode 100644 index 00000000..2283d5df --- /dev/null +++ b/gitbook/validation/error.md @@ -0,0 +1,22 @@ +## Validation.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an 'error value into an Validation<'ok, 'error> + +## Function Signature: + +```fsharp +'error -> Validation<'ok, 'error> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Validation = + Validation.error "Something bad happened" +``` + diff --git a/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs b/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs index 63b8c4e6..dfbc2937 100644 --- a/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs +++ b/src/FsToolkit.ErrorHandling.JobResult/JobResult.fs @@ -43,12 +43,10 @@ module JobResult = Ok x |> Job.result - let inline returnError x = + let inline error x = Error x |> Job.result - let inline error x = returnError x - let inline map2 ([] f) xJR yJR = Job.map2 (Result.map2 f) xJR yJR let inline map3 ([] f) xJR yJR zJR = Job.map3 (Result.map3 f) xJR yJR zJR diff --git a/src/FsToolkit.ErrorHandling.JobResult/List.fs b/src/FsToolkit.ErrorHandling.JobResult/List.fs index 2f9405fe..51594f6b 100644 --- a/src/FsToolkit.ErrorHandling.JobResult/List.fs +++ b/src/FsToolkit.ErrorHandling.JobResult/List.fs @@ -42,9 +42,9 @@ module List = match s, fR with | Ok ys, Ok y -> return! traverseJobResultA' (JobResult.singleton (y :: ys)) f xs | Error errs, Error e -> - return! traverseJobResultA' (JobResult.returnError (e :: errs)) f xs - | Ok _, Error e -> return! traverseJobResultA' (JobResult.returnError [ e ]) f xs - | Error e, Ok _ -> return! traverseJobResultA' (JobResult.returnError e) f xs + return! traverseJobResultA' (JobResult.error (e :: errs)) f xs + | Ok _, Error e -> return! traverseJobResultA' (JobResult.error [ e ]) f xs + | Error e, Ok _ -> return! traverseJobResultA' (JobResult.error e) f xs } diff --git a/src/FsToolkit.ErrorHandling/Array.fs b/src/FsToolkit.ErrorHandling/Array.fs index 78decbe4..a8f2cca7 100644 --- a/src/FsToolkit.ErrorHandling/Array.fs +++ b/src/FsToolkit.ErrorHandling/Array.fs @@ -91,13 +91,9 @@ module Array = return! traverseAsyncResultA' (AsyncResult.ok (Array.append [| y |] ys)) f xs | Error errs, Error e -> return! - traverseAsyncResultA' - (AsyncResult.returnError (Array.append [| e |] errs)) - f - xs - | Ok _, Error e -> - return! traverseAsyncResultA' (AsyncResult.returnError [| e |]) f xs - | Error e, Ok _ -> return! traverseAsyncResultA' (AsyncResult.returnError e) f xs + traverseAsyncResultA' (AsyncResult.error (Array.append [| e |] errs)) f xs + | Ok _, Error e -> return! traverseAsyncResultA' (AsyncResult.error [| e |]) f xs + | Error e, Ok _ -> return! traverseAsyncResultA' (AsyncResult.error e) f xs } let traverseResultA f xs = traverseResultA' (Ok [||]) f xs diff --git a/src/FsToolkit.ErrorHandling/AsyncResult.fs b/src/FsToolkit.ErrorHandling/AsyncResult.fs index 0335121e..5b0d5d95 100644 --- a/src/FsToolkit.ErrorHandling/AsyncResult.fs +++ b/src/FsToolkit.ErrorHandling/AsyncResult.fs @@ -9,12 +9,10 @@ module AsyncResult = Ok value |> Async.singleton - let inline returnError (error: 'error) : Async> = + let inline error (error: 'error) : Async> = Error error |> Async.singleton - let inline error (error: 'error) : Async> = returnError error - let inline map ([] mapper: 'input -> 'output) (input: Async>) @@ -31,7 +29,7 @@ module AsyncResult = ([] binder: 'input -> Async>) (input: Async>) : Async> = - Async.bind (Result.either binder returnError) input + Async.bind (Result.either binder error) input let inline foldResult ([] onSuccess: 'input -> 'output) diff --git a/src/FsToolkit.ErrorHandling/AsyncValidation.fs b/src/FsToolkit.ErrorHandling/AsyncValidation.fs index 326005f4..05a82885 100644 --- a/src/FsToolkit.ErrorHandling/AsyncValidation.fs +++ b/src/FsToolkit.ErrorHandling/AsyncValidation.fs @@ -43,10 +43,6 @@ module AsyncValidation = ) } - let inline returnError (error: 'error) : AsyncValidation<'ok, 'error> = - Error [ error ] - |> async.Return - let inline orElse (ifError: AsyncValidation<'ok, 'errorOutput>) (result: AsyncValidation<'ok, 'errorInput>) diff --git a/src/FsToolkit.ErrorHandling/List.fs b/src/FsToolkit.ErrorHandling/List.fs index 901258f5..390cc05e 100644 --- a/src/FsToolkit.ErrorHandling/List.fs +++ b/src/FsToolkit.ErrorHandling/List.fs @@ -79,10 +79,9 @@ module List = match s, fR with | Ok ys, Ok y -> return! traverseAsyncResultA' (AsyncResult.ok (y :: ys)) f xs | Error errs, Error e -> - return! traverseAsyncResultA' (AsyncResult.returnError (e :: errs)) f xs - | Ok _, Error e -> - return! traverseAsyncResultA' (AsyncResult.returnError [ e ]) f xs - | Error e, Ok _ -> return! traverseAsyncResultA' (AsyncResult.returnError e) f xs + return! traverseAsyncResultA' (AsyncResult.error (e :: errs)) f xs + | Ok _, Error e -> return! traverseAsyncResultA' (AsyncResult.error [ e ]) f xs + | Error e, Ok _ -> return! traverseAsyncResultA' (AsyncResult.error e) f xs } let traverseResultA f xs = traverseResultA' (Ok []) f xs diff --git a/src/FsToolkit.ErrorHandling/TaskResult.fs b/src/FsToolkit.ErrorHandling/TaskResult.fs index 8abfa50c..484f64d0 100644 --- a/src/FsToolkit.ErrorHandling/TaskResult.fs +++ b/src/FsToolkit.ErrorHandling/TaskResult.fs @@ -28,12 +28,10 @@ module TaskResult = Ok x |> Task.singleton - let inline returnError x = + let inline error x = Error x |> Task.singleton - let inline error x = returnError x - let inline map2 ([] f) xTR yTR = Task.map2 (Result.map2 f) xTR yTR let inline map3 ([] f) xTR yTR zTR = Task.map3 (Result.map3 f) xTR yTR zTR diff --git a/src/FsToolkit.ErrorHandling/Validation.fs b/src/FsToolkit.ErrorHandling/Validation.fs index 95f630cc..e3ea642a 100644 --- a/src/FsToolkit.ErrorHandling/Validation.fs +++ b/src/FsToolkit.ErrorHandling/Validation.fs @@ -31,9 +31,6 @@ module Validation = @ errs2 ) - let inline returnError (error: 'error) : Validation<'ok, 'error> = Error [ error ] - - /// /// Returns if it is Ok, otherwise returns /// diff --git a/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs b/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs index 793d49d4..38ded2d7 100644 --- a/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs +++ b/tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs @@ -795,43 +795,3 @@ let bindRequireValueOptionTests = type CreatePostResult = | PostSuccess of NotifyNewPostRequest | NotAllowedToPost - -// [] -// let JobResultCETests = -// let createPost userId = taskResult { -// let! isAllowed = allowedToPost userId -// if isAllowed then -// let! postId = createPostSuccess validCreatePostRequest -// let! followerIds = getFollowersSuccess sampleUserId -// return PostSuccess {NewPostId = postId; UserIds = followerIds} -// else -// return NotAllowedToPost -// } -// testList "JobResult Computation Expression tests" [ -// testCase "bind with all Ok" <| fun _ -> -// createPost sampleUserId -// |> Expect.hasJobOkValueSync (PostSuccess {NewPostId = PostId newPostId; UserIds = followerIds}) - -// testCase "bind with an Error" <| fun _ -> -// createPost (UserId (System.Guid.NewGuid())) -// |> Expect.hasJobErrorValueSync commonEx -// ] - -// [] -// let JobResultOperatorTests = -// testList "JobResult Operators Tests" [ -// testCase "map & apply operators" <| fun _ -> -// let getFollowersResult = getFollowersSuccess sampleUserId -// let createPostResult = createPostSuccess validCreatePostRequest -// newPostRequest getFollowersResult <*> createPostResult -// |> Expect.hasJobOkValueSync {NewPostId = PostId newPostId; UserIds = followerIds} - -// testCase "bind operator" <| fun _ -> -// allowedToPost sampleUserId -// >>= (fun isAllowed -> -// if isAllowed then -// createPostSuccess validCreatePostRequest -// else -// JobResult.returnError (Exception "")) -// |> Expect.hasJobOkValueSync (PostId newPostId) -// ] diff --git a/tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs b/tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs index ae36e625..d52afcc3 100644 --- a/tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs +++ b/tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs @@ -851,7 +851,7 @@ let TaskResultOperatorTests = if isAllowed then createPostSuccess validCreatePostRequest else - TaskResult.returnError (Exception "") + TaskResult.error (Exception "") ) |> Expect.hasTaskOkValueSync (PostId newPostId) ] diff --git a/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs b/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs index be85a5e2..d26a4f39 100644 --- a/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs +++ b/tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs @@ -781,7 +781,7 @@ let asyncResultOperatorTests = if isAllowed then createPostSuccess validCreatePostRequest else - AsyncResult.returnError (Exception "") + AsyncResult.error (Exception "") ) |> Expect.hasAsyncOkValue (PostId newPostId) } From 928fbc528755437d128bb516c90ac34a1f06d53b Mon Sep 17 00:00:00 2001 From: Matthew Watt Date: Fri, 21 Feb 2025 09:21:09 -0600 Subject: [PATCH 2/2] Fix mixup with docs + touchup some formatting --- gitbook/asyncResult/error.md | 2 +- gitbook/asyncValidation/error.md | 10 +++++----- gitbook/cancellableTaskValidation/error.md | 10 +++++----- gitbook/jobResult/error.md | 2 +- gitbook/taskResult/error.md | 2 +- gitbook/validation/error.md | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/gitbook/asyncResult/error.md b/gitbook/asyncResult/error.md index 09cfd13e..8392ff81 100644 --- a/gitbook/asyncResult/error.md +++ b/gitbook/asyncResult/error.md @@ -2,7 +2,7 @@ Namespace: `FsToolkit.ErrorHandling` -Lift an 'error value into an Async> +Lift an `'error` value into an `Async>` ## Function Signature: diff --git a/gitbook/asyncValidation/error.md b/gitbook/asyncValidation/error.md index 48084b44..d8c55f76 100644 --- a/gitbook/asyncValidation/error.md +++ b/gitbook/asyncValidation/error.md @@ -1,13 +1,13 @@ -## CancellableTaskValidation +## AsyncValidation.error Namespace: `FsToolkit.ErrorHandling` -Lift an 'error value into an CancellableTaskValidation<'ok, 'error> +Lift an `'error` value into an `Async>` ## Function Signature: ```fsharp -'error -> CancellableTaskValidation<'ok, 'error> +'error -> Async> ``` ## Examples @@ -16,7 +16,7 @@ Lift an 'error value into an CancellableTaskValidation<'ok, 'error> ```fsharp -let result : CancellableTaskValidation = - CancellableTaskValidation.error "Something bad happened" +let result : Async> = + AsyncValidation.error "Something bad happened" ``` diff --git a/gitbook/cancellableTaskValidation/error.md b/gitbook/cancellableTaskValidation/error.md index d7c10a4c..3fd14354 100644 --- a/gitbook/cancellableTaskValidation/error.md +++ b/gitbook/cancellableTaskValidation/error.md @@ -1,13 +1,13 @@ -## CancellableTaskValidation.error +## CancellableTaskValidation Namespace: `FsToolkit.ErrorHandling` -Lift an 'error value into an Async> +Lift an `'error` value into an `CancellableTaskValidation<'ok, 'error>` ## Function Signature: ```fsharp -'error -> Async> +'error -> CancellableTaskValidation<'ok, 'error> ``` ## Examples @@ -16,7 +16,7 @@ Lift an 'error value into an Async> ```fsharp -let result : Async> = - AsyncValidation.error "Something bad happened" +let result : CancellableTaskValidation = + CancellableTaskValidation.error "Something bad happened" ``` diff --git a/gitbook/jobResult/error.md b/gitbook/jobResult/error.md index f140b108..32657b42 100644 --- a/gitbook/jobResult/error.md +++ b/gitbook/jobResult/error.md @@ -2,7 +2,7 @@ Namespace: `FsToolkit.ErrorHandling` -Lift an 'error value into an Job> +Lift an `'error` value into an `Job>` ## Function Signature: diff --git a/gitbook/taskResult/error.md b/gitbook/taskResult/error.md index 02c9ca40..97b28fce 100644 --- a/gitbook/taskResult/error.md +++ b/gitbook/taskResult/error.md @@ -2,7 +2,7 @@ Namespace: `FsToolkit.ErrorHandling` -Lift an 'error value into an Task> +Lift an `'error` value into an `Task>` ## Function Signature: diff --git a/gitbook/validation/error.md b/gitbook/validation/error.md index 2283d5df..d2a23847 100644 --- a/gitbook/validation/error.md +++ b/gitbook/validation/error.md @@ -2,7 +2,7 @@ Namespace: `FsToolkit.ErrorHandling` -Lift an 'error value into an Validation<'ok, 'error> +Lift an `'error` value into an `Validation<'ok, 'error>` ## Function Signature: