From 59769df4b9860f6f0504706ca3131c619ea9a672 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Wed, 12 Nov 2025 12:08:48 +0530 Subject: [PATCH 1/2] fix: NullReferenceException and JsonReaderException in Taxonomy model; improve error handling and messaging. --- CHANGELOG.md | 12 +++++ Contentstack.Core/Models/Taxonomy.cs | 79 +++++++++++++++++++++------- Directory.Build.props | 2 +- 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db238bf..b08d5ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +### Version: 2.25.2 +#### Date: Nov-13-2025 + +##### Fix: +- Taxonomy + - Fixed NullReferenceException in `_Url` property when Stack is null + - Fixed NullReferenceException in constructor when ContentstackClient parameter is null + - Fixed InvalidCastException in `GetContentstackError` when exception is not a WebException + - Fixed NullReferenceException in `GetContentstackError` when WebException.Response is null + - Fixed JsonReaderException in `GetContentstackError` when response is not valid JSON + - All exceptions now properly throw TaxonomyException (extends ContentstackException) with descriptive error messages + ### Version: 2.25.1 #### Date: Nov-10-2025 diff --git a/Contentstack.Core/Models/Taxonomy.cs b/Contentstack.Core/Models/Taxonomy.cs index a125833..9d7322f 100644 --- a/Contentstack.Core/Models/Taxonomy.cs +++ b/Contentstack.Core/Models/Taxonomy.cs @@ -21,6 +21,14 @@ protected override string _Url { get { + if (this.Stack == null) + { + throw new TaxonomyException("Taxonomy Stack instance is null. Please ensure the Taxonomy is properly initialized with a ContentstackClient instance."); + } + if (this.Stack.Config == null) + { + throw new TaxonomyException("Taxonomy Stack Config is null. Please ensure the ContentstackClient is properly configured."); + } Config config = this.Stack.Config; return String.Format("{0}/taxonomies/entries", config.BaseUrl); } @@ -40,6 +48,10 @@ internal Taxonomy() } internal Taxonomy(ContentstackClient stack): base(stack) { + if (stack == null) + { + throw new TaxonomyException("ContentstackClient instance cannot be null when creating a Taxonomy instance."); + } this.Stack = stack; this._StackHeaders = stack._LocalHeaders; } @@ -252,30 +264,59 @@ internal static ContentstackException GetContentstackError(Exception ex) try { - System.Net.WebException webEx = (System.Net.WebException)ex; - - using (var exResp = webEx.Response) - using (var stream = exResp.GetResponseStream()) - using (var reader = new StreamReader(stream)) + System.Net.WebException webEx = ex as System.Net.WebException; + + if (webEx != null && webEx.Response != null) { - errorMessage = reader.ReadToEnd(); - JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); + using (var exResp = webEx.Response) + { + var stream = exResp.GetResponseStream(); + if (stream != null) + { + using (stream) + using (var reader = new StreamReader(stream)) + { + errorMessage = reader.ReadToEnd(); + + if (!string.IsNullOrWhiteSpace(errorMessage)) + { + try + { + JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); - JToken token = data["error_code"]; - if (token != null) - errorCode = token.Value(); + JToken token = data["error_code"]; + if (token != null) + errorCode = token.Value(); - token = data["error_message"]; - if (token != null) - errorMessage = token.Value(); + token = data["error_message"]; + if (token != null) + errorMessage = token.Value(); - token = data["errors"]; - if (token != null) - errors = token.ToObject>(); + token = data["errors"]; + if (token != null) + errors = token.ToObject>(); + } + catch (Newtonsoft.Json.JsonException) + { + // If JSON parsing fails, use the raw error message + // errorMessage is already set from ReadToEnd() + } + } - var response = exResp as HttpWebResponse; - if (response != null) - statusCode = response.StatusCode; + var response = exResp as HttpWebResponse; + if (response != null) + statusCode = response.StatusCode; + } + } + else + { + errorMessage = webEx.Message; + } + } + } + else + { + errorMessage = ex.Message; } } catch diff --git a/Directory.Build.props b/Directory.Build.props index e15a9a2..0e81c8f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 2.25.1 + 2.25.2 From f888bd15900650e3986fe2c24514d645fad1e76b Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Wed, 12 Nov 2025 12:45:22 +0530 Subject: [PATCH 2/2] chore: version bump removed and changelog modified --- CHANGELOG.md | 15 +++++++-------- Directory.Build.props | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b08d5ef..db9a175 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,20 +1,19 @@ ### Version: 2.25.2 #### Date: Nov-13-2025 -##### Fix: -- Taxonomy - - Fixed NullReferenceException in `_Url` property when Stack is null - - Fixed NullReferenceException in constructor when ContentstackClient parameter is null - - Fixed InvalidCastException in `GetContentstackError` when exception is not a WebException - - Fixed NullReferenceException in `GetContentstackError` when WebException.Response is null - - Fixed JsonReaderException in `GetContentstackError` when response is not valid JSON - - All exceptions now properly throw TaxonomyException (extends ContentstackException) with descriptive error messages + ### Version: 2.25.1 #### Date: Nov-10-2025 ##### Enh: - Improved Error messages +##### Fix: +- Taxonomy + - Fixed NullReferenceExceptions + - Fixed InvalidCastException in `GetContentstackError` when exception is not a WebException + - Fixed JsonReaderException in `GetContentstackError` when response is not valid JSON + - All exceptions now properly throw TaxonomyException (extends ContentstackException) with descriptive error messages ### Version: 2.25.0 #### Date: Jan-07-2025 diff --git a/Directory.Build.props b/Directory.Build.props index 0e81c8f..e15a9a2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 2.25.2 + 2.25.1