Skip to content

Commit fc4eae2

Browse files
committed
Enhance error handling and messaging across multiple files.
1 parent b1b268e commit fc4eae2

11 files changed

Lines changed: 82 additions & 82 deletions

File tree

Contentstack.Core/ContentstackClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
136136
}
137137
else
138138
{
139-
throw new InvalidOperationException("Add PreviewToken or ManagementToken in LivePreviewConfig");
139+
throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig.");
140140
}
141141
}
142142
this.SerializerSettings.DateParseHandling = DateParseHandling.None;
@@ -338,7 +338,7 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
338338
}
339339
catch (Exception ex)
340340
{
341-
throw GetContentstackError(ex);
341+
throw new GetContentstackError("Contentstack client request failed. Check your network settings or request parameters and try again: " + ex.Message, ex);
342342
}
343343
}
344344

@@ -372,7 +372,7 @@ private async Task<JObject> GetLivePreviewData()
372372
}
373373
else
374374
{
375-
throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig");
375+
throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig.");
376376
}
377377

378378
if (!string.IsNullOrEmpty(this.LivePreviewConfig.ReleaseId))
@@ -873,7 +873,7 @@ private async Task<SyncStack> GetResultAsync(string Init = "false", SyncType Syn
873873
}
874874
catch (Exception ex)
875875
{
876-
throw GetContentstackError(ex);
876+
throw new GetContentstackError("An error occurred while processing the Contentstack client request: " + ex.Message, ex);
877877
}
878878
}
879879
#endregion

Contentstack.Core/Internals/AssetJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override Asset ReadJson(JsonReader reader, Type objectType, Asset existin
2020

2121
public override void WriteJson(JsonWriter writer, Asset value, JsonSerializer serializer)
2222
{
23-
throw new NotImplementedException();
23+
throw new NotImplementedException("Failed to convert asset JSON. Please check the asset format and data integrity.");
2424
}
2525
}
2626
}

Contentstack.Core/Internals/ContentstackConvert.cs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public static Int32 ToInt32(object input)
1515
{
1616
output = Convert.ToInt32(input);
1717
}
18-
catch (Exception e)
19-
{
20-
if (e.Source != null)
21-
{
22-
Console.WriteLine("IOException source: {0}", e.Source);
23-
}
18+
catch (Exception e)
19+
{
20+
if (e.Source != null)
21+
{
22+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
23+
}
2424
}
2525

2626
return output;
@@ -34,12 +34,12 @@ public static bool ToBoolean(object input)
3434
{
3535
output = Convert.ToBoolean(input);
3636
}
37-
catch (Exception e)
38-
{
39-
if (e.Source != null)
40-
{
41-
Console.WriteLine("IOException source: {0}", e.Source);
42-
}
37+
catch (Exception e)
38+
{
39+
if (e.Source != null)
40+
{
41+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
42+
}
4343
}
4444

4545
return output;
@@ -52,13 +52,13 @@ public static string ToString(object input, string defaultValue = "")
5252
try
5353
{
5454
output = Convert.ToString(input);
55-
}
56-
catch (Exception e)
57-
{
58-
if (e.Source != null)
59-
{
60-
Console.WriteLine("IOException source: {0}", e.Source);
61-
}
55+
}
56+
catch (Exception e)
57+
{
58+
if (e.Source != null)
59+
{
60+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
61+
}
6262
}
6363

6464
return output;
@@ -76,7 +76,7 @@ public static double ToDouble(object input)
7676
{
7777
if (e.Source != null)
7878
{
79-
Console.WriteLine("IOException source: {0}", e.Source);
79+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
8080
}
8181
}
8282

@@ -90,15 +90,15 @@ public static decimal ToDecimal(object input)
9090
try
9191
{
9292
output = Convert.ToDecimal(input);
93-
}
94-
catch (Exception e)
95-
{
96-
if (e.Source != null)
97-
{
98-
Console.WriteLine("IOException source: {0}", e.Source);
99-
}
100-
}
101-
93+
}
94+
catch (Exception e)
95+
{
96+
if (e.Source != null)
97+
{
98+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
99+
}
100+
}
101+
102102
return output;
103103
}
104104

