-
Notifications
You must be signed in to change notification settings - Fork 186
/
CsomError.cs
85 lines (77 loc) · 3.09 KB
/
CsomError.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Text;
using System.Text.Json;
namespace PnP.Core
{
/// <summary>
/// Csom service error
/// </summary>
public class CsomError : ServiceError
{
/// <summary>
/// Default constructor for the <see cref="SharePointRestError"/> error
/// </summary>
/// <param name="type"><see cref="ErrorType"/> type of the error</param>
/// <param name="httpResponseCode">Http response code of the service request that failed</param>
/// <param name="errorJson">Json containing the error information</param>
public CsomError(ErrorType type, int httpResponseCode, JsonElement errorJson) : base(type, httpResponseCode)
{
ParseError(errorJson);
}
/// <summary>
/// SharePoint server error code
/// </summary>
public long ServerErrorCode { get; private set; }
/// <summary>
/// Outputs a <see cref="CsomError"/> to a string representation
/// </summary>
/// <returns>String representation</returns>
public override string ToString()
{
var errorString = new StringBuilder();
errorString.AppendLine($"HttpResponseCode: {HttpResponseCode}");
errorString.AppendLine($"Message: {Message}");
errorString.AppendLine($"ClientRequestId: {ClientRequestId}");
foreach (var property in AdditionalData)
{
errorString.AppendLine($"{property.Key}: {property.Value}");
}
return errorString.ToString();
}
private void ParseError(JsonElement error)
{
ClientRequestId = error.GetProperty("TraceCorrelationId").GetGuid().ToString();
var errorInfo = error.GetProperty("ErrorInfo");
if (errorInfo.ValueKind != JsonValueKind.Null)
{
if (errorInfo.TryGetProperty("ErrorMessage", out JsonElement errorMessage))
{
if (errorMessage.ValueKind == JsonValueKind.String)
{
Message = errorMessage.GetString();
}
}
if (errorInfo.TryGetProperty("ErrorCode", out JsonElement errorCode))
{
if (errorCode.ValueKind == JsonValueKind.Number)
{
ServerErrorCode = errorCode.GetInt64();
}
}
if (errorInfo.TryGetProperty("ErrorValue", out JsonElement errorValue))
{
if (errorValue.ValueKind != JsonValueKind.Null)
{
AddAdditionalData("ErrorValue", errorValue.GetRawText());
}
}
if (errorInfo.TryGetProperty("ErrorTypeName", out JsonElement errorTypeName))
{
if (errorTypeName.ValueKind != JsonValueKind.Null)
{
AddAdditionalData("ErrorTypeName", errorTypeName.GetRawText());
}
}
}
}
}
}