public BusinessRuleProblemDetails(BusinessRuleException exception)
 {
     this.Title  = exception.Message;
     this.Status = StatusCodes.Status400BadRequest;
     this.Detail = exception.Details;
     this.Type   = "https://somedomain/validation-error";
 }
Ejemplo n.º 2
0
    /// <summary>
    /// Handles a BusinessRuleException by displaying the details and general error.
    /// </summary>
    /// <param name="ex">An exception object generated from Entity Framework</param>
    private void HandleException(BusinessRuleException ex)
    {
        var details = from detail in ex.RuleDetails
                      select new
        {
            Error = detail
        };

        ShowExceptions(details, ex.Message, STR_TITLE_ValidationErrors, STR_TITLE_ICON_warning, STR_PANEL_danger);
    }
Ejemplo n.º 3
0
        private void HandleException(BusinessRuleException ex)
        {
            var details = from error in ex.Errors
                          select new
            {
                Error = error.Message
            };

            ShowExceptions(details, $"There are business rule errors in processing {ex.ExecutionContext}", STR_TITLE_ValidationErrors, STR_TITLE_ICON_warning, STR_PANEL_danger);
        }
Ejemplo n.º 4
0
        public virtual void AppIdIsInvalid(Guid appId)
        {
            bool ishad = _applicationRepository.AppIdIsInvalid(appId);

            if (ishad)
            {
                var retValue = new ReturnValue();
                retValue.Set((byte)RegisterDeviceCode.AppIdIsInvalid, DeviceBusinessRule.AppIdIsInvalid);
                throw BusinessRuleException.Create(retValue);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 判断AppId是否无效
        /// </summary>
        /// <param name="appId"></param>
        public virtual void AppIdIsInvalid(Guid appId)
        {
            var result = _applicatinoRepository.AppIdIsInvalid(appId);

            if (result)
            {
                var retValue = new ReturnValue();
                retValue.Set((byte)CreateMessageCode.AppIdIsInvalid, MessageBusinessRule.AppIdIsInvalid);
                throw BusinessRuleException.Create(retValue);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 创建App之前的业务验证
        /// </summary>
        protected void Validate <TEntity>(TEntity t) where TEntity : Entity
        {
            var brokenRules = t.GetBrokenRules().ToList();

            if (brokenRules.Count() > 0)
            {
                var retValue = new ReturnValue();
                retValue.Set((byte)GeneralRetValue.Business, brokenRules);
                throw BusinessRuleException.Create(retValue);
            }
        }
Ejemplo n.º 7
0
        public static FulcrumException ToFulcrumException(FulcrumError error)
        {
            if (error == null)
            {
                return(null);
            }
            FulcrumException exception;

            switch (error.TypeId)
            {
            case BusinessRuleException.ExceptionTypeId:
                exception = new BusinessRuleException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case ConflictException.ExceptionTypeId:
                exception = new ConflictException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case ServerContractException.ExceptionTypeId:
                exception = new ServerContractException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case NotFoundException.ExceptionTypeId:
                exception = new NotFoundException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case UnauthorizedException.ExceptionTypeId:
                exception = new UnauthorizedException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case AssertionFailedException.ExceptionTypeId:
                exception = new AssertionFailedException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case NotImplementedException.ExceptionTypeId:
                exception = new NotImplementedException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case TryAgainException.ExceptionTypeId:
                exception = new TryAgainException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            default:
                exception = null;
                break;
            }
            if (exception == null)
            {
                var message = $"The TypeId ({error.TypeId}) was not recognized: {error.ToJsonString(Formatting.Indented)}";
                return(new AssertionFailedException(message, ToFulcrumException(error.InnerError)));
            }
            exception.CopyFrom(error);
            return(exception);
        }
Ejemplo n.º 8
0
        private static Task HandleBusinessRuleException(HttpContext context, BusinessRuleException ex)
        {
            context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            ApiResponseDto errorDetails = new()
            {
                StatusCode = context.Response.StatusCode,
                Message    = ex.Result.Message,
                Data       = null
            };

            return(DefaultContextResponseSend(context, errorDetails));
        }
Ejemplo n.º 9
0
        private bool IsHasApplicationByAppName(string appName)
        {
            bool isHas = _applicationRepository.AppNameIsInvalid(appName);

            if (isHas)
            {
                var retValue = new ReturnValue();
                retValue.Set((byte)CreateApplicationCode.AppNameIsHad, ApplicationBusinessRule.AppNameIsHad);
                throw BusinessRuleException.Create(retValue);
            }
            return(isHas);
        }
Ejemplo n.º 10
0
        public TResponse Create(BusinessRuleException excep)
        {
            returnValue = excep.ReturnValue;

            var result = new TResponse();

            result.State      = excep.Code == 0;
            result.StatusCode = (TCode)Enum.Parse(typeof(TCode), excep.Code.ToString());
            result.StatusText = excep.ReturnValue.GetBrokenRuleString();

            return(result);
        }
Ejemplo n.º 11
0
        private Ticket GetTicketWithExceptions(string ticketId, ExpectedResultEnum expectedFacadeResult)
        {
            FulcrumException fulcrumException = null;

            switch (expectedFacadeResult)
            {
            case ExpectedResultEnum.Ok:
                return(new Ticket
                {
                    Id = ticketId
                });

            case ExpectedResultEnum.BusinessRuleException:
                fulcrumException = new BusinessRuleException("Business rule exception");
                break;

            case ExpectedResultEnum.ConflictException:
                fulcrumException = new ConflictException("Conflict exception");
                break;

            case ExpectedResultEnum.ServerContractException:
                fulcrumException = new ServerContractException("Contract exception");
                break;

            case ExpectedResultEnum.NotFoundException:
                fulcrumException = new NotFoundException("Not found exception");
                break;

            case ExpectedResultEnum.UnauthorizedException:
                fulcrumException = new UnauthorizedException("Unauthorized exception");
                break;

            case ExpectedResultEnum.AssertionFailedException:
                fulcrumException = new AssertionFailedException("Assertion failed exception");
                break;

            case ExpectedResultEnum.NotImplementedException:
                fulcrumException = new NotImplementedException("Not implemented exception");
                break;

            case ExpectedResultEnum.TryAgainException:
                fulcrumException = new TryAgainException("Try again exception");
                break;

            default:
                fulcrumException = new AssertionFailedException($"Unexpected switch value: {expectedFacadeResult}");
                break;
            }
            // This is to be able to test that the properties are copied all the way back to the test case.
            fulcrumException.Code = fulcrumException.InstanceId;
            throw fulcrumException;
        }
Ejemplo n.º 12
0
        public void BreFlowControlExample(BusinessRuleException bre)
        {
            switch (bre.Message)
            {
            case "OutOfAcceptableRange":
                DoOutOfAcceptableRangeWork();
                break;

            default:
                DoInAcceptableRangeWork();
                break;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 判断指定AppName是否已存在
        /// </summary>
        /// <param name="AppName"></param>
        /// <returns></returns>
        private bool IsHasApplication(Application application)
        {
            bool isHas = _applicationRepository.IsHas(application);

            if (isHas)
            {
                //var retValue = new CreateApplicationRetValue();
                var retValue = new ReturnValue();
                retValue.Set((byte)CreateApplicationCode.AppIdHad, ApplicationBusinessRule.AppIdIsHad);
                throw BusinessRuleException.Create(retValue);
            }
            return(isHas);
        }
Ejemplo n.º 14
0
        public void BRException_CtorWithInnerException_Pass()
        {
            // Arrange
            var message            = nameof(BusinessRuleException);
            var placeholderMessage = $"Violation of business rule. {message}";
            var innerMessage       = message + message;

            // Act
            var bre = new BusinessRuleException(message, new Exception(innerMessage));

            // Assert
            Assert.IsNotNull(bre);
            Assert.AreEqual(placeholderMessage, bre.Message);
            Assert.IsNotNull(bre.InnerException);
            Assert.AreEqual(innerMessage, bre.InnerException.Message);
        }
Ejemplo n.º 15
0
        private static Task HandleBusinessRuleExceptionAsync(HttpContext context, BusinessRuleException exception, bool invalidLogin = false)
        {
            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
            var message = $"{exception.Message}";

            if (exception.InnerException != null)
            {
                message += $"\n\n Inner exception: {exception.InnerException.Message}";
            }

            return(context.Response.WriteAsync(new ExceptionData()
            {
                StatusCode = context.Response.StatusCode,
                Message = message,
                Title = exception.Title,
                InvalidLogin = invalidLogin,
            }.ToString()));
        }
Ejemplo n.º 16
0
        public static object GetResult(BaseResponse response)
        {
            BusinessRuleExceptions exs = null;

            if (response.Acknowledgement.Status == Acknowledgement.STATUS_NONOK)
            {
                foreach (Reply reply in response.Acknowledgement.Replys)
                {
                    var errorCode  = reply.ErrorCode ?? string.Empty;
                    var errorField = reply.ErrorField ?? string.Empty;
                    var errorLevel = reply.Level ?? BusinessRuleException.Level.Error.ToString();
                    // Add BusinessRuleException for all known errors.

                    BusinessRuleException error =
                        BusinessRuleException.CreateBusinessRuleException(reply.DefaultMessage, errorField);
                    if (Enum.TryParse <BusinessRuleException.Level>(errorLevel, out var exceptionLevel))
                    {
                        error.ExceptionLevel = exceptionLevel;
                    }

                    error.AddErrorCode(errorCode);
                    if (exs == null)
                    {
                        exs = new BusinessRuleExceptions(reply.DefaultMessage);
                    }
                    exs.AddErrorCode(errorCode);

                    // Add error data.
                    int idx = 0;
                    reply.CodeValues?.Execute(param => error.AddData((idx++).ToString(), param));

                    exs.AddBusinessRuleException(error);

                    RLog.Error(reply.DefaultMessage);
                }

                exs?.ThrowIfAny();
            }

            return(response.InternalGetResult());
        }
Ejemplo n.º 17
0
        public override void OnException(HttpActionExecutedContext context)
        {
            TracingSystem.TraceException(context.ActionContext.Request.RequestUri.AbsolutePath, context.Exception);

            if (context.Exception is BusinessRuleException)
            {
                BusinessRuleException ex = context.Exception as BusinessRuleException;
                throw new HttpResponseException(context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, context.Exception.Message));
            }

            if (context.Exception is ValidationException)
            {
                ValidationException ex = context.Exception as ValidationException;
                throw new HttpResponseException(context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, context.Exception.Message));
            }

            if (context.Exception is ArgumentNullException)
            {
                throw new HttpResponseException(context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, context.Exception.Message));
            }

            throw new HttpResponseException(context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, context.Exception.InnerException?.Message ?? context.Exception.Message));
        }
Ejemplo n.º 18
0
 ///<summary>
 ///Handles a customer BusinessRuleException by displaying the details and the general error.
 /// </summary>
 /// <param name="ex"></param>
 private void HandleException(BusinessRuleException ex)
 {
     var details = from detail in ex.RuleDetails
                   select new
                   {
                       Error = detail
                   };
     ShowExceptions(details, ex.Message, STR_TITLE_ValidationErrors, STR_TITLE_ICON_warning, STR_PANEL_danger);
 }
 public static ModelStateDictionary MergeWithBusinessException(this ModelStateDictionary modelState, BusinessRuleException ex)
 {
     modelState.AddModelError(string.Empty, ex.Message);
     return modelState;
 }