Skip to content

Commit

Permalink
Changed raise to calling Reraise() extension method to preserve t…
Browse files Browse the repository at this point in the history
…he call stack
  • Loading branch information
xperiandri committed Nov 5, 2023
1 parent eb91858 commit b836eed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/FSharp.Data.GraphQL.Shared/AsyncVal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module AsyncVal =
match x with
| Value v -> v
| Async a -> a |> Async.RunSynchronously
| Failure f -> raise f
| Failure f -> f.Reraise()

/// Create new AsyncVal from Async computation.
let inline ofAsync (a: Async<'T>) = Async(a)
Expand All @@ -54,7 +54,7 @@ module AsyncVal =
match x with
| Value v -> async.Return v
| Async a -> a
| Failure f -> async.Return (raise f)
| Failure f -> async.Return (f.Reraise())

/// Returns an empty AsyncVal with immediatelly executed value.
let inline empty<'T> : AsyncVal<'T> = AsyncVal<'T>.Zero
Expand Down Expand Up @@ -111,7 +111,7 @@ module AsyncVal =
match bound with
| Value v -> return v
| Async a -> return! a
| Failure f -> return raise f
| Failure f -> return f.Reraise()
})
| Failure f -> Failure(f)

Expand All @@ -133,7 +133,7 @@ module AsyncVal =
let! r = a
results.[i] <- r
| Failure f ->
results.[i] <- raise f
results.[i] <- f.Reraise()
return results })
else Value (values |> Array.map (fun (Value v) -> v))

Expand All @@ -156,7 +156,7 @@ module AsyncVal =
indexes.Add i
continuations.Add a
| Failure f ->
results.[i] <- raise f
results.[i] <- f.Reraise()
if indexes.Count = 0
then Value(results)
else Async(async {
Expand Down Expand Up @@ -193,7 +193,7 @@ type AsyncValBuilder () =
match bound with
| Value v -> return v
| Async a -> return! a
| Failure f -> return raise f })
| Failure f -> return f.Reraise() })


[<AutoOpen>]
Expand Down
16 changes: 16 additions & 0 deletions src/FSharp.Data.GraphQL.Shared/Exception.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[<AutoOpen>]
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module System.Exception

open System
open System.Diagnostics
open System.Runtime.ExceptionServices

// Useful for reraising exceptions under an async {...} and task {...} contexts
// See this for more details: https://github.com/fsharp/fslang-suggestions/issues/660
type internal Exception with

[<DebuggerHidden>]
member __.Reraise () =
(ExceptionDispatchInfo.Capture __).Throw ()
Unchecked.defaultof<_>
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="Helpers\Reflection.fs" />
<Compile Include="Helpers\MemoryCache.fs" />
<Compile Include="Errors.fs" />
<Compile Include="Exception.fs" />
<Compile Include="ValidationTypes.fs" />
<Compile Include="AsyncVal.fs" />
<Compile Include="Ast.fs" />
Expand Down
2 changes: 1 addition & 1 deletion tests/FSharp.Data.GraphQL.Tests/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type TestObserver<'T>(obs : IObservable<'T>, ?onReceived : TestObserver<'T> -> '
member _.OnCompleted() =
isCompleted <- true
mre.Set() |> ignore
member _.OnError(error) = raise error
member _.OnError(error) = error.Reraise()
member _.OnNext(value) =
received.Add(value)
onReceived |> Option.iter (fun evt -> evt this value)
Expand Down

0 comments on commit b836eed

Please sign in to comment.