Skip to content

Commit 59769df

Browse files
committed
fix: NullReferenceException and JsonReaderException in Taxonomy model; improve error handling and messaging.
1 parent f8e629c commit 59769df

File tree

3 files changed

+73
-20
lines changed

3 files changed

+73
-20
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
### Version: 2.25.2
2+
#### Date: Nov-13-2025
3+
4+
##### Fix:
5+
- Taxonomy
6+
- Fixed NullReferenceException in `_Url` property when Stack is null
7+
- Fixed NullReferenceException in constructor when ContentstackClient parameter is null
8+
- Fixed InvalidCastException in `GetContentstackError` when exception is not a WebException
9+
- Fixed NullReferenceException in `GetContentstackError` when WebException.Response is null
10+
- Fixed JsonReaderException in `GetContentstackError` when response is not valid JSON
11+
- All exceptions now properly throw TaxonomyException (extends ContentstackException) with descriptive error messages
12+
113
### Version: 2.25.1
214
#### Date: Nov-10-2025
315

Contentstack.Core/Models/Taxonomy.cs

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ protected override string _Url
2121
{
2222
get
2323
{
24+
if (this.Stack == null)
25+
{
26+
throw new TaxonomyException("Taxonomy Stack instance is null. Please ensure the Taxonomy is properly initialized with a ContentstackClient instance.");
27+
}
28+
if (this.Stack.Config == null)
29+
{
30+
throw new TaxonomyException("Taxonomy Stack Config is null. Please ensure the ContentstackClient is properly configured.");
31+
}
2432
Config config = this.Stack.Config;
2533
return String.Format("{0}/taxonomies/entries", config.BaseUrl);
2634
}
@@ -40,6 +48,10 @@ internal Taxonomy()
4048
}
4149
internal Taxonomy(ContentstackClient stack): base(stack)
4250
{
51+
if (stack == null)
52+
{
53+
throw new TaxonomyException("ContentstackClient instance cannot be null when creating a Taxonomy instance.");
54+
}
4355
this.Stack = stack;
4456
this._StackHeaders = stack._LocalHeaders;
4557
}
@@ -252,30 +264,59 @@ internal static ContentstackException GetContentstackError(Exception ex)
252264

253265
try
254266
{
255-
System.Net.WebException webEx = (System.Net.WebException)ex;
256-
257-
using (var exResp = webEx.Response)
258-
using (var stream = exResp.GetResponseStream())
259-
using (var reader = new StreamReader(stream))
267+
System.Net.WebException webEx = ex as System.Net.WebException;
268+
269+
if (webEx != null && webEx.Response != null)
260270
{
261-
errorMessage = reader.ReadToEnd();
262-
JObject data = JObject.Parse(errorMessage.Replace("\r\n", ""));
271+
using (var exResp = webEx.Response)
272+
{
273+
var stream = exResp.GetResponseStream();
274+
if (stream != null)
275+
{
276+
using (stream)
277+
using (var reader = new StreamReader(stream))
278+
{
279+
errorMessage = reader.ReadToEnd();
280+
281+
if (!string.IsNullOrWhiteSpace(errorMessage))
282+
{
283+
try
284+
{
285+
JObject data = JObject.Parse(errorMessage.Replace("\r\n", ""));
263286

264-
JToken token = data["error_code"];
265-
if (token != null)
266-
errorCode = token.Value<int>();
287+
JToken token = data["error_code"];
288+
if (token != null)
289+
errorCode = token.Value<int>();
267290

268-
token = data["error_message"];
269-
if (token != null)
270-
errorMessage = token.Value<string>();
291+
token = data["error_message"];
292+
if (token != null)
293+
errorMessage = token.Value<string>();
271294

272-
token = data["errors"];
273-
if (token != null)
274-
errors = token.ToObject<Dictionary<string, object>>();
295+
token = data["errors"];
296+
if (token != null)
297+
errors = token.ToObject<Dictionary<string, object>>();
298+
}
299+
catch (Newtonsoft.Json.JsonException)
300+
{
301+
// If JSON parsing fails, use the raw error message
302+
// errorMessage is already set from ReadToEnd()
303+
}
304+
}
275305

276-
var response = exResp as HttpWebResponse;
277-
if (response != null)
278-
statusCode = response.StatusCode;
306+
var response = exResp as HttpWebResponse;
307+
if (response != null)
308+
statusCode = response.StatusCode;
309+
}
310+
}
311+
else
312+
{
313+
errorMessage = webEx.Message;
314+
}
315+
}
316+
}
317+
else
318+
{
319+
errorMessage = ex.Message;
279320
}
280321
}
281322
catch

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>2.25.1</Version>
3+
<Version>2.25.2</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)