-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrowHelper.cs
More file actions
65 lines (58 loc) · 2.52 KB
/
Copy pathThrowHelper.cs
File metadata and controls
65 lines (58 loc) · 2.52 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Text.Json;
using System.Reflection;
using System;
using System.Diagnostics;
namespace DarkPatterns.OpenApiCodegen.Json.Extensions;
internal static class ThrowHelper
{
private static readonly PropertyInfo? s_JsonException_AppendPathInformation
= typeof(JsonException).GetProperty("AppendPathInformation", BindingFlags.NonPublic | BindingFlags.Instance);
/// <summary>
/// Generate a <see cref="JsonException"/> using the internal
/// <c>JsonException.AppendPathInformation</c> property that will
/// eventually include the JSON path, line number, and byte position in
/// line.
/// <para>
/// The final message of the exception looks like: The JSON value could
/// not be converted to {0}. Path: $.{JSONPath} | LineNumber:
/// {LineNumber} | BytePositionInLine: {BytePositionInLine}.
/// </para>
/// </summary>
/// <param name="propertyType">Property type.</param>
/// <returns><see cref="JsonException"/>.</returns>
public static JsonException GenerateJsonException_DeserializeUnableToConvertValue(Type propertyType)
{
Debug.Assert(s_JsonException_AppendPathInformation != null);
JsonException jsonException = new JsonException($"The JSON value could not be converted to {propertyType}.");
s_JsonException_AppendPathInformation?.SetValue(jsonException, true);
return jsonException;
}
/// <summary>
/// Generate a <see cref="JsonException"/> using the internal
/// <c>JsonException.AppendPathInformation</c> property that will
/// eventually include the JSON path, line number, and byte position in
/// line.
/// <para>
/// The final message of the exception looks like: The JSON value '{1}'
/// could not be converted to {0}. Path: $.{JSONPath} | LineNumber:
/// {LineNumber} | BytePositionInLine: {BytePositionInLine}.
/// </para>
/// </summary>
/// <param name="propertyType">Property type.</param>
/// <param name="propertyValue">Value that could not be parsed into
/// property type.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/>.</param>
/// <returns><see cref="JsonException"/>.</returns>
public static JsonException GenerateJsonException_DeserializeUnableToConvertValue(
Type propertyType,
string propertyValue,
Exception? innerException = null)
{
Debug.Assert(s_JsonException_AppendPathInformation != null);
JsonException jsonException = new JsonException(
$"The JSON value '{propertyValue}' could not be converted to {propertyType}.",
innerException);
s_JsonException_AppendPathInformation?.SetValue(jsonException, true);
return jsonException;
}
}