protected async Task <IActionResult> Error(HttpStatusCode statusCode = (HttpStatusCode)422,
                                                   string propertyKey        = "", string errorMessage = "")
        {
            var errors = new Dictionary <string, List <string> >();

            if (!string.IsNullOrEmpty(errorMessage) && !string.IsNullOrEmpty(propertyKey))
            {
                var errorsList = new List <string> {
                    errorMessage
                };
                errors.Add(propertyKey, errorsList);
            }

            foreach (var item in ModelState)
            {
                var errorMessages = item.Value.Errors.Select(x => x.ErrorMessage);

                var validErrorMessages = new List <string>();
                validErrorMessages.AddRange(errorMessages.Where(message => !string.IsNullOrEmpty(message)));

                if (validErrorMessages.Count > 0)
                {
                    if (errors.ContainsKey(item.Key))
                    {
                        errors[item.Key].AddRange(validErrorMessages);
                    }
                    else
                    {
                        errors.Add(item.Key, validErrorMessages.ToList());
                    }
                }
            }

            var errorsRootObject = new ErrorsRootObject {
                Errors = errors
            };

            var errorsJson = JsonFieldsSerializer.Serialize(errorsRootObject, null);

            return(await Task.FromResult <IActionResult>(new ErrorActionResult(errorsJson, statusCode)));
        }
Example #2
0
        private void SetError(ActionExecutedContext actionExecutedContext, KeyValuePair <string, List <string> > error)
        {
            var bindingError = new Dictionary <string, List <string> > {
                { error.Key, error.Value }
            };
            var errorsRootObject = new ErrorsRootObject
            {
                Errors = bindingError
            };

            var errorJson = _jsonFieldsSerializer.Serialize(errorsRootObject, null);

            actionExecutedContext.Result = new ErrorActionResult(errorJson, HttpStatusCode.BadRequest);
        }
        private void SetError(ActionExecutedContext actionExecutedContext, KeyValuePair <string, List <string> > error)
        {
            var bindingError = new Dictionary <string, List <string> >
            {
                {
                    error.Key, error.Value
                }
            };

            var errorsRootObject = new ErrorsRootObject
            {
                Errors = bindingError
            };

            var errorJson = _jsonFieldsSerializer.Serialize(errorsRootObject, null);
            var response  = new ContentResult();

            response.Content             = errorJson;
            response.ContentType         = "application/json";
            response.StatusCode          = (int)HttpStatusCode.InternalServerError;
            actionExecutedContext.Result = response;
        }
Example #4
0
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Response != null && actionExecutedContext.Response.StatusCode != HttpStatusCode.OK)
            {
                var content = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;

                DefaultWeApiErrorsModel defaultWebApiErrorsModel = JsonConvert.DeserializeObject <DefaultWeApiErrorsModel>(content);

                // If both are null this means that it is not the default web api error format,
                // which means that it the error is formatted by our standard and we don't need to do anything.
                if (!string.IsNullOrEmpty(defaultWebApiErrorsModel.Message) &&
                    !string.IsNullOrEmpty(defaultWebApiErrorsModel.MessageDetail))
                {
                    Dictionary <string, List <string> > bindingError = new Dictionary <string, List <string> >()
                    {
                        {
                            "lookup_error",
                            new List <string>()
                            {
                                "not found"
                            }
                        }
                    };

                    var errorsRootObject = new ErrorsRootObject()
                    {
                        Errors = bindingError
                    };

                    var errorJson = _jsonFieldsSerializer.Serialize(errorsRootObject, null);

                    actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(
                        HttpStatusCode.BadRequest, errorJson);
                }
            }

            base.OnActionExecuted(actionExecutedContext);
        }