private string GetErrorResponse(Exception e) { var errors = new ErrorCollection(); errors.Add(new Error(400, e.Message, ErrorMeta.FromException(e))); return(errors.GetJson()); }
public async Task WriteAsync(OutputFormatterWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } _logger?.LogInformation("Formatting response as JSONAPI"); var response = context.HttpContext.Response; using (var writer = context.WriterFactory(response.Body, Encoding.UTF8)) { response.ContentType = Constants.ContentType; string responseContent; try { responseContent = GetResponseBody(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("400", e.Message)); responseContent = errors.GetJson(); response.StatusCode = 400; } await writer.WriteAsync(responseContent); await writer.FlushAsync(); } }
protected IActionResult UserLockedResult() { var errors = new ErrorCollection(); errors.Add(new Error(403, "User is Locked")); return(new ContentResult() { StatusCode = StatusCodes.Status403Forbidden, Content = errors.GetJson() }); }
private string GetErrorJson(object responseObject, ILogger logger) { if (responseObject.GetType() == typeof(Error)) { var errors = new ErrorCollection(); errors.Add((Error)responseObject); return(errors.GetJson()); } else { logger?.LogInformation("Response was not a JSONAPI entity. Serializing as plain JSON."); return(JsonConvert.SerializeObject(responseObject)); } }
private string GetResponseBody(object responseObject, IJsonApiContext jsonApiContext, ILogger logger) { if (responseObject.GetType() == typeof(Error) || jsonApiContext.RequestEntity == null) { if (responseObject.GetType() == typeof(Error)) { var errors = new ErrorCollection(); errors.Add((Error)responseObject); return(errors.GetJson()); } else { logger?.LogInformation("Response was not a JSONAPI entity. Serializing as plain JSON."); return(JsonConvert.SerializeObject(responseObject)); } } else { return(JsonApiSerializer.Serialize(responseObject, jsonApiContext)); } }
public async Task WriteAsync(OutputFormatterWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var logger = GetService <ILoggerFactory>(context)? .CreateLogger <JsonApiOutputFormatter>(); logger?.LogInformation("Formatting response as JSONAPI"); var response = context.HttpContext.Response; using (var writer = context.WriterFactory(response.Body, Encoding.UTF8)) { var jsonApiContext = GetService <IJsonApiContext>(context); response.ContentType = "application/vnd.api+json"; string responseContent; try { responseContent = GetResponseBody(context.Object, jsonApiContext, logger); } catch (Exception e) { logger?.LogError(new EventId(), e, "An error ocurred while formatting the response"); var errors = new ErrorCollection(); errors.Add(new Error("400", e.Message)); responseContent = errors.GetJson(); response.StatusCode = 400; } await writer.WriteAsync(responseContent); await writer.FlushAsync(); } }