Skip to content

Commit 33b72c3

Browse files
committed
Add Exception module
1 parent 3eb1db7 commit 33b72c3

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/FSharpPlus/Control/Monoid.fs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ type Plus =
3131
#if !FABLE_COMPILER
3232
static member ``+`` (x: StringBuilder , y: StringBuilder , [<Optional>]_mthd: Plus ) = StringBuilder().Append(x).Append(y)
3333
static member ``+`` (_: Id0 , _: Id0 , [<Optional>]_mthd: Plus ) = Id0 ""
34-
static member ``+`` (x: AggregateException, y: AggregateException, [<Optional>]_mthd: Plus ) = new AggregateException (seq {yield! x.InnerExceptions; yield! y.InnerExceptions})
35-
static member ``+`` (x: exn , y: exn , [<Optional>]_mthd: Plus ) =
36-
let f (e: exn) = match e with :? AggregateException as a -> a.InnerExceptions :> seq<_> | _ -> Seq.singleton e
37-
let left = f x
38-
new AggregateException (seq { yield! left; yield! Seq.except left (f y) }) :> exn
34+
static member ``+`` (x: AggregateException, y: AggregateException, [<Optional>]_mthd: Plus ) = Exception.add x y
35+
static member ``+`` (x: exn , y: exn , [<Optional>]_mthd: Plus ) = Exception.add x y :> exn
3936
#else
4037
static member ``+`` (x: StringBuilder , y: StringBuilder , [<Optional>]_mthd: Plus ) = StringBuilder().Append(string x).Append(string y)
4138
static member ``+`` (_: Id0 , _: Id0 , [<Optional>]_mthd: Plus ) = Id0 ""
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace FSharpPlus
2+
3+
/// Additional operations on Exception
4+
[<RequireQualifiedAccess>]
5+
module Exception =
6+
open System
7+
open System.Runtime.ExceptionServices
8+
open FSharpPlus.Internals.Errors
9+
10+
#if !FABLE_COMPILER
11+
12+
/// Throws the given exception with its original stacktrace.
13+
let inline rethrow<'T> (exn: exn) =
14+
raiseIfNull (nameof exn) exn
15+
(ExceptionDispatchInfo.Capture exn).Throw ()
16+
Unchecked.defaultof<'T>
17+
18+
/// Combines exceptions from 2 exceptions into a single AggregateException.
19+
/// Exceptions already present in the first argument won't be added.
20+
let add (exn1: exn) (exn2: exn) =
21+
raiseIfNull (nameof exn1) exn1
22+
raiseIfNull (nameof exn2) exn2
23+
let f (e: exn) =
24+
match e with
25+
:? AggregateException as a -> a.InnerExceptions :> seq<_>
26+
| _ -> Seq.singleton e
27+
let left = f exn1
28+
new AggregateException (seq { yield! left; yield! Seq.except left (f exn2) })
29+
30+
#endif

src/FSharpPlus/FSharpPlus.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<Compile Include="Extensions/Nullable.fs" />
3939
<Compile Include="Extensions/Result.fs" />
4040
<Compile Include="Extensions/Choice.fs" />
41+
<Compile Include="Extensions/Exception.fs" />
4142
<Compile Include="Extensions/Seq.fs" />
4243
<Compile Include="Extensions/IList.fs" />
4344
<Compile Include="Extensions/List.fs" />

0 commit comments

Comments
 (0)