@@ -109,20 +109,20 @@ public static DateTime ToDateTime(object input)
109109
try
110110
{
111111
output = DateTime.Parse(ContentstackConvert.ToString(input));
112-
}
113-
catch (Exception e)
114-
{
115-
if (e.Source != null)
116-
{
117-
Console.WriteLine("IOException source: {0}", e.Source);
118-
}
119-
}
120-
112+
}
113+
catch (Exception e)
114+
{
115+
if (e.Source != null)
116+
{
117+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
118+
}
119+
}
120+
121121
return output;
122122
}
123123

124124
public static string ToISODate(object input)
125-
{
125+
{
126126
DateTime now = DateTime.Now;
127127
try
128128
{
@@ -132,7 +132,7 @@ public static string ToISODate(object input)
132132
{
133133
if (e.Source != null)
134134
{
135-
Console.WriteLine("IOException source: {0}", e.Source);
135+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
136136
}
137137
}
138138
return now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'sszzz");
@@ -186,7 +186,7 @@ public static object GetValue(string value)
186186
{
187187
if (e.Source != null)
188188
{
189-
Console.WriteLine("IOException source: {0}", e.Source);
189+
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
190190
}
191191
}
192192

@@ -222,8 +222,8 @@ public static RegexOptions GetRegexOptions(string option)
222222
return RegexOptions.None;
223223
}
224224
}
225-
}
226-
225+
}
226+
227227
public static Stream GenerateStreamFromString(string s)
228228
{
229229
MemoryStream stream = new MemoryStream();
@@ -232,7 +232,7 @@ public static Stream GenerateStreamFromString(string s)
232232
writer.Flush();
233233
stream.Position = 0;
234234
return stream;
235-
}
235+
}
236236

237237
#endregion
238238

Contentstack.Core/Internals/StackOutput.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,20 @@ internal class StackOutput
218218
// }
219219
// }
220220

221-
// if (output.ContainsKey("asset"))
222-
// {
223-
// if (output["asset"].GetType() == typeof(string))
224-
// {
225-
// this._Object = output["asset"];
226-
// }
227-
// else
228-
// {
229-
// this._Object = output["asset"];
230-
// //var tmp = this._Objects as object[];
231-
// //if (tmp != null)
232-
// // this._Object = tmp.FirstOrDefault();
233-
// }
234-
// }
221+
// if (output.ContainsKey("asset"))
222+
// {
223+
// if (output["asset"].GetType() == typeof(string))
224+
// {
225+
// this._Object = output["asset"];
226+
// }
227+
// else
228+
// {
229+
// this._Object = output["asset"];
230+
// //var tmp = this._Objects as object[];
231+
// //if (tmp != null)
232+
// // this._Object = tmp.FirstOrDefault();
233+
// }
234+
// }
235235
// if (output.ContainsKey("assets"))
236236
// {
237237
// if (output["assets"].GetType() == typeof(string))
@@ -305,7 +305,7 @@ internal class StackOutput
305305
// }
306306
// catch (Exception ex)
307307
// {
308-
// throw new ContentstackError(ex);
308+
// throw new ContentstackError($"Failed to generate stack output: {ex.Message}\nStackTrace: {ex.StackTrace}");
309309
// }
310310
// }
311311
// public Dictionary<string, object> GetObjectDict(JContainer container)

