/// <summary> /// Helper method to perform parameter validation /// </summary> /// <typeparam name="T"></typeparam> /// <param name="result"></param> /// <param name="resource"></param> /// <param name="verb"></param> /// <returns></returns> bool ValidateParameters <T>(IResult <T> result, string resource, string verb) { if (string.IsNullOrWhiteSpace(resource)) { result.AddError($"nullargumentexception.{nameof(resource)}", $"{nameof(resource)} cannot be null or empty"); } if (string.IsNullOrWhiteSpace(verb)) { result.AddError($"nullargumentexception.{nameof(verb)}", $"{nameof(verb)} cannot be null or empty"); } return(!result.HasErrors); }
/// <summary> /// Add error record and log message /// </summary> /// <param name="result"></param> /// <param name="logger"></param> /// <param name="errorKey">Error key found in culture files</param> /// <param name="errorMessage">Translated error message</param> /// <param name="logMessage">Log message - commonly un-translated version of errorMessage</param> /// <param name="resourceIdentifier"></param> /// <param name="errors">Additional errors to forward. These are assumed to have already been translated.</param> /// <param name="methodName">Name of calling method for use in log message for improved debugging</param> public static void AddErrorsAndLog <T>(this IResult <T> result, ILogger logger, string errorKey, string errorMessage, string logMessage, string resourceIdentifier, IEnumerable <IError> errors = null, string methodName = null) { if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = ""; } // Add singular error if (!string.IsNullOrWhiteSpace(errorKey)) { result.AddError(errorKey, errorMessage); } // Add error list if (errors != null && errors.Any()) { result.AddErrors(errors); } var identifierText = !string.IsNullOrWhiteSpace(resourceIdentifier) ? $" ({resourceIdentifier}) -" : ""; // If not provided, try to detect method name if (string.IsNullOrWhiteSpace(methodName)) { methodName = new StackTrace().GetFrame(1).GetMethod().Name; } // Log all of the above logger.LogError($"[{methodName}]{identifierText} {logMessage}"); }
/// <summary> /// Adds a new error with the calling class and method name as the key /// </summary> /// <param name="result"></param> /// <param name="message"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IError AddError <T>(this IResult <T> result, string message) { var frame = new StackFrame(1); var method = frame.GetMethod(); var className = method.DeclaringType.Name; var methodName = Regex.Replace(method.Name, "<|>.*", ""); // Method name without wrapper var key = $"{className}.{methodName}"; return(result.AddError(ErrorType.Error, key, message)); }
public static List <IError> AddErrors <T>(this IResult <T> result, IEnumerable <IError> errors) { if (errors == null || !errors.Any()) { return(result.Errors); } foreach (var error in errors) { result.AddError(error); } return(result.Errors); }
public static IError AddExceptionError <T>(this IResult <T> result, string key, Exception exception) { var message = $"{exception.Message}\n{exception.StackTrace}"; return(result.AddError(ErrorType.Error, key, message)); }
public static IError AddError <T>(this IResult <T> result, string key, string message) => result.AddError(ErrorType.Error, key, message);
/// <summary> /// Add translated error record /// </summary> /// <param name="result"></param> /// <param name="localizer"></param> /// <param name="key">Error key found in culture files</param> /// <param name="arguments">The values with which to format the translated error message</param> public static IError AddError <T>(this IResult <T> result, IStringLocalizer localizer, ErrorType type, string key, params object[] arguments) => result.AddError(type, key, localizer[key, arguments]);