From caaecbe652955839c75d444f9f8ffa889b53f8d3 Mon Sep 17 00:00:00 2001 From: Andrii Chebukin Date: Tue, 7 Nov 2023 21:54:10 +0400 Subject: [PATCH] Changed `raise` to calling `Reraise()` extension method to preserve the call stack (#230) --- .../SwaggerProvider.DesignTime.fsproj | 1 + src/SwaggerProvider.DesignTime/Utils.fs | 2 +- .../v2/Parser/Parsers.fs | 4 +--- .../v2/Parser/SwaggerParser.fs | 7 +++++-- src/SwaggerProvider.Runtime/Exception.fs | 16 ++++++++++++++++ .../SwaggerProvider.Runtime.fsproj | 1 + 6 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 src/SwaggerProvider.Runtime/Exception.fs diff --git a/src/SwaggerProvider.DesignTime/SwaggerProvider.DesignTime.fsproj b/src/SwaggerProvider.DesignTime/SwaggerProvider.DesignTime.fsproj index 45bc994..6217e74 100644 --- a/src/SwaggerProvider.DesignTime/SwaggerProvider.DesignTime.fsproj +++ b/src/SwaggerProvider.DesignTime/SwaggerProvider.DesignTime.fsproj @@ -32,6 +32,7 @@ ProvidedApiClientBase.fs + AssemblyInfo.fs diff --git a/src/SwaggerProvider.DesignTime/Utils.fs b/src/SwaggerProvider.DesignTime/Utils.fs index 9933fa8..c633d76 100644 --- a/src/SwaggerProvider.DesignTime/Utils.fs +++ b/src/SwaggerProvider.DesignTime/Utils.fs @@ -52,7 +52,7 @@ module SchemaReader = return if String.IsNullOrEmpty err then - raise wex + wex.Reraise() else err.ToString() | Choice2Of2 e -> return failwith(e.ToString()) diff --git a/src/SwaggerProvider.DesignTime/v2/Parser/Parsers.fs b/src/SwaggerProvider.DesignTime/v2/Parser/Parsers.fs index 572af31..ec4abc2 100644 --- a/src/SwaggerProvider.DesignTime/v2/Parser/Parsers.fs +++ b/src/SwaggerProvider.DesignTime/v2/Parser/Parsers.fs @@ -361,9 +361,7 @@ module Parsers = else match Int32.TryParse(property) with | true, value -> Some value - | false, _ -> - raise - <| UnknownFieldValueException(obj, property, "HTTP Status Code", spec) + | false, _ -> raise(UnknownFieldValueException(obj, property, "HTTP Status Code", spec)) code, parseResponseObject context objValue) diff --git a/src/SwaggerProvider.DesignTime/v2/Parser/SwaggerParser.fs b/src/SwaggerProvider.DesignTime/v2/Parser/SwaggerParser.fs index 32a8c6f..3f5b560 100644 --- a/src/SwaggerProvider.DesignTime/v2/Parser/SwaggerParser.fs +++ b/src/SwaggerProvider.DesignTime/v2/Parser/SwaggerParser.fs @@ -4,6 +4,7 @@ open SwaggerProvider.Internal.v2.Parser.Schema open SwaggerProvider.Internal.v2.Parser.Exceptions module internal JsonAdapter = + open System.Text.Json /// Schema node for Swagger schemes in Json format @@ -52,9 +53,11 @@ module internal JsonAdapter = (JsonDocument.Parse string).RootElement |> JsonNodeAdapter module internal YamlAdapter = + + open System + open System.Collections.Generic open System.IO open YamlDotNet.Serialization - open System.Collections.Generic let (|List|_|)(node: obj) = match node with @@ -138,7 +141,7 @@ module internal YamlAdapter = use reader = new StringReader(text) deserializer.Deserialize(reader) |> YamlNodeAdapter with - | :? YamlDotNet.Core.YamlException as e when not <| isNull e.InnerException -> raise e.InnerException // inner exceptions are much more informative + | :? YamlDotNet.Core.YamlException as e when not <| isNull e.InnerException -> e.InnerException.Reraise() // inner exceptions are much more informative | _ -> reraise() module SwaggerParser = diff --git a/src/SwaggerProvider.Runtime/Exception.fs b/src/SwaggerProvider.Runtime/Exception.fs new file mode 100644 index 0000000..e7f8294 --- /dev/null +++ b/src/SwaggerProvider.Runtime/Exception.fs @@ -0,0 +1,16 @@ +[] +[] +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 Exception with + + [] + member __.Reraise() = + (ExceptionDispatchInfo.Capture __).Throw() + Unchecked.defaultof<_> diff --git a/src/SwaggerProvider.Runtime/SwaggerProvider.Runtime.fsproj b/src/SwaggerProvider.Runtime/SwaggerProvider.Runtime.fsproj index 8c363a2..29b12b6 100644 --- a/src/SwaggerProvider.Runtime/SwaggerProvider.Runtime.fsproj +++ b/src/SwaggerProvider.Runtime/SwaggerProvider.Runtime.fsproj @@ -21,6 +21,7 @@ AssemblyInfo.fs +