@@ -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
0 commit comments