Skip to content

Commit a080634

Browse files
committed
Refactor error handling to utilize custom exceptions and improve error messaging consistency across various components.
1 parent 3e51f35 commit a080634

File tree

10 files changed

+146
-68
lines changed

10 files changed

+146
-68
lines changed

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("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig.");
139+
throw new InvalidOperationException(ErrorMessages.LivePreviewTokenMissing);
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 new GetContentstackError("Contentstack client request failed. Check your network settings or request parameters and try again: " + ex.Message, ex);
341+
throw new GetContentstackError(string.Format(ErrorMessages.ContentstackClientRequestError, ex.Message), ex);
342342
}
343343
}
344344

@@ -372,7 +372,7 @@ private async Task<JObject> GetLivePreviewData()
372372
}
373373
else
374374
{
375-
throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig.");
375+
throw new InvalidOperationException(ErrorMessages.LivePreviewTokenMissing);
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 new GetContentstackError("An error occurred while processing the Contentstack client request: " + ex.Message, ex);
876+
throw new GetContentstackError(string.Format(ErrorMessages.ContentstackSyncRequestError, ex.Message), ex);
877877
}
878878
}
879879
#endregion

Contentstack.Core/Internals/ContentstackConvert.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static Int32 ToInt32(object input)
1919
{
2020
if (e.Source != null)
2121
{
22-
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
22+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
2323
}
2424
}
2525

@@ -38,7 +38,7 @@ public static bool ToBoolean(object input)
3838
{
3939
if (e.Source != null)
4040
{
41-
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
41+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
4242
}
4343
}
4444

@@ -57,7 +57,7 @@ public static string ToString(object input, string defaultValue = "")
5757
{
5858
if (e.Source != null)
5959
{
60-
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
60+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
6161
}
6262
}
6363

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

@@ -95,7 +95,7 @@ public static decimal ToDecimal(object input)
9595
{
9696
if (e.Source != null)
9797
{
98-
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
98+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
9999
}
100100
}
101101

@@ -114,7 +114,7 @@ public static DateTime ToDateTime(object input)
114114
{
115115
if (e.Source != null)
116116
{
117-
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
117+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
118118
}
119119
}
120120

@@ -132,7 +132,7 @@ public static string ToISODate(object input)
132132
{
133133
if (e.Source != null)
134134
{
135-
Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
135+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
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("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace);
189+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
190190
}
191191
}
192192

Contentstack.Core/Internals/ContentstackExceptions.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,47 @@ public static EntryException CreateForProcessingError(Exception innerException)
149149
innerException);
150150
}
151151
}
152+
153+
/// <summary>
154+
/// Exception thrown when there are taxonomy-related errors
155+
/// </summary>
156+
public class TaxonomyException : ContentstackException
157+
{
158+
public TaxonomyException(string message) : base(message)
159+
{
160+
}
161+
162+
public TaxonomyException(string message, Exception innerException) : base(message, innerException)
163+
{
164+
}
165+
166+
public static TaxonomyException CreateForProcessingError(Exception innerException)
167+
{
168+
return new TaxonomyException(
169+
string.Format(ErrorMessages.TaxonomyProcessingError,
170+
ErrorMessages.FormatExceptionDetails(innerException)),
171+
innerException);
172+
}
173+
}
174+
175+
/// <summary>
176+
/// Exception thrown when there are content type-related errors
177+
/// </summary>
178+
public class ContentTypeException : ContentstackException
179+
{
180+
public ContentTypeException(string message) : base(message)
181+
{
182+
}
183+
184+
public ContentTypeException(string message, Exception innerException) : base(message, innerException)
185+
{
186+
}
187+
188+
public static ContentTypeException CreateForProcessingError(Exception innerException)
189+
{
190+
return new ContentTypeException(
191+
ErrorMessages.ContentTypeProcessingError,
192+
innerException);
193+
}
194+
}
152195
}

Contentstack.Core/Internals/ErrorMessages.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal static class ErrorMessages
1818

1919
// Entry related errors
2020
public const string EntryProcessingError = "An error occurred while processing the entry. {0}";
21+
public const string EntryUidRequired = "Please set entry uid.";
22+
public const string EntryNotFoundInCache = "Entry is not present in cache";
2123

2224
// Global Field related errors
2325
public const string GlobalFieldIdNullError = "GlobalFieldId required. This value cannot be null or empty, define it in your configuration.";
@@ -27,6 +29,39 @@ internal static class ErrorMessages
2729
// Live Preview related errors
2830
public const string LivePreviewTokenMissing = "Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig.";
2931

