/// <summary>
        /// Converts to.
        /// </summary>
        /// <param name="sqlException">The SQL exception.</param>
        /// <returns>Beyova.Diagnostic.BaseException.</returns>
        public static BaseException ConvertTo(SqlStoredProcedureException sqlException)
        {
            BaseException result = null;

            if (sqlException != null)
            {
                switch (sqlException.Code.Major)
                {
                case ExceptionCode.MajorCode.NullOrInvalidValue:
                    result = new InvalidObjectException(sqlException, reason: sqlException.Code.Minor);
                    break;

                case ExceptionCode.MajorCode.UnauthorizedOperation:
                    result = new UnauthorizedOperationException(sqlException, minorCode: sqlException.Code.Minor);
                    break;

                case ExceptionCode.MajorCode.OperationForbidden:
                    result = new OperationForbiddenException(sqlException.Code.Minor, sqlException);
                    break;

                case ExceptionCode.MajorCode.DataConflict:
                    result = new DataConflictException(sqlException.Code.Minor, innerException: sqlException);
                    break;

                default:
                    result = sqlException;
                    break;
                }
            }

            return(result);
        }
Exemple #2
0
        internal static IActionResult ErrorResult <TModel>(BaseException exception)
        {
            switch (exception)
            {
            case ForbiddenException _:
            case NotFoundException _:
            case UnauthorizedException _:
                LogWarning <TModel>("A warning has occurred.", exception);
                break;

            default:
                LogError <TModel>("An error has occurred.", exception);
                break;
            }

            var @try = HandleException <TModel>(exception);

            return(@try.Match(
                       failure => failure switch
            {
                InvalidRequestException _ => new BadRequestObjectResult(@try),
                ForbiddenException _ => new ObjectResult(@try)
                {
                    StatusCode = 403
                },
                NotFoundException _ => new NotFoundObjectResult(@try),
                AlreadyExistsException _ => new ConflictObjectResult(@try),
                UnauthorizedException _ => new UnauthorizedObjectResult(@try),
                InvalidObjectException _ => new UnprocessableEntityObjectResult(@try),
                _ => new ObjectResult(@try)
                {
                    StatusCode = 500
                },
            },
        /// <summary>
        /// To the exception.
        /// </summary>
        /// <param name="exceptionInfo">The exception information.</param>
        /// <returns>System.Exception.</returns>
        public static Exception ToException(this ExceptionInfo exceptionInfo)
        {
            Exception result = null;

            if (exceptionInfo != null)
            {
                var innerException = exceptionInfo.InnerException;

                switch (exceptionInfo.ExceptionType)
                {
                case "Beyova.Diagnostic.HttpOperationException":
                    return(new HttpOperationException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint));

                case "Beyova.Diagnostic.SqlStoredProcedureException":
                    return(new SqlStoredProcedureException(exceptionInfo.Message, exceptionInfo.Code));

                default:
                    break;
                }

                switch (exceptionInfo.Code.Major)
                {
                case ExceptionCode.MajorCode.CreditNotAfford:
                    result = new CreditNotAffordException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.DataConflict:
                    result = new DataConflictException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.NotImplemented:
                    result = new UnimplementedException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.NullOrInvalidValue:
                    result = new InvalidObjectException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.OperationFailure:
                    result = new OperationFailureException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.OperationForbidden:
                    result = new OperationForbiddenException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.ResourceNotFound:
                    result = new ResourceNotFoundException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.ServiceUnavailable:
                    result = new ServiceUnavailableException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.UnauthorizedOperation:
                    result = new UnauthorizedOperationException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.HttpBlockError:
                    result = new HttpOperationException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                case ExceptionCode.MajorCode.Unsupported:
                    result = new UnsupportedException(exceptionInfo.Key ?? Guid.NewGuid(), exceptionInfo.CreatedStamp ?? DateTime.UtcNow, exceptionInfo.Message, exceptionInfo.Scene, exceptionInfo.Code, ToException(innerException), exceptionInfo.OperatorCredential, exceptionInfo.Data, exceptionInfo.Hint);
                    break;

                default:
                    result = new Exception(exceptionInfo.Message);
                    break;
                }
            }

            return(result);
        }