/
JsonDocumentUtility.fs
37 lines (32 loc) 路 1.29 KB
/
JsonDocumentUtility.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace Songhay.Modules.Bolero
open System.Net
open System.Text.Json
open Microsoft.Extensions.Logging
open FsToolkit.ErrorHandling
open FsToolkit.ErrorHandling.Operator.Result
open Songhay.Modules.JsonDocumentUtility
/// <summary>
/// Bolero/Blazor-specific functions, wrapping <see cref="Songhay.Modules.JsonDocumentUtility" />.
/// </summary>
module JsonDocumentUtility =
/// <summary>
/// Wraps <see cref="tryGetRootElement" /> with the specified <see cref="ILogger" />,
/// binding to the specified JSON Result.
/// </summary>
let tryGetJsonElement (logger: ILogger option) (jsonResult: Result<string, HttpStatusCode>) =
jsonResult
|> Result.mapError
(
fun code ->
if logger.IsSome then
logger.Value.LogError($"The expected {nameof HttpStatusCode}, `{code},` is not here.")
JsonException <| $"{nameof HttpStatusCode}: {code.ToString()}"
)
>>= fun json -> json |> tryGetRootElement
|> Result.mapError
(
fun err ->
if logger.IsSome then
logger.Value.LogError(err.Message, err.GetType().FullName, err.Source, err.StackTrace)
JsonException err.Message
)