Beispiel #1
0
        public static UserError Create(UserException exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }
            var exceptionType = exception.GetType();

            if (!exceptionType.IsGenericType)
            {
                throw new NotSupportedException(
                          $"Expected to get a generic UserException. Got: {exception.GetType().Name}.");
            }
            dynamic dynamicException = exception;

            if (exceptionType.GetGenericTypeDefinition() == typeof(UserException <>))
            {
                var errorType     = exceptionType.GetGenericArguments().First();
                var userErrorType = typeof(UserError <>).MakeGenericType(errorType);
                return((UserError)Activator.CreateInstance(userErrorType, exception.Message, dynamicException.Type));
            }
            if (exceptionType.GetGenericTypeDefinition() == typeof(UserException <,>))
            {
                var genericArgs   = exceptionType.GetGenericArguments();
                var userErrorType = typeof(UserError <,>).MakeGenericType(genericArgs[0], genericArgs[1]);
                return((UserError)Activator.CreateInstance(userErrorType, exception.Message, dynamicException.Type,
                                                           dynamicException.Details));
            }
            throw new NotSupportedException($"Exception of type {exception.GetType().Name} is not supported.");
        }
 public static UserError <T> ToUserError <T>(this UserException <T> exception) where T : struct
 {
     if (exception == null)
     {
         throw new ArgumentNullException(nameof(exception));
     }
     return(UserError.Create(exception));
 }
 public static UserError ToUserError(this UserException exception)
 {
     return(UserError.Create(exception));
 }
Beispiel #4
0
 public static UserError <T, TDetails> Create <T, TDetails>(UserException <T, TDetails> exception) where T : struct
 {
     return(new UserError <T, TDetails>(exception.Message, exception.Type, exception.Details));
 }
Beispiel #5
0
 public static UserError <T> Create <T>(UserException <T> exception) where T : struct
 {
     return(new UserError <T>(exception.Message, exception.Type));
 }
Beispiel #6
0
 public static UserException <T, TDetails> ToUserException <T, TDetails>(this UserError <T, TDetails> error)
     where T : struct
 {
     return(UserException.Create(error.Type, error.Details, error.Message));
 }