Contentstack.Core/Models/Asset.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public object this[string key]
6464
{
6565
if (e.Source != null)
6666
{
67-
Console.WriteLine("IOException source: {0}", e.Source);
67+
Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
6868
}
6969
}
7070
}
@@ -322,7 +322,7 @@ public DateTime GetCreateAt()
322322
catch (Exception e)
323323
{
324324
if (e.Source != null)
325-
Console.WriteLine("IOException source: {0}", e.Source);
325+
Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
326326
}
327327
return DateTime.MinValue;
328328
}
@@ -362,7 +362,7 @@ public DateTime GetUpdateAt()
362362
catch (Exception e)
363363
{
364364
if (e.Source != null)
365-
Console.WriteLine("IOException source: {0}", e.Source);
365+
Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
366366
}
367367
return DateTime.MinValue;
368368
}
@@ -384,7 +384,7 @@ public DateTime GetDeleteAt()
384384
catch (Exception e)
385385
{
386386
if (e.Source != null)
387-
Console.WriteLine("IOException source: {0}", e.Source);
387+
Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
388388
}
389389
return DateTime.MinValue;
390390
}
@@ -424,7 +424,7 @@ public async Task<Asset> Fetch()
424424
}
425425
catch (Exception ex)
426426
{
427-
throw GetContentstackError(ex);
427+
throw new ContentstackException("Unable to process asset. Ensure the file format, size, and encoding meet the required standards.", ex);
428428
}
429429
}
430430

Contentstack.Core/Models/AssetLibrary.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public AssetLibrary Only(String[] fieldUid)
369369
}
370370
catch (Exception e)
371371
{
372-
Console.WriteLine("IOException source: {0}", e.Source);
372+
Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}");
373373
}
374374

375375
return this;
@@ -401,7 +401,7 @@ public AssetLibrary Except(String[] fieldUids)
401401
}
402402
catch (Exception e)
403403
{
404-
Console.WriteLine("IOException source: {0}", e.Source);
404+
Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}");
405405
}
406406
return this;
407407
}
@@ -508,7 +508,7 @@ private async Task<JObject> Exec()
508508
}
509509
catch (Exception ex)
510510
{
511-
throw GetContentstackError(ex);
511+
throw new ContentstackException($"Exception in {ex.GetType().Name}: {ex.Message}\nStackTrace: {ex.StackTrace}");
512512
}
513513
}
514514
#endregion

Contentstack.Core/Models/ContentType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public async Task<JObject> Fetch(Dictionary<string, object> param = null)
176176
}
177177
catch (Exception ex)
178178
{
179-
throw GetContentstackError(ex);
179+
throw new ContentstackException("Content type processing failed. Verify the schema and ensure all required fields are configured.", ex);
180180
}
181181
}
182182

Contentstack.Core/Models/Entry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ public async Task<T> Fetch<T>()
14051405
} else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) {
14061406
headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken;
14071407
} else {
1408-
throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig");
1408+
throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig.");
14091409
}
14101410

14111411
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId))
@@ -1446,7 +1446,7 @@ public async Task<T> Fetch<T>()
14461446
}
14471447
catch (Exception ex)
14481448
{
1449-
throw GetContentstackError(ex);
1449+
throw new ContentstackException("An error occurred while processing the entry "+ $"Exception: {ex.Message}\nSource: {ex.Source ?? "Unknown"}\nStackTrace: {ex.StackTrace ?? "No stack trace available"}");
14501450
}
14511451
}
14521452

Contentstack.Core/Models/GlobalField.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public string GlobalFieldId
3333
set
3434
{
3535
if (String.IsNullOrEmpty(value))
36-
throw new ArgumentNullException("GlobalFieldId cannot be null or empty.");
36+
throw new ArgumentNullException("GlobalFieldId required. This value cannot be null or empty, define it in your configuration.");
3737
this.Uid = value;
3838
}
3939
}
@@ -189,7 +189,7 @@ public async Task<JObject> Fetch(Dictionary<string, object> param = null)
189189
}
190190
catch (Exception ex)
191191
{
192-
throw GetContentstackError(ex);
192+
throw new ContentstackException("An error occurred while processing the globalField. "+$"Exception: {ex.Message}\nSource: {ex.Source ?? "Unknown"}\nStackTrace: {ex.StackTrace ?? "No stack trace available"}");
193193
}
194194
}
195195

Contentstack.Core/Models/GlobalFieldQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public async Task<JObject> Find(Dictionary<string, object> param = null)
157157
}
158158
catch (Exception ex)
159159
{
160-
throw GetContentstackError(ex);
160+
throw new ContentstackException("Global field query failed. Check your query syntax and field schema before retrying.", ex);
161161
}
162162
}
163163

0 commit comments

Comments
 (0)