Files
xCommons/Extensions/ExceptionExtensions.cs
T
2024-01-25 04:39:39 +03:30

170 lines
5.0 KiB
C#

using System;
using xExceptions.Constants;
using xExceptions.Models;
namespace xCommons.Extensions {
/// <summary>
/// this is an extension pack for Exceptions
/// </summary>
public static partial class ExceptionExtensions {
/// <summary>
/// determines an string contains an XError content or not
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsXError (this string source) {
//
if (source.IsNullOrEmpty ()) {
return false;
}
//
var xError = source.ToXError ();
if (xError != null) {
return true;
}
//
// If Convert Proccess fails ...
var normalString = source.ToNormalString ();
//
// Return result based on string values ...
return normalString.Contains ("id") && normalString.Contains ("message");
}
/// <summary>
/// deserialize an string to to XError class instance
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XError ToXError (this string source) {
//
if (source.IsNullOrEmpty ()) {
return null;
}
//
try {
//
var err = source.FromJSON<XError> ();
//
// if err is null we have to manually get value
if (err == null) {
//
var normalString = source.ToNormalString ();
var parts = normalString.Split (',');
if (parts.Length > 2) {
return null;
}
//
var idContainer = parts[0];
var messageContainer = parts[1];
}
//
return err;
} catch {
return null;
}
}
public static XError ToXError (this XException source) {
//
var errorId = (int) source;
var errorMessage = source.GetStringValue ();
//
return new XError {
Id = errorId,
Message = errorMessage
};
}
/// <summary>
/// Convert an XError instance to an Exception
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Exception ToException (this XError source) {
//
var jsonStr = source.ToJSON ();
return new Exception (jsonStr);
}
public static Exception ToException (this XException source) {
return source
.ToXError ()
.ToException ();
}
/// <summary>
/// Convert an Exception to corresponding XError Object
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XError FromException (this Exception source) {
//
if (source == null || source.Message.IsNullOrEmpty ()) {
return null;
}
//
return source.Message.ToXError ();
}
/// <summary>
/// Add Content to Contentable XException member and return XError Object
/// </summary>
/// <param name="exception"></param>
/// <param name="value"></param>
/// <returns></returns>
public static XError AddContentToError (this XException exception, string value) {
//
var xError = exception.ToXError ();
xError.Message = string.Format (xError.Message, value);
//
return xError;
}
/// <summary>
/// Add Content to Contentable XException member and return Exception Object
/// </summary>
/// <param name="exception"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Exception AddContentToException (this XException exception, string value) {
//
var xError = exception.ToXError ();
xError.Message = string.Format (xError.Message, value);
//
return xError.ToException ();
}
/// <summary>
/// Throw Specific Exception ...
/// </summary>
/// <param name="source"></param>
/// <param name="content"></param>
public static void Throw (
this XException source,
string content = null
) {
//
Exception exception = null;
if (content.IsNullOrEmpty ()) {
exception = source.ToException ();
} else {
exception = source.AddContentToException (content);
}
//
throw exception;
}
}
}