private string GetErrorResponse(Exception e)
        {
            var errors = new ErrorCollection();

            errors.Add(new Error(400, e.Message, ErrorMeta.FromException(e)));
            return(errors.GetJson());
        }
Example #2
0
        public async Task WriteAsync(OutputFormatterWriteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var response = context.HttpContext.Response;

            using var writer = context.WriterFactory(response.Body, Encoding.UTF8);
            string responseContent;

            if (_serializer == null)
            {
                responseContent = JsonConvert.SerializeObject(context.Object);
            }
            else
            {
                response.ContentType = Constants.ContentType;
                try
                {
                    if (context.Object is ProblemDetails pd)
                    {
                        var errors = new ErrorCollection();
                        errors.Add(new Error(pd.Status.Value, pd.Title, pd.Detail));
                        responseContent = _serializer.Serialize(errors);
                    }
                    else
                    {
                        responseContent = _serializer.Serialize(context.Object);
                    }
                }
                catch (Exception e)
                {
                    _logger?.LogError(new EventId(), e, "An error ocurred while formatting the response");
                    var errors = new ErrorCollection();
                    errors.Add(new Error(500, e.Message, ErrorMeta.FromException(e)));
                    responseContent     = _serializer.Serialize(errors);
                    response.StatusCode = 500;
                }
            }
            await writer.WriteAsync(responseContent);

            await writer.FlushAsync();
        }
        public static ErrorCollection ConvertToErrorCollection <T>(this ModelStateDictionary modelState, IResourceGraph resourceGraph)
        {
            ErrorCollection collection = new ErrorCollection();

            foreach (var entry in modelState)
            {
                if (entry.Value.Errors.Any() == false)
                {
                    continue;
                }

                var attrName = resourceGraph.GetPublicAttributeName <T>(entry.Key);

                foreach (var modelError in entry.Value.Errors)
                {
                    if (modelError.Exception is JsonApiException jex)
                    {
                        collection.Errors.AddRange(jex.GetError().Errors);
                    }
                    else
                    {
                        collection.Errors.Add(new Error(
                                                  status: 422,
                                                  title: entry.Key,
                                                  detail: modelError.ErrorMessage,
                                                  meta: modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null,
                                                  source: attrName == null ? null : new {
                            pointer = $"/data/attributes/{attrName}"
                        }));
                    }
                }
            }

            return(collection);
        }
        public static ErrorCollection ConvertToErrorCollection(this ModelStateDictionary modelState)
        {
            ErrorCollection collection = new ErrorCollection();

            foreach (var entry in modelState)
            {
                if (entry.Value.Errors.Any() == false)
                {
                    continue;
                }

                foreach (var modelError in entry.Value.Errors)
                {
                    if (modelError.Exception is JsonApiException jex)
                    {
                        collection.Errors.AddRange(jex.GetError().Errors);
                    }
                    else
                    {
                        collection.Errors.Add(new Error(400, entry.Key, modelError.ErrorMessage, modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null));
                    }
                }
            }

            return(collection);
        }
        public static ErrorCollection ConvertToErrorCollection <T>(this ModelStateDictionary modelState) where T : class, IIdentifiable
        {
            ErrorCollection collection = new ErrorCollection();

            foreach (var entry in modelState)
            {
                if (entry.Value.Errors.Any() == false)
                {
                    continue;
                }

                var targetedProperty = typeof(T).GetProperty(entry.Key);
                var attrName         = targetedProperty.GetCustomAttribute <AttrAttribute>().PublicAttributeName;

                foreach (var modelError in entry.Value.Errors)
                {
                    if (modelError.Exception is JsonApiException jex)
                    {
                        collection.Errors.AddRange(jex.GetError().Errors);
                    }
                    else
                    {
                        collection.Errors.Add(new Error(
                                                  status: 422,
                                                  title: entry.Key,
                                                  detail: modelError.ErrorMessage,
                                                  meta: modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null,
                                                  source: attrName == null ? null : new
                        {
                            pointer = $"/data/attributes/{attrName}"
                        }));
                    }
                }
            }
            return(collection);
        }