AppException.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Globalization;
  3. namespace MTWorkHR.Core.Global
  4. {
  5. public class AppException : Exception
  6. {
  7. /// <summary>
  8. /// Gets the error number associated with this exception.
  9. /// </summary>
  10. public readonly ExceptionEnum ErrorNumber;
  11. /// <summary>
  12. /// Gets the formatted error message.
  13. /// </summary>
  14. public string ErrorMessage { get; private set; } = string.Empty;
  15. public AppException(string errorMessage) : base(errorMessage)
  16. {
  17. ErrorMessage = errorMessage;
  18. }
  19. /// <summary>
  20. /// Creates a new instance of <see cref="AppException"/> using an error number, language, and optional format arguments.
  21. /// </summary>
  22. /// <param name="errorNumber">The error number (enum) representing the exception type.</param>
  23. /// <param name="language">The language code (e.g., "en" or "ar").</param>
  24. /// <param name="args">Optional arguments to format the error message.</param>
  25. public AppException(ExceptionEnum errorNumber, params object[] args) : base()
  26. {
  27. ErrorNumber = errorNumber;
  28. ErrorMessage = GetMessage(errorNumber, GlobalInfo.lang?? "en", args);
  29. // Initialize the base exception with the localized error message.
  30. this.SetErrorMessage();
  31. }
  32. /// <summary>
  33. /// Sets the exception message for the base exception class.
  34. /// </summary>
  35. private void SetErrorMessage()
  36. {
  37. base.HelpLink = ErrorNumber.ToString(); // Optional: Use error number as a link or reference
  38. base.Source = nameof(AppException); // Optional: Set the exception source
  39. }
  40. /// <summary>
  41. /// Retrieves the error message based on the provided error code and language.
  42. /// </summary>
  43. /// <param name="code">The error number (enum).</param>
  44. /// <param name="language">The language code (default: "en").</param>
  45. /// <param name="args">Optional parameters for formatting the message.</param>
  46. /// <returns>The localized error message.</returns>
  47. public static string GetMessage(ExceptionEnum code, string language = "en", params object[] args)
  48. {
  49. if (AppExceptions.ExceptionMessages.TryGetValue(code, out var translations)
  50. && translations.TryGetValue(language, out var message))
  51. {
  52. try
  53. {
  54. return string.Format(CultureInfo.InvariantCulture, message, args);
  55. }
  56. catch (FormatException)
  57. {
  58. return language == "ar" ? "فشل في تنسيق الرسالة." : "Message formatting failed.";
  59. }
  60. }
  61. // Default fallback
  62. return language == "ar" ? "رسالة الخطأ غير موجودة." : "Error message not found.";
  63. }
  64. public override string ToString()
  65. {
  66. return $"{base.ToString()}\nError Number: {ErrorNumber}\nError Message: {ErrorMessage}";
  67. }
  68. }
  69. }