32+
// Client Request related errors
33+
public const string ContentstackClientRequestError = "Contentstack client request failed. Check your network settings or request parameters and try again: {0}";
34+
public const string ContentstackSyncRequestError = "An error occurred while processing the Contentstack client request: {0}";
35+
36+
// Taxonomy related errors
37+
public const string TaxonomyProcessingError = "An error occurred while processing the taxonomy operation: {0}";
38+
39+
// Content Type related errors
40+
public const string ContentTypeProcessingError = "Content type processing failed. Verify the schema and ensure all required fields are configured.";
41+
42+
// Authentication and Configuration errors
43+
public const string StackApiKeyRequired = "Stack api key can not be null.";
44+
public const string AccessTokenRequired = "Access token can not be null.";
45+
public const string EnvironmentRequired = "Environment can not be null.";
46+
public const string AuthenticationNotPresent = "Authentication Not present.";
47+
public const string ContentTypeNameRequired = "Please set contentType name.";
48+
49+
// JSON and Parsing errors
50+
public const string InvalidJsonFormat = "Please provide valid JSON.";
51+
public const string ParsingError = "Parsing Error.";
52+
53+
// Network and Server errors
54+
public const string NoConnectionError = "Connection error";
55+
public const string ServerError = "Server interaction went wrong, Please try again.";
56+
public const string NetworkUnavailable = "Network not available.";
57+
public const string DefaultError = "Oops! Something went wrong. Please try again.";
58+
59+
// Cache related errors
60+
public const string SavingNetworkCallResponseForCache = "Error while saving network call response.";
61+
62+
// Initialization errors
63+
public const string ContentstackDefaultMethodNotCalled = "You must called Contentstack.stack() first";
64+
3065
// Helper method to format exception details
3166
public static string FormatExceptionDetails(Exception ex)
3267
{

Contentstack.Core/Models/Asset.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public Asset IncludeMetadata()
252252
}
253253
catch (Exception e)
254254
{
255-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
255+
throw AssetException.CreateForProcessingError(e);
256256
}
257257
return this;
258258
}
@@ -322,7 +322,7 @@ public DateTime GetCreateAt()
322322
catch (Exception e)
323323
{
324324
if (e.Source != null)
325-
Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
325+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
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($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
365+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
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($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}");
387+
Console.WriteLine(ErrorMessages.FormatExceptionDetails(e));
388388
}
389389
return DateTime.MinValue;
390390
}
@@ -424,7 +424,7 @@ public async Task<Asset> Fetch()
424424
}
425425
catch (Exception ex)
426426
{
427-
throw new ContentstackException(string.Format(ErrorMessages.AssetProcessingError, ErrorMessages.FormatExceptionDetails(ex)));
427+
throw AssetException.CreateForProcessingError(ex);
428428
}
429429
}
430430

Contentstack.Core/Models/AssetLibrary.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public AssetLibrary Query(JObject QueryObject)
100100
}
101101
catch (Exception e)
102102
{
103-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
103+
throw AssetException.CreateForProcessingError(e);
104104
}
105105
return this;
106106
}
@@ -124,7 +124,7 @@ public AssetLibrary IncludeFallback()
124124
}
125125
catch (Exception e)
126126
{
127-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
127+
throw AssetException.CreateForProcessingError(e);
128128
}
129129
return this;
130130
}
@@ -149,7 +149,7 @@ public AssetLibrary IncludeBranch()
149149
}
150150
catch (Exception e)
151151
{
152-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
152+
throw AssetException.CreateForProcessingError(e);
153153
}
154154
return this;
155155
}
@@ -241,7 +241,7 @@ public AssetLibrary IncludeMetadata()
241241
}
242242
catch (Exception e)
243243
{
244-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
244+
throw AssetException.CreateForProcessingError(e);
245245
}
246246
return this;
247247
}
@@ -285,7 +285,7 @@ public AssetLibrary Skip(int number)
285285
}
286286
catch (Exception e)
287287
{
288-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
288+
throw AssetException.CreateForProcessingError(e);
289289
}
290290
return this;
291291
}
@@ -311,7 +311,7 @@ public AssetLibrary Limit(int number)
311311
}
312312
catch (Exception e)
313313
{
314-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
314+
throw AssetException.CreateForProcessingError(e);
315315
}
316316
return this;
317317
}
@@ -340,7 +340,7 @@ public AssetLibrary Tags(string[] tags)
340340
}
341341
catch (Exception e)
342342
{
343-
throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e);
343+
throw AssetException.CreateForProcessingError(e);
344344
}
345345
return this;
346346
}
@@ -369,7 +369,7 @@ public AssetLibrary Only(String[] fieldUid)
369369
}
370370
catch (Exception e)
371371
{
372-
Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}");
372+
throw AssetException.CreateForProcessingError(e);
373373
}
374374

375375
return this;
@@ -401,7 +401,7 @@ public AssetLibrary Except(String[] fieldUids)
401401
}
402402
catch (Exception e)
403403
{
404-
Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}");
404+
throw AssetException.CreateForProcessingError(e);
405405
}
406406
return this;
407407
}

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 new ContentstackException("Content type processing failed. Verify the schema and ensure all required fields are configured.", ex);
179+
throw ContentTypeException.CreateForProcessingError(ex);
180180
}
181181
}
182182

0 commit comments

Comments
 (0)