public void Errors_FromModelState() { modelState.AddModelError("Empty", ""); modelState.AddModelError("Error", "Error"); modelState.AddModelError("EmptyErrors", ""); modelState.AddModelError("EmptyErrors", "E"); modelState.Add("NoErrors", new ModelState()); modelState.AddModelError("TwoErrors", "Error1"); modelState.AddModelError("TwoErrors", "Error2"); modelState.AddModelError("NullError", (String)null); modelState.AddModelError("NullErrors", (String)null); modelState.AddModelError("NullErrors", "NotNullError"); modelState.AddModelError("WhitespaceErrors", " "); modelState.AddModelError("WhitespaceErrors", "Whitespace"); Dictionary <String, String> actual = modelState.Errors(); Assert.Equal(" ", actual["WhitespaceErrors"]); Assert.Equal("NotNullError", actual["NullErrors"]); Assert.Equal("Error1", actual["TwoErrors"]); Assert.Equal("E", actual["EmptyErrors"]); Assert.Equal("Error", actual["Error"]); Assert.Null(actual["NullError"]); Assert.Equal(7, actual.Count); Assert.Null(actual["Empty"]); }
public static JsonResult AsJsonResult(this ModelStateDictionary modelState) { return(new JsonResult(new { Success = modelState.IsValid, Erros = modelState.Errors() })); }
public static IEnumerable Errors(this ModelStateDictionary modelState) { if (!modelState.IsValid) { return(modelState.Errors()); } return(null); }
/// <summary> /// Create JSON ContentResult with errors, formidtemplate and database id /// </summary> /// <param name="modelState"></param> /// <param name="formIDTemplate"></param> This is connection between backend and front. Every component will have template id. /// Example project-pmid-1234, project-client-1234. In this template must be project-{0}-{1}. /// {1} will be used to replace and will be replace into front end with DatabaseID /// <param name="DatabaseID"></param> /// <returns>JSON ContentResult</returns> public static ContentResult GenerateJsonErrors(this ModelStateDictionary modelState, String formIDTemplate, Int32 DatabaseID) { var errors = modelState.Errors(); var responseData = new { Errros = errors, formIDTemplate = formIDTemplate, ID = DatabaseID }; ContentResult jsonErrors = JsonHelper.JsonResult(responseData); return(jsonErrors); }
public object CreateErrorResponse(object referenceObject, ModelStateDictionary modelState) { return(new { Success = false, Object = referenceObject, ErrorMessages = modelState.Errors() }); }
public object CreateErrorResponse(ModelStateDictionary modelState) { return(new { Success = false, ErrorMessages = modelState.Errors() }); }
public async Task Invoke(HttpContext context, IErrorLogService service) { try { await _next(context); } catch (RulesException re) { if (context.Response.HasStarted) { throw; } context.Response.StatusCode = 400; context.Response.Headers.Add("exception", "validationException"); var modelState = new ModelStateDictionary(); re.AddModelStateErrors(modelState); var json = JsonConvert.SerializeObject(modelState.Errors(true), _jsonSettings); await context.Response.WriteAsync(json); } catch (CustomMessageException cm) { if (context.Response.HasStarted) { throw; } object[] args = new object[2]; args[0] = cm.Message; args[1] = cm.StackTrace; _logger.LogError(cm, "Exception: Message {0}, Stack {1}", args); ErrorLogDto value = new ErrorLogDto(); value.CustomMessage = cm.ExceptionMessage; value.Message = cm.Message; value.Page = context.Request.Path; value.Stack = cm.StackTrace; value.Type = "Exception"; await service.SaveErrorLog(value); context.Response.StatusCode = 500; context.Response.ContentType = "application/json"; context.Response.Headers.Add("exception", "messageException"); var json = JsonConvert.SerializeObject(new { Message = cm.ExceptionMessage }, _jsonSettings); await context.Response.WriteAsync(json); } catch (Exception ex) { if (context.Response.HasStarted) { throw; } int argCount = 3; if (ex.InnerException != null) { argCount = 5; } object[] args = new object[argCount]; args[0] = ex.Message; args[1] = ex.StackTrace; args[2] = context.Request.Path; if (ex.InnerException != null) { args[3] = ex.InnerException.Message; args[4] = ex.InnerException.StackTrace; _logger.LogError(ex, "Exception: Message {0}, Stack {1}, Path {2}. Inner Exception: Message {3}, Stack {4}", args); } else { _logger.LogError(ex, "Exception: Message {0}, Stack {1}, Path {2}", args); } ErrorLogDto value = new ErrorLogDto(); value.CustomMessage = ex.Message; value.Message = ex.Message; value.Page = context.Request.Path; value.Stack = ex.StackTrace; value.Type = "Exception"; await service.SaveErrorLog(value); context.Response.StatusCode = 500; context.Response.ContentType = "application/json"; context.Response.Headers.Add("exception", "messageException"); object obj; if (ex.InnerException != null) { obj = new { Message = ex.Message, Stack = ex.StackTrace, Path = context.Request.Path, InnerMessage = ex.InnerException.Message, InnerStack = ex.InnerException.StackTrace } } ; else { obj = new { Message = ex.Message, Stack = ex.StackTrace, Path = context.Request.Path } }; var json = JsonConvert.SerializeObject(obj, _jsonSettings); await context.Response.WriteAsync(json); } } }
public JsonResult Format(object viewModel, ModelStateDictionary modelState) => Format(modelState.Errors());
public JsonResult Format(object viewModel, ModelStateDictionary modelState) => new JsonResult(new { isSuccessful = false, errorMessages = modelState.Errors() });