Ejemplo n.º 1
0
        public TRes HandleProccessExecution <TReq, TRes>(ICommand <TReq, TRes> command, TReq req)
        {
            _fileAccessor.WriteNewLine(GetRequestText(command, req));

            var result = command.Execute(req);

            _fileAccessor.WriteNewLine(GetResultText(command, result));

            return(result);
        }
        private Task HandleExceptionAsync(HttpContext context, Exception ex)
        {
            var code = HttpStatusCode.InternalServerError; // 500

            object errorResponse;
            var    errorText = "";

            switch (ex)
            {
            case FluentValidation.ValidationException validationEx:
                code = HttpStatusCode.UnprocessableEntity;
                var errors = new List <object>();
                foreach (var exception in validationEx.Errors)
                {
                    errors.Add(new { property = exception.PropertyName, error = exception.ErrorMessage });
                }
                errorResponse = new
                {
                    Errors  = errors,
                    Message = "Validation exception"
                };
                break;

            case EntityNotFoundException:
                errorText     = "Entitet sa prosledjenim identifikatorom nije pronadjen u bazi : ";
                code          = HttpStatusCode.NotFound;
                errorResponse = new
                {
                    Error   = "Entitet sa prosledjenim identifikatorom nije pronadjen u bazi",
                    Message = "Entity not found Exception"
                };
                break;

            default:
                errorText     = "Desila se neocekivana greska na serveru, Greska: ";
                errorResponse = new {
                    Error   = "Internal server error",
                    Message = ex.Message
                };
                break;
            }


            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;

            _textFileAccessor.WriteNewLine(errorText + ex.Message);

            return(context.Response.WriteAsync(JsonConvert.SerializeObject(errorResponse)));
        }