private async Task HandleGlobalExceptionAsync(HttpContext context, Exception ex)
 {
     if (!_environment.IsDevelopment())
     {
         try
         {
             var response = new ResponseTemplateViewDto <bool> {
                 IsSuccess = false, ErrorMessage = "Unhandled exception"
             };
             await WriteResponseAsync(context, HttpStatusCode.InternalServerError, JsonConvert.SerializeObject(response));
         }
         catch (Exception)
         {
             var response = new ResponseTemplateViewDto <bool>
             {
                 IsSuccess    = false,
                 ErrorMessage = $"An unexpected error has occured. Please contact our customer support with the following code {context.TraceIdentifier}."
             };
             await WriteResponseAsync(context, HttpStatusCode.InternalServerError, JsonConvert.SerializeObject(response));
         }
     }
     else
     {
         var response = new ResponseTemplateViewDto <bool> {
             IsSuccess = false, ErrorMessage = ex.Message
         };
         await WriteResponseAsync(context, HttpStatusCode.InternalServerError, JsonConvert.SerializeObject(response));
     }
 }
示例#2
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var eDictionary = context.ModelState
                                  .Where(x => x.Value.Errors.Count > 0)
                                  .ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).First());

                var response = new ResponseTemplateViewDto <List <ValidationErrorDto> >
                {
                    IsSuccess    = false,
                    ErrorMessage = "Model is not valid",
                    Data         = new List <ValidationErrorDto>()
                };

                foreach (var(key, value) in eDictionary)
                {
                    response.Data.Add(new ValidationErrorDto {
                        Key = key, Value = value
                    });
                }

                context.Result = new BadRequestObjectResult(response);
            }
        }
        private async Task HandleUnknownExceptionAsync(HttpContext context, UEFAException ex)
        {
            var response = new ResponseTemplateViewDto <bool> {
                ErrorMessage = ex.Message, IsSuccess = false
            };

            await WriteResponseAsync(context, HttpStatusCode.OK, JsonConvert.SerializeObject(response));
        }