From fc4eae21a5587568c28ef50cafa28ff96d45838c Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Sun, 31 Aug 2025 14:08:36 +0530 Subject: [PATCH 1/3] Enhance error handling and messaging across multiple files. --- Contentstack.Core/ContentstackClient.cs | 8 +- .../Internals/AssetJsonConverter.cs | 2 +- .../Internals/ContentstackConvert.cs | 88 +++++++++---------- Contentstack.Core/Internals/StackOutput.cs | 30 +++---- Contentstack.Core/Models/Asset.cs | 10 +-- Contentstack.Core/Models/AssetLibrary.cs | 6 +- Contentstack.Core/Models/ContentType.cs | 2 +- Contentstack.Core/Models/Entry.cs | 4 +- Contentstack.Core/Models/GlobalField.cs | 4 +- Contentstack.Core/Models/GlobalFieldQuery.cs | 2 +- Contentstack.Core/Models/Query.cs | 8 +- 11 files changed, 82 insertions(+), 82 deletions(-) diff --git a/Contentstack.Core/ContentstackClient.cs b/Contentstack.Core/ContentstackClient.cs index 69dd881..172bca3 100644 --- a/Contentstack.Core/ContentstackClient.cs +++ b/Contentstack.Core/ContentstackClient.cs @@ -136,7 +136,7 @@ public ContentstackClient(IOptions options) } else { - throw new InvalidOperationException("Add PreviewToken or ManagementToken in LivePreviewConfig"); + throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); } } this.SerializerSettings.DateParseHandling = DateParseHandling.None; @@ -338,7 +338,7 @@ public async Task GetContentTypes(Dictionary param = null } catch (Exception ex) { - throw GetContentstackError(ex); + throw new GetContentstackError("Contentstack client request failed. Check your network settings or request parameters and try again: " + ex.Message, ex); } } @@ -372,7 +372,7 @@ private async Task GetLivePreviewData() } else { - throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig"); + throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); } if (!string.IsNullOrEmpty(this.LivePreviewConfig.ReleaseId)) @@ -873,7 +873,7 @@ private async Task GetResultAsync(string Init = "false", SyncType Syn } catch (Exception ex) { - throw GetContentstackError(ex); + throw new GetContentstackError("An error occurred while processing the Contentstack client request: " + ex.Message, ex); } } #endregion diff --git a/Contentstack.Core/Internals/AssetJsonConverter.cs b/Contentstack.Core/Internals/AssetJsonConverter.cs index 9eee45a..7a38008 100644 --- a/Contentstack.Core/Internals/AssetJsonConverter.cs +++ b/Contentstack.Core/Internals/AssetJsonConverter.cs @@ -20,7 +20,7 @@ public override Asset ReadJson(JsonReader reader, Type objectType, Asset existin public override void WriteJson(JsonWriter writer, Asset value, JsonSerializer serializer) { - throw new NotImplementedException(); + throw new NotImplementedException("Failed to convert asset JSON. Please check the asset format and data integrity."); } } } diff --git a/Contentstack.Core/Internals/ContentstackConvert.cs b/Contentstack.Core/Internals/ContentstackConvert.cs index a35a235..6adca75 100644 --- a/Contentstack.Core/Internals/ContentstackConvert.cs +++ b/Contentstack.Core/Internals/ContentstackConvert.cs @@ -15,12 +15,12 @@ public static Int32 ToInt32(object input) { output = Convert.ToInt32(input); } - catch (Exception e) - { - if (e.Source != null) - { - Console.WriteLine("IOException source: {0}", e.Source); - } + catch (Exception e) + { + if (e.Source != null) + { + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + } } return output; @@ -34,12 +34,12 @@ public static bool ToBoolean(object input) { output = Convert.ToBoolean(input); } - catch (Exception e) - { - if (e.Source != null) - { - Console.WriteLine("IOException source: {0}", e.Source); - } + catch (Exception e) + { + if (e.Source != null) + { + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + } } return output; @@ -52,13 +52,13 @@ public static string ToString(object input, string defaultValue = "") try { output = Convert.ToString(input); - } - catch (Exception e) - { - if (e.Source != null) - { - Console.WriteLine("IOException source: {0}", e.Source); - } + } + catch (Exception e) + { + if (e.Source != null) + { + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + } } return output; @@ -76,7 +76,7 @@ public static double ToDouble(object input) { if (e.Source != null) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); } } @@ -90,15 +90,15 @@ public static decimal ToDecimal(object input) try { output = Convert.ToDecimal(input); - } - catch (Exception e) - { - if (e.Source != null) - { - Console.WriteLine("IOException source: {0}", e.Source); - } - } - + } + catch (Exception e) + { + if (e.Source != null) + { + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + } + } + return output; } @@ -109,20 +109,20 @@ public static DateTime ToDateTime(object input) try { output = DateTime.Parse(ContentstackConvert.ToString(input)); - } - catch (Exception e) - { - if (e.Source != null) - { - Console.WriteLine("IOException source: {0}", e.Source); - } - } - + } + catch (Exception e) + { + if (e.Source != null) + { + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + } + } + return output; } public static string ToISODate(object input) - { + { DateTime now = DateTime.Now; try { @@ -132,7 +132,7 @@ public static string ToISODate(object input) { if (e.Source != null) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); } } return now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'sszzz"); @@ -186,7 +186,7 @@ public static object GetValue(string value) { if (e.Source != null) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); } } @@ -222,8 +222,8 @@ public static RegexOptions GetRegexOptions(string option) return RegexOptions.None; } } - } - + } + public static Stream GenerateStreamFromString(string s) { MemoryStream stream = new MemoryStream(); @@ -232,7 +232,7 @@ public static Stream GenerateStreamFromString(string s) writer.Flush(); stream.Position = 0; return stream; - } + } #endregion diff --git a/Contentstack.Core/Internals/StackOutput.cs b/Contentstack.Core/Internals/StackOutput.cs index 0b20489..235acc1 100644 --- a/Contentstack.Core/Internals/StackOutput.cs +++ b/Contentstack.Core/Internals/StackOutput.cs @@ -218,20 +218,20 @@ internal class StackOutput // } // } - // if (output.ContainsKey("asset")) - // { - // if (output["asset"].GetType() == typeof(string)) - // { - // this._Object = output["asset"]; - // } - // else - // { - // this._Object = output["asset"]; - // //var tmp = this._Objects as object[]; - // //if (tmp != null) - // // this._Object = tmp.FirstOrDefault(); - // } - // } + // if (output.ContainsKey("asset")) + // { + // if (output["asset"].GetType() == typeof(string)) + // { + // this._Object = output["asset"]; + // } + // else + // { + // this._Object = output["asset"]; + // //var tmp = this._Objects as object[]; + // //if (tmp != null) + // // this._Object = tmp.FirstOrDefault(); + // } + // } // if (output.ContainsKey("assets")) // { // if (output["assets"].GetType() == typeof(string)) @@ -305,7 +305,7 @@ internal class StackOutput // } // catch (Exception ex) // { - // throw new ContentstackError(ex); + // throw new ContentstackError($"Failed to generate stack output: {ex.Message}\nStackTrace: {ex.StackTrace}"); // } // } // public Dictionary GetObjectDict(JContainer container) diff --git a/Contentstack.Core/Models/Asset.cs b/Contentstack.Core/Models/Asset.cs index dcc07e9..74b0ed0 100644 --- a/Contentstack.Core/Models/Asset.cs +++ b/Contentstack.Core/Models/Asset.cs @@ -64,7 +64,7 @@ public object this[string key] { if (e.Source != null) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); } } } @@ -322,7 +322,7 @@ public DateTime GetCreateAt() catch (Exception e) { if (e.Source != null) - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); } return DateTime.MinValue; } @@ -362,7 +362,7 @@ public DateTime GetUpdateAt() catch (Exception e) { if (e.Source != null) - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); } return DateTime.MinValue; } @@ -384,7 +384,7 @@ public DateTime GetDeleteAt() catch (Exception e) { if (e.Source != null) - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); } return DateTime.MinValue; } @@ -424,7 +424,7 @@ public async Task Fetch() } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException("Unable to process asset. Ensure the file format, size, and encoding meet the required standards.", ex); } } diff --git a/Contentstack.Core/Models/AssetLibrary.cs b/Contentstack.Core/Models/AssetLibrary.cs index 8ec17e8..33da842 100644 --- a/Contentstack.Core/Models/AssetLibrary.cs +++ b/Contentstack.Core/Models/AssetLibrary.cs @@ -369,7 +369,7 @@ public AssetLibrary Only(String[] fieldUid) } catch (Exception e) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); } return this; @@ -401,7 +401,7 @@ public AssetLibrary Except(String[] fieldUids) } catch (Exception e) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); } return this; } @@ -508,7 +508,7 @@ private async Task Exec() } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException($"Exception in {ex.GetType().Name}: {ex.Message}\nStackTrace: {ex.StackTrace}"); } } #endregion diff --git a/Contentstack.Core/Models/ContentType.cs b/Contentstack.Core/Models/ContentType.cs index bfd47a7..c58eede 100644 --- a/Contentstack.Core/Models/ContentType.cs +++ b/Contentstack.Core/Models/ContentType.cs @@ -176,7 +176,7 @@ public async Task Fetch(Dictionary param = null) } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException("Content type processing failed. Verify the schema and ensure all required fields are configured.", ex); } } diff --git a/Contentstack.Core/Models/Entry.cs b/Contentstack.Core/Models/Entry.cs index 1551a4b..96ad72c 100644 --- a/Contentstack.Core/Models/Entry.cs +++ b/Contentstack.Core/Models/Entry.cs @@ -1405,7 +1405,7 @@ public async Task Fetch() } else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) { headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken; } else { - throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig"); + throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); } if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId)) @@ -1446,7 +1446,7 @@ public async Task Fetch() } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException("An error occurred while processing the entry "+ $"Exception: {ex.Message}\nSource: {ex.Source ?? "Unknown"}\nStackTrace: {ex.StackTrace ?? "No stack trace available"}"); } } diff --git a/Contentstack.Core/Models/GlobalField.cs b/Contentstack.Core/Models/GlobalField.cs index 3d8efd2..f53ac47 100644 --- a/Contentstack.Core/Models/GlobalField.cs +++ b/Contentstack.Core/Models/GlobalField.cs @@ -33,7 +33,7 @@ public string GlobalFieldId set { if (String.IsNullOrEmpty(value)) - throw new ArgumentNullException("GlobalFieldId cannot be null or empty."); + throw new ArgumentNullException("GlobalFieldId required. This value cannot be null or empty, define it in your configuration."); this.Uid = value; } } @@ -189,7 +189,7 @@ public async Task Fetch(Dictionary param = null) } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException("An error occurred while processing the globalField. "+$"Exception: {ex.Message}\nSource: {ex.Source ?? "Unknown"}\nStackTrace: {ex.StackTrace ?? "No stack trace available"}"); } } diff --git a/Contentstack.Core/Models/GlobalFieldQuery.cs b/Contentstack.Core/Models/GlobalFieldQuery.cs index 270af47..2645b48 100644 --- a/Contentstack.Core/Models/GlobalFieldQuery.cs +++ b/Contentstack.Core/Models/GlobalFieldQuery.cs @@ -157,7 +157,7 @@ public async Task Find(Dictionary param = null) } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException("Global field query failed. Check your query syntax and field schema before retrying.", ex); } } diff --git a/Contentstack.Core/Models/Query.cs b/Contentstack.Core/Models/Query.cs index c50a09e..58ba58d 100644 --- a/Contentstack.Core/Models/Query.cs +++ b/Contentstack.Core/Models/Query.cs @@ -1247,7 +1247,7 @@ public Query Only(String[] fieldUid) } catch (Exception e) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); } return this; @@ -1316,7 +1316,7 @@ public Query Except(String[] fieldUids) } catch (Exception e) { - Console.WriteLine("IOException source: {0}", e.Source); + Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); } return this; } @@ -1864,7 +1864,7 @@ private async Task Exec() } else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) { headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken; } else { - throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig"); + throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); } if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId)) @@ -1948,7 +1948,7 @@ private async Task Exec() } catch (Exception ex) { - throw GetContentstackError(ex); + throw new ContentstackException("Query failed to execute. Review your syntax and filters, then try again.", ex); } } From 3e51f35ff85b9a5e9611b11fa53e986cbe19575c Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 2 Sep 2025 10:53:44 +0530 Subject: [PATCH 2/3] Refactor error handling in asset and entry processing to use custom exceptions for improved clarity and maintainability. --- .../Internals/AssetJsonConverter.cs | 2 +- .../Internals/ContentstackExceptions.cs | 152 ++++++++++++++++++ Contentstack.Core/Internals/ErrorMessages.cs | 39 +++++ Contentstack.Core/Models/Asset.cs | 4 +- Contentstack.Core/Models/AssetLibrary.cs | 2 +- Contentstack.Core/Models/Entry.cs | 4 +- Contentstack.Core/Models/GlobalField.cs | 4 +- Contentstack.Core/Models/GlobalFieldQuery.cs | 2 +- Contentstack.Core/Models/Query.cs | 82 +++++----- 9 files changed, 241 insertions(+), 50 deletions(-) create mode 100644 Contentstack.Core/Internals/ContentstackExceptions.cs create mode 100644 Contentstack.Core/Internals/ErrorMessages.cs diff --git a/Contentstack.Core/Internals/AssetJsonConverter.cs b/Contentstack.Core/Internals/AssetJsonConverter.cs index 7a38008..677682d 100644 --- a/Contentstack.Core/Internals/AssetJsonConverter.cs +++ b/Contentstack.Core/Internals/AssetJsonConverter.cs @@ -20,7 +20,7 @@ public override Asset ReadJson(JsonReader reader, Type objectType, Asset existin public override void WriteJson(JsonWriter writer, Asset value, JsonSerializer serializer) { - throw new NotImplementedException("Failed to convert asset JSON. Please check the asset format and data integrity."); + throw AssetException.CreateForJsonConversionError(); } } } diff --git a/Contentstack.Core/Internals/ContentstackExceptions.cs b/Contentstack.Core/Internals/ContentstackExceptions.cs new file mode 100644 index 0000000..0d94a2e --- /dev/null +++ b/Contentstack.Core/Internals/ContentstackExceptions.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Net; + +namespace Contentstack.Core.Internals +{ + /// + /// Base exception class for all Contentstack exceptions + /// + public class ContentstackException : Exception + { + public int ErrorCode { get; set; } + public HttpStatusCode StatusCode { get; set; } + public Dictionary Errors { get; set; } + + public ContentstackException() : base() + { + } + + public ContentstackException(string message) : base(message) + { + } + + public ContentstackException(string message, Exception innerException) : base(message, innerException) + { + } + + public ContentstackException(Exception ex) : base(ex.Message, ex) + { + } + } + + /// + /// Exception thrown when there are issues with query filters or parameters + /// + public class QueryFilterException : ContentstackException + { + public QueryFilterException() : base(ErrorMessages.QueryFilterError) + { + } + + public QueryFilterException(string message) : base(message) + { + } + + public QueryFilterException(string message, Exception innerException) : base(message, innerException) + { + } + + public static QueryFilterException Create(Exception innerException = null) + { + return new QueryFilterException( + string.Format(ErrorMessages.InvalidParamsError, + innerException?.Message ?? ErrorMessages.QueryFilterError), + innerException); + } + } + + /// + /// Exception thrown when there are asset-related errors + /// + public class AssetException : ContentstackException + { + public AssetException(string message) : base(message) + { + } + + public AssetException(string message, Exception innerException) : base(message, innerException) + { + } + + public static AssetException CreateForJsonConversionError() + { + return new AssetException(ErrorMessages.AssetJsonConversionError); + } + + public static AssetException CreateForProcessingError(Exception innerException) + { + return new AssetException( + string.Format(ErrorMessages.AssetProcessingError, + ErrorMessages.FormatExceptionDetails(innerException)), + innerException); + } + } + + /// + /// Exception thrown when there are live preview-related errors + /// + public class LivePreviewException : ContentstackException + { + public LivePreviewException() : base(ErrorMessages.LivePreviewTokenMissing) + { + } + + public LivePreviewException(string message) : base(message) + { + } + + public LivePreviewException(string message, Exception innerException) : base(message, innerException) + { + } + } + + /// + /// Exception thrown when there are global field-related errors + /// + public class GlobalFieldException : ContentstackException + { + public GlobalFieldException(string message) : base(message) + { + } + + public GlobalFieldException(string message, Exception innerException) : base(message, innerException) + { + } + + public static GlobalFieldException CreateForProcessingError(Exception innerException) + { + return new GlobalFieldException( + string.Format(ErrorMessages.GlobalFieldProcessingError, + ErrorMessages.FormatExceptionDetails(innerException)), + innerException); + } + + public static GlobalFieldException CreateForIdNull() + { + return new GlobalFieldException(ErrorMessages.GlobalFieldIdNullError); + } + } + + /// + /// Exception thrown when there are entry-related errors + /// + public class EntryException : ContentstackException + { + public EntryException(string message) : base(message) + { + } + + public EntryException(string message, Exception innerException) : base(message, innerException) + { + } + + public static EntryException CreateForProcessingError(Exception innerException) + { + return new EntryException( + string.Format(ErrorMessages.EntryProcessingError, + ErrorMessages.FormatExceptionDetails(innerException)), + innerException); + } + } +} \ No newline at end of file diff --git a/Contentstack.Core/Internals/ErrorMessages.cs b/Contentstack.Core/Internals/ErrorMessages.cs new file mode 100644 index 0000000..5efa956 --- /dev/null +++ b/Contentstack.Core/Internals/ErrorMessages.cs @@ -0,0 +1,39 @@ +using System; + +namespace Contentstack.Core.Internals +{ + /// + /// Contains all error message constants used throughout the SDK + /// + internal static class ErrorMessages + { + // Query and Filter related errors + public const string QueryFilterError = "Please provide valid params."; + public const string InvalidParamsError = "Invalid parameters provided. {0}"; + + // Asset related errors + public const string AssetJsonConversionError = "Failed to convert asset JSON. Please check the asset format and data integrity."; + public const string AssetProcessingError = "An error occurred while processing the asset. {0}"; + public const string AssetLibraryRequestError = "Exception in {0}: {1}\nStackTrace: {2}"; + + // Entry related errors + public const string EntryProcessingError = "An error occurred while processing the entry. {0}"; + + // Global Field related errors + public const string GlobalFieldIdNullError = "GlobalFieldId required. This value cannot be null or empty, define it in your configuration."; + public const string GlobalFieldProcessingError = "An error occurred while processing the globalField. {0}"; + public const string GlobalFieldQueryError = "Global field query failed. Check your query syntax and field schema before retrying."; + + // Live Preview related errors + public const string LivePreviewTokenMissing = "Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."; + + // Helper method to format exception details + public static string FormatExceptionDetails(Exception ex) + { + return string.Format("Exception: {0}\nSource: {1}\nStackTrace: {2}", + ex.Message, + ex.Source ?? "Unknown", + ex.StackTrace ?? "No stack trace available"); + } + } +} \ No newline at end of file diff --git a/Contentstack.Core/Models/Asset.cs b/Contentstack.Core/Models/Asset.cs index 74b0ed0..afd3bae 100644 --- a/Contentstack.Core/Models/Asset.cs +++ b/Contentstack.Core/Models/Asset.cs @@ -64,7 +64,7 @@ public object this[string key] { if (e.Source != null) { - Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } } @@ -424,7 +424,7 @@ public async Task Fetch() } catch (Exception ex) { - throw new ContentstackException("Unable to process asset. Ensure the file format, size, and encoding meet the required standards.", ex); + throw new ContentstackException(string.Format(ErrorMessages.AssetProcessingError, ErrorMessages.FormatExceptionDetails(ex))); } } diff --git a/Contentstack.Core/Models/AssetLibrary.cs b/Contentstack.Core/Models/AssetLibrary.cs index 33da842..a6c0314 100644 --- a/Contentstack.Core/Models/AssetLibrary.cs +++ b/Contentstack.Core/Models/AssetLibrary.cs @@ -508,7 +508,7 @@ private async Task Exec() } catch (Exception ex) { - throw new ContentstackException($"Exception in {ex.GetType().Name}: {ex.Message}\nStackTrace: {ex.StackTrace}"); + throw AssetException.CreateForProcessingError(ex); } } #endregion diff --git a/Contentstack.Core/Models/Entry.cs b/Contentstack.Core/Models/Entry.cs index 96ad72c..653636e 100644 --- a/Contentstack.Core/Models/Entry.cs +++ b/Contentstack.Core/Models/Entry.cs @@ -1405,7 +1405,7 @@ public async Task Fetch() } else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) { headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken; } else { - throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); + throw new LivePreviewException(); } if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId)) @@ -1446,7 +1446,7 @@ public async Task Fetch() } catch (Exception ex) { - throw new ContentstackException("An error occurred while processing the entry "+ $"Exception: {ex.Message}\nSource: {ex.Source ?? "Unknown"}\nStackTrace: {ex.StackTrace ?? "No stack trace available"}"); + throw EntryException.CreateForProcessingError(ex); } } diff --git a/Contentstack.Core/Models/GlobalField.cs b/Contentstack.Core/Models/GlobalField.cs index f53ac47..995cc4f 100644 --- a/Contentstack.Core/Models/GlobalField.cs +++ b/Contentstack.Core/Models/GlobalField.cs @@ -33,7 +33,7 @@ public string GlobalFieldId set { if (String.IsNullOrEmpty(value)) - throw new ArgumentNullException("GlobalFieldId required. This value cannot be null or empty, define it in your configuration."); + throw GlobalFieldException.CreateForIdNull(); this.Uid = value; } } @@ -189,7 +189,7 @@ public async Task Fetch(Dictionary param = null) } catch (Exception ex) { - throw new ContentstackException("An error occurred while processing the globalField. "+$"Exception: {ex.Message}\nSource: {ex.Source ?? "Unknown"}\nStackTrace: {ex.StackTrace ?? "No stack trace available"}"); + throw GlobalFieldException.CreateForProcessingError(ex); } } diff --git a/Contentstack.Core/Models/GlobalFieldQuery.cs b/Contentstack.Core/Models/GlobalFieldQuery.cs index 2645b48..b71cdc2 100644 --- a/Contentstack.Core/Models/GlobalFieldQuery.cs +++ b/Contentstack.Core/Models/GlobalFieldQuery.cs @@ -157,7 +157,7 @@ public async Task Find(Dictionary param = null) } catch (Exception ex) { - throw new ContentstackException("Global field query failed. Check your query syntax and field schema before retrying.", ex); + throw new ContentstackException(ErrorMessages.GlobalFieldQueryError, ex); } } diff --git a/Contentstack.Core/Models/Query.cs b/Contentstack.Core/Models/Query.cs index 58ba58d..98346bf 100644 --- a/Contentstack.Core/Models/Query.cs +++ b/Contentstack.Core/Models/Query.cs @@ -183,7 +183,7 @@ public Query SetLanguage(Language language) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } @@ -284,12 +284,12 @@ public Query Where(String key, Object value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw new QueryFilterException(); } } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; @@ -307,12 +307,12 @@ public Query ReferenceIn(String key, Query query) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw new QueryFilterException(); } } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; @@ -330,12 +330,12 @@ public Query ReferenceNotIn(String key, Query query) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw new QueryFilterException(); } } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; @@ -367,12 +367,12 @@ public Query AddQuery(String key, String value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw new QueryFilterException(); } } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -403,7 +403,7 @@ public Query RemoveQuery(String key) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -461,7 +461,7 @@ public Query And(List queryObjects) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -527,7 +527,7 @@ public Query Or(List queryObjects) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -571,7 +571,7 @@ public Query LessThan(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -615,7 +615,7 @@ public Query LessThanOrEqualTo(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -658,7 +658,7 @@ public Query GreaterThan(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -702,7 +702,7 @@ public Query GreaterThanOrEqualTo(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -747,7 +747,7 @@ public Query NotEqualTo(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } @@ -797,7 +797,7 @@ public Query ContainedIn(String key, Object[] values) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -845,7 +845,7 @@ public Query NotContainedIn(String key, Object[] values) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -887,7 +887,7 @@ public Query Exists(String key) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -928,7 +928,7 @@ public Query NotExists(String key) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -962,7 +962,7 @@ public Query IncludeReferenceContentTypeUID() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1053,12 +1053,12 @@ private Query Tags(String[] tags) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw new QueryFilterException(); } } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1089,7 +1089,7 @@ public Query Ascending(String key) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -1125,7 +1125,7 @@ public Query Descending(String key) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -1158,7 +1158,7 @@ public Query IncludeCount() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1187,7 +1187,7 @@ public Query IncludeMetadata() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1215,7 +1215,7 @@ public Query IncludeOwner() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1378,7 +1378,7 @@ public Query IncludeFallback() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1406,7 +1406,7 @@ public Query IncludeBranch() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1434,7 +1434,7 @@ public Query AddParam(string key, string value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1462,7 +1462,7 @@ public Query includeEmbeddedItems() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1491,7 +1491,7 @@ public Query IncludeSchema() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1520,7 +1520,7 @@ public Query Skip(int number) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1549,7 +1549,7 @@ public Query Limit(int number) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return this; } @@ -1585,7 +1585,7 @@ public Query Regex(String key, String regex) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -1634,7 +1634,7 @@ public Query Regex(String key, String regex, String modifiers) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } } else @@ -1786,7 +1786,7 @@ public async Task Count() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw QueryFilterException.Create(e); } return await Exec(); } @@ -1864,7 +1864,7 @@ private async Task Exec() } else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) { headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken; } else { - throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); + throw new LivePreviewException(); } if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId)) @@ -1948,7 +1948,7 @@ private async Task Exec() } catch (Exception ex) { - throw new ContentstackException("Query failed to execute. Review your syntax and filters, then try again.", ex); + throw QueryFilterException.Create(ex); } } From a08063494db7778652ab52f11a472d340dd09671 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 3 Sep 2025 13:40:54 +0530 Subject: [PATCH 3/3] Refactor error handling to utilize custom exceptions and improve error messaging consistency across various components. --- Contentstack.Core/ContentstackClient.cs | 8 ++-- .../Internals/ContentstackConvert.cs | 16 +++---- .../Internals/ContentstackExceptions.cs | 43 ++++++++++++++++++ Contentstack.Core/Internals/ErrorMessages.cs | 35 +++++++++++++++ Contentstack.Core/Models/Asset.cs | 10 ++--- Contentstack.Core/Models/AssetLibrary.cs | 18 ++++---- Contentstack.Core/Models/ContentType.cs | 2 +- Contentstack.Core/Models/Entry.cs | 22 +++++----- Contentstack.Core/Models/Query.cs | 44 +++++++++---------- Contentstack.Core/Models/Taxonomy.cs | 16 +++---- 10 files changed, 146 insertions(+), 68 deletions(-) diff --git a/Contentstack.Core/ContentstackClient.cs b/Contentstack.Core/ContentstackClient.cs index 172bca3..5a3fdb7 100644 --- a/Contentstack.Core/ContentstackClient.cs +++ b/Contentstack.Core/ContentstackClient.cs @@ -136,7 +136,7 @@ public ContentstackClient(IOptions options) } else { - throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); + throw new InvalidOperationException(ErrorMessages.LivePreviewTokenMissing); } } this.SerializerSettings.DateParseHandling = DateParseHandling.None; @@ -338,7 +338,7 @@ public async Task GetContentTypes(Dictionary param = null } catch (Exception ex) { - throw new GetContentstackError("Contentstack client request failed. Check your network settings or request parameters and try again: " + ex.Message, ex); + throw new GetContentstackError(string.Format(ErrorMessages.ContentstackClientRequestError, ex.Message), ex); } } @@ -372,7 +372,7 @@ private async Task GetLivePreviewData() } else { - throw new InvalidOperationException("Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."); + throw new InvalidOperationException(ErrorMessages.LivePreviewTokenMissing); } if (!string.IsNullOrEmpty(this.LivePreviewConfig.ReleaseId)) @@ -873,7 +873,7 @@ private async Task GetResultAsync(string Init = "false", SyncType Syn } catch (Exception ex) { - throw new GetContentstackError("An error occurred while processing the Contentstack client request: " + ex.Message, ex); + throw new GetContentstackError(string.Format(ErrorMessages.ContentstackSyncRequestError, ex.Message), ex); } } #endregion diff --git a/Contentstack.Core/Internals/ContentstackConvert.cs b/Contentstack.Core/Internals/ContentstackConvert.cs index 6adca75..369bc32 100644 --- a/Contentstack.Core/Internals/ContentstackConvert.cs +++ b/Contentstack.Core/Internals/ContentstackConvert.cs @@ -19,7 +19,7 @@ public static Int32 ToInt32(object input) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } @@ -38,7 +38,7 @@ public static bool ToBoolean(object input) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } @@ -57,7 +57,7 @@ public static string ToString(object input, string defaultValue = "") { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } @@ -76,7 +76,7 @@ public static double ToDouble(object input) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } @@ -95,7 +95,7 @@ public static decimal ToDecimal(object input) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } @@ -114,7 +114,7 @@ public static DateTime ToDateTime(object input) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } @@ -132,7 +132,7 @@ public static string ToISODate(object input) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } return now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'sszzz"); @@ -186,7 +186,7 @@ public static object GetValue(string value) { if (e.Source != null) { - Console.WriteLine("Exception in {0}: {1}\nStackTrace: {2}", e.GetType().Name, e.Message, e.StackTrace); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } } diff --git a/Contentstack.Core/Internals/ContentstackExceptions.cs b/Contentstack.Core/Internals/ContentstackExceptions.cs index 0d94a2e..320d412 100644 --- a/Contentstack.Core/Internals/ContentstackExceptions.cs +++ b/Contentstack.Core/Internals/ContentstackExceptions.cs @@ -149,4 +149,47 @@ public static EntryException CreateForProcessingError(Exception innerException) innerException); } } + + /// + /// Exception thrown when there are taxonomy-related errors + /// + public class TaxonomyException : ContentstackException + { + public TaxonomyException(string message) : base(message) + { + } + + public TaxonomyException(string message, Exception innerException) : base(message, innerException) + { + } + + public static TaxonomyException CreateForProcessingError(Exception innerException) + { + return new TaxonomyException( + string.Format(ErrorMessages.TaxonomyProcessingError, + ErrorMessages.FormatExceptionDetails(innerException)), + innerException); + } + } + + /// + /// Exception thrown when there are content type-related errors + /// + public class ContentTypeException : ContentstackException + { + public ContentTypeException(string message) : base(message) + { + } + + public ContentTypeException(string message, Exception innerException) : base(message, innerException) + { + } + + public static ContentTypeException CreateForProcessingError(Exception innerException) + { + return new ContentTypeException( + ErrorMessages.ContentTypeProcessingError, + innerException); + } + } } \ No newline at end of file diff --git a/Contentstack.Core/Internals/ErrorMessages.cs b/Contentstack.Core/Internals/ErrorMessages.cs index 5efa956..1c4b150 100644 --- a/Contentstack.Core/Internals/ErrorMessages.cs +++ b/Contentstack.Core/Internals/ErrorMessages.cs @@ -18,6 +18,8 @@ internal static class ErrorMessages // Entry related errors public const string EntryProcessingError = "An error occurred while processing the entry. {0}"; + public const string EntryUidRequired = "Please set entry uid."; + public const string EntryNotFoundInCache = "Entry is not present in cache"; // Global Field related errors 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 // Live Preview related errors public const string LivePreviewTokenMissing = "Live Preview token missing. Add either a PreviewToken or a ManagementToken in the LivePreviewConfig."; + // Client Request related errors + public const string ContentstackClientRequestError = "Contentstack client request failed. Check your network settings or request parameters and try again: {0}"; + public const string ContentstackSyncRequestError = "An error occurred while processing the Contentstack client request: {0}"; + + // Taxonomy related errors + public const string TaxonomyProcessingError = "An error occurred while processing the taxonomy operation: {0}"; + + // Content Type related errors + public const string ContentTypeProcessingError = "Content type processing failed. Verify the schema and ensure all required fields are configured."; + + // Authentication and Configuration errors + public const string StackApiKeyRequired = "Stack api key can not be null."; + public const string AccessTokenRequired = "Access token can not be null."; + public const string EnvironmentRequired = "Environment can not be null."; + public const string AuthenticationNotPresent = "Authentication Not present."; + public const string ContentTypeNameRequired = "Please set contentType name."; + + // JSON and Parsing errors + public const string InvalidJsonFormat = "Please provide valid JSON."; + public const string ParsingError = "Parsing Error."; + + // Network and Server errors + public const string NoConnectionError = "Connection error"; + public const string ServerError = "Server interaction went wrong, Please try again."; + public const string NetworkUnavailable = "Network not available."; + public const string DefaultError = "Oops! Something went wrong. Please try again."; + + // Cache related errors + public const string SavingNetworkCallResponseForCache = "Error while saving network call response."; + + // Initialization errors + public const string ContentstackDefaultMethodNotCalled = "You must called Contentstack.stack() first"; + // Helper method to format exception details public static string FormatExceptionDetails(Exception ex) { diff --git a/Contentstack.Core/Models/Asset.cs b/Contentstack.Core/Models/Asset.cs index afd3bae..a2a0d6b 100644 --- a/Contentstack.Core/Models/Asset.cs +++ b/Contentstack.Core/Models/Asset.cs @@ -252,7 +252,7 @@ public Asset IncludeMetadata() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -322,7 +322,7 @@ public DateTime GetCreateAt() catch (Exception e) { if (e.Source != null) - Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } return DateTime.MinValue; } @@ -362,7 +362,7 @@ public DateTime GetUpdateAt() catch (Exception e) { if (e.Source != null) - Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } return DateTime.MinValue; } @@ -384,7 +384,7 @@ public DateTime GetDeleteAt() catch (Exception e) { if (e.Source != null) - Console.WriteLine($"Exception: {e.Message}\nSource: {e.Source ?? "Unknown"}\nStackTrace: {e.StackTrace ?? "No stack trace available"}"); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } return DateTime.MinValue; } @@ -424,7 +424,7 @@ public async Task Fetch() } catch (Exception ex) { - throw new ContentstackException(string.Format(ErrorMessages.AssetProcessingError, ErrorMessages.FormatExceptionDetails(ex))); + throw AssetException.CreateForProcessingError(ex); } } diff --git a/Contentstack.Core/Models/AssetLibrary.cs b/Contentstack.Core/Models/AssetLibrary.cs index a6c0314..102c5dc 100644 --- a/Contentstack.Core/Models/AssetLibrary.cs +++ b/Contentstack.Core/Models/AssetLibrary.cs @@ -100,7 +100,7 @@ public AssetLibrary Query(JObject QueryObject) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -124,7 +124,7 @@ public AssetLibrary IncludeFallback() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -149,7 +149,7 @@ public AssetLibrary IncludeBranch() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -241,7 +241,7 @@ public AssetLibrary IncludeMetadata() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -285,7 +285,7 @@ public AssetLibrary Skip(int number) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -311,7 +311,7 @@ public AssetLibrary Limit(int number) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -340,7 +340,7 @@ public AssetLibrary Tags(string[] tags) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw AssetException.CreateForProcessingError(e); } return this; } @@ -369,7 +369,7 @@ public AssetLibrary Only(String[] fieldUid) } catch (Exception e) { - Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); + throw AssetException.CreateForProcessingError(e); } return this; @@ -401,7 +401,7 @@ public AssetLibrary Except(String[] fieldUids) } catch (Exception e) { - Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); + throw AssetException.CreateForProcessingError(e); } return this; } diff --git a/Contentstack.Core/Models/ContentType.cs b/Contentstack.Core/Models/ContentType.cs index c58eede..1baf39b 100644 --- a/Contentstack.Core/Models/ContentType.cs +++ b/Contentstack.Core/Models/ContentType.cs @@ -176,7 +176,7 @@ public async Task Fetch(Dictionary param = null) } catch (Exception ex) { - throw new ContentstackException("Content type processing failed. Verify the schema and ensure all required fields are configured.", ex); + throw ContentTypeException.CreateForProcessingError(ex); } } diff --git a/Contentstack.Core/Models/Entry.cs b/Contentstack.Core/Models/Entry.cs index 653636e..a5c9ef8 100644 --- a/Contentstack.Core/Models/Entry.cs +++ b/Contentstack.Core/Models/Entry.cs @@ -510,7 +510,7 @@ public Entry SetLanguage(Language language) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } @@ -724,7 +724,7 @@ private Entry IncludeCreatedBy() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -801,7 +801,7 @@ private Entry IncludeUpdatedBy() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1094,7 +1094,7 @@ public Entry IncludeReferenceContentTypeUID() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1121,7 +1121,7 @@ public Entry IncludeFallback() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1149,7 +1149,7 @@ public Entry IncludeMetadata() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1177,7 +1177,7 @@ public Entry AddParam(string key, string value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1204,7 +1204,7 @@ public Entry IncludeBranch() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1231,7 +1231,7 @@ public Entry includeEmbeddedItems() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1259,7 +1259,7 @@ private Entry IncludeSchema() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } @@ -1286,7 +1286,7 @@ public Entry IncludeOwner() } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw EntryException.CreateForProcessingError(e); } return this; } diff --git a/Contentstack.Core/Models/Query.cs b/Contentstack.Core/Models/Query.cs index 98346bf..c80b29a 100644 --- a/Contentstack.Core/Models/Query.cs +++ b/Contentstack.Core/Models/Query.cs @@ -284,7 +284,7 @@ public Query Where(String key, Object value) } else { - throw new QueryFilterException(); + throw QueryFilterException.Create(); } } catch (Exception e) @@ -307,7 +307,7 @@ public Query ReferenceIn(String key, Query query) } else { - throw new QueryFilterException(); + throw QueryFilterException.Create(); } } catch (Exception e) @@ -330,7 +330,7 @@ public Query ReferenceNotIn(String key, Query query) } else { - throw new QueryFilterException(); + throw QueryFilterException.Create(); } } catch (Exception e) @@ -367,7 +367,7 @@ public Query AddQuery(String key, String value) } else { - throw new QueryFilterException(); + throw QueryFilterException.Create(); } } catch (Exception e) @@ -466,7 +466,7 @@ public Query And(List queryObjects) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -532,7 +532,7 @@ public Query Or(List queryObjects) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -576,7 +576,7 @@ public Query LessThan(String key, Object value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -620,7 +620,7 @@ public Query LessThanOrEqualTo(String key, Object value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; } @@ -663,7 +663,7 @@ public Query GreaterThan(String key, Object value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -707,7 +707,7 @@ public Query GreaterThanOrEqualTo(String key, Object value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -753,7 +753,7 @@ public Query NotEqualTo(String key, Object value) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -802,7 +802,7 @@ public Query ContainedIn(String key, Object[] values) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -850,7 +850,7 @@ public Query NotContainedIn(String key, Object[] values) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -892,7 +892,7 @@ public Query Exists(String key) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -933,7 +933,7 @@ public Query NotExists(String key) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; @@ -1053,7 +1053,7 @@ private Query Tags(String[] tags) } else { - throw new QueryFilterException(); + throw QueryFilterException.Create(); } } catch (Exception e) @@ -1094,7 +1094,7 @@ public Query Ascending(String key) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; } @@ -1130,7 +1130,7 @@ public Query Descending(String key) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; } @@ -1247,7 +1247,7 @@ public Query Only(String[] fieldUid) } catch (Exception e) { - Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } return this; @@ -1316,7 +1316,7 @@ public Query Except(String[] fieldUids) } catch (Exception e) { - Console.WriteLine($"Exception in {e.GetType().Name}: {e.Message}\nStackTrace: {e.StackTrace}"); + Console.WriteLine(ErrorMessages.FormatExceptionDetails(e)); } return this; } @@ -1590,7 +1590,7 @@ public Query Regex(String key, String regex) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; } @@ -1639,7 +1639,7 @@ public Query Regex(String key, String regex, String modifiers) } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw QueryFilterException.Create(); } return this; } diff --git a/Contentstack.Core/Models/Taxonomy.cs b/Contentstack.Core/Models/Taxonomy.cs index f30dc13..a493251 100644 --- a/Contentstack.Core/Models/Taxonomy.cs +++ b/Contentstack.Core/Models/Taxonomy.cs @@ -73,12 +73,12 @@ public Taxonomy Above(String key, Object value) Dictionary queryValue = new Dictionary{ { "$above", value } }; QueryValueJson.Add(key, queryValue); } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw TaxonomyException.CreateForProcessingError(e); } } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw TaxonomyException.CreateForProcessingError(null); } return this; @@ -112,12 +112,12 @@ public Taxonomy EqualAndAbove(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw TaxonomyException.CreateForProcessingError(e); } } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw TaxonomyException.CreateForProcessingError(null); } return this; @@ -151,12 +151,12 @@ public Taxonomy Below(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw TaxonomyException.CreateForProcessingError(e); } } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw TaxonomyException.CreateForProcessingError(null); } return this; @@ -190,12 +190,12 @@ public Taxonomy EqualAndBelow(String key, Object value) } catch (Exception e) { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, e); + throw TaxonomyException.CreateForProcessingError(e); } } else { - throw new Exception(StackConstants.ErrorMessage_QueryFilterException, null); + throw TaxonomyException.CreateForProcessingError(null); } return this;