-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstackErrorException.cs
More file actions
144 lines (131 loc) · 5.11 KB
/
ContentstackErrorException.cs
File metadata and controls
144 lines (131 loc) · 5.11 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json.Serialization;
namespace Contentstack.Management.Core.Exceptions
{
/// <summary>
/// A base exception for Contentstack API.
/// </summary>
public class ContentstackErrorException: Exception
{
#region Private Variables
private string _ErrorMessage = string.Empty;
#endregion
#region Public Variables
/// <summary>
/// This is http response status code of REST request to Contentstack.
/// </summary>
public HttpStatusCode StatusCode { get; set; }
/// <summary>
/// This is http response Header of REST request to Contentstack.
/// </summary>
public HttpResponseHeaders Header { get; set; }
/// <summary>
/// This is http response phrase code of REST request to Contentstack.
/// </summary>
public string ReasonPhrase { get; set; }
/// <summary>
/// This is error message.
/// </summary>
public new string Message { get; set; }
/// <summary>
/// This is error message.
/// </summary>
[JsonProperty("error_message")]
[JsonPropertyName("error_message")]
public string ErrorMessage
{
get
{
return this._ErrorMessage;
}
set
{
this._ErrorMessage = value;
this.Message = value;
}
}
/// <summary>
/// This is error code.
/// </summary>
[JsonProperty("error_code")]
[JsonPropertyName("error_code")]
public int ErrorCode { get; set; }
/// <summary>
/// Set of errors in detail.
/// </summary>
[JsonProperty("errors")]
[JsonPropertyName("errors")]
public Dictionary<string, object> Errors { get; set; }
/// <summary>
/// Number of retry attempts made before this exception was thrown.
/// </summary>
public int RetryAttempts { get; set; }
/// <summary>
/// The original exception that caused this error, if this is a network error wrapped in an HTTP exception.
/// </summary>
public Exception OriginalError { get; set; }
/// <summary>
/// Indicates whether this error originated from a network failure.
/// </summary>
public bool IsNetworkError { get; set; }
#endregion
public static ContentstackErrorException CreateException(HttpResponseMessage response)
{
var stringResponse = response.Content.ReadAsStringAsync().Result;
ContentstackErrorException exception = null;
if (!string.IsNullOrEmpty(stringResponse))
{
try
{
exception = JObject.Parse(stringResponse).ToObject<ContentstackErrorException>();
}
catch (JsonReaderException)
{
// Handle HTML error responses or other non-JSON content
exception = new ContentstackErrorException();
// Extract meaningful error message from HTML if possible
if (stringResponse.Contains("Cannot GET") || stringResponse.Contains("Cannot POST") || stringResponse.Contains("Cannot PUT") || stringResponse.Contains("Cannot DELETE"))
{
// Extract the endpoint path from HTML error message
var startIndex = stringResponse.IndexOf("Cannot");
var endIndex = stringResponse.IndexOf("</pre>", startIndex);
if (startIndex >= 0 && endIndex > startIndex)
{
var errorMessage = stringResponse.Substring(startIndex, endIndex - startIndex).Trim();
exception.ErrorMessage = $"API endpoint error: {errorMessage}";
}
else
{
exception.ErrorMessage = "API endpoint not found or not supported";
}
}
else
{
exception.ErrorMessage = "Invalid response format received from server";
}
}
catch (Exception ex)
{
// Handle any other JSON parsing issues
exception = new ContentstackErrorException();
exception.ErrorMessage = $"Failed to parse server response: {ex.Message}";
}
}
else
{
exception = new ContentstackErrorException();
}
exception.StatusCode = response.StatusCode;
exception.Header = response.Headers;
exception.ReasonPhrase = response.ReasonPhrase;
return exception;
}
public ContentstackErrorException() { }
}
}