Skip to content

Commit c24ec6e

Browse files
committed
Changed raise to calling Reraise() extension method to preserve the call stack
1 parent e96f1d7 commit c24ec6e

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/FSharp.Data.GraphQL.Shared/AsyncVal.fs

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module AsyncVal =
4141
match x with
4242
| Value v -> v
4343
| Async a -> a |> Async.RunSynchronously
44-
| Failure f -> raise f
44+
| Failure f -> f.Reraise()
4545

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

5959
/// Returns an empty AsyncVal with immediatelly executed value.
6060
let inline empty<'T> : AsyncVal<'T> = AsyncVal<'T>.Zero
@@ -111,7 +111,7 @@ module AsyncVal =
111111
match bound with
112112
| Value v -> return v
113113
| Async a -> return! a
114-
| Failure f -> return raise f
114+
| Failure f -> return f.Reraise()
115115
})
116116
| Failure f -> Failure(f)
117117

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

@@ -156,7 +156,7 @@ module AsyncVal =
156156
indexes.Add i
157157
continuations.Add a
158158
| Failure f ->
159-
results.[i] <- raise f
159+
results.[i] <- f.Reraise()
160160
if indexes.Count = 0
161161
then Value(results)
162162
else Async(async {
@@ -193,7 +193,7 @@ type AsyncValBuilder () =
193193
match bound with
194194
| Value v -> return v
195195
| Async a -> return! a
196-
| Failure f -> return raise f })
196+
| Failure f -> return f.Reraise() })
197197

198198

199199
[<AutoOpen>]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[<AutoOpen>]
2+
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
3+
module System.Exception
4+
5+
open System
6+
open System.Diagnostics
7+
open System.Runtime.ExceptionServices
8+
9+
// Useful for reraising exceptions under an async {...} and task {...} contexts
10+
// See this for more details: https://github.com/fsharp/fslang-suggestions/issues/660
11+
type internal Exception with
12+
13+
[<DebuggerHidden>]
14+
member __.Reraise () =
15+
(ExceptionDispatchInfo.Capture __).Throw ()
16+
Unchecked.defaultof<_>

src/FSharp.Data.GraphQL.Shared/FSharp.Data.GraphQL.Shared.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Helpers\Reflection.fs" />
5050
<Compile Include="Helpers\MemoryCache.fs" />
5151
<Compile Include="Errors.fs" />
52+
<Compile Include="Exception.fs" />
5253
<Compile Include="ValidationTypes.fs" />
5354
<Compile Include="AsyncVal.fs" />
5455
<Compile Include="Ast.fs" />

tests/FSharp.Data.GraphQL.Tests/Helpers.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ type TestObserver<'T>(obs : IObservable<'T>, ?onReceived : TestObserver<'T> -> '
146146
member _.OnCompleted() =
147147
isCompleted <- true
148148
mre.Set() |> ignore
149-
member _.OnError(error) = raise error
149+
member _.OnError(error) = error.Reraise()
150150
member _.OnNext(value) =
151151
received.Add(value)
152152
onReceived |> Option.iter (fun evt -> evt this value)

0 commit comments

Comments
 (0)