fix(api): send proper content-type header for errors
This commit is contained in:
parent
cbef15eaa2
commit
0f47042dd1
@ -1,3 +1,5 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
@ -37,6 +39,13 @@ public static class APIJsonExt
|
|||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task WriteJSON(this HttpResponse resp, int statusCode, string jsonText)
|
||||||
|
{
|
||||||
|
resp.StatusCode = statusCode;
|
||||||
|
resp.Headers.Add("content-type", "application/json");
|
||||||
|
await resp.WriteAsync(jsonText);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct FrontersReturnNew
|
public struct FrontersReturnNew
|
||||||
|
@ -107,31 +107,19 @@ public class Startup
|
|||||||
|
|
||||||
// handle common ISEs that are generated by invalid user input
|
// handle common ISEs that are generated by invalid user input
|
||||||
if (exc.Error.IsUserError())
|
if (exc.Error.IsUserError())
|
||||||
{
|
await ctx.Response.WriteJSON(400, "{\"message\":\"400: Bad Request\",\"code\":0}");
|
||||||
ctx.Response.StatusCode = 400;
|
|
||||||
await ctx.Response.WriteAsync("{\"message\":\"400: Bad Request\",\"code\":0}");
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (exc.Error is not PKError)
|
else if (exc.Error is not PKError)
|
||||||
{
|
await ctx.Response.WriteJSON(500, "{\"message\":\"500: Internal Server Error\",\"code\":0}");
|
||||||
ctx.Response.StatusCode = 500;
|
|
||||||
await ctx.Response.WriteAsync("{\"message\":\"500: Internal Server Error\",\"code\":0}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// for some reason, if we don't specifically cast to ModelParseError, it uses the base's ToJson method
|
// for some reason, if we don't specifically cast to ModelParseError, it uses the base's ToJson method
|
||||||
else if (exc.Error is ModelParseError fe)
|
else if (exc.Error is ModelParseError fe)
|
||||||
{
|
await ctx.Response.WriteJSON(fe.ResponseCode, JsonConvert.SerializeObject(fe.ToJson()));
|
||||||
ctx.Response.StatusCode = fe.ResponseCode;
|
|
||||||
await ctx.Response.WriteAsync(JsonConvert.SerializeObject(fe.ToJson()));
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var err = (PKError)exc.Error;
|
var err = (PKError)exc.Error;
|
||||||
ctx.Response.StatusCode = err.ResponseCode;
|
await ctx.Response.WriteJSON(err.ResponseCode, JsonConvert.SerializeObject(err.ToJson()));
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(err.ToJson());
|
|
||||||
await ctx.Response.WriteAsync(json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await ctx.Response.CompleteAsync();
|
await ctx.Response.CompleteAsync();
|
||||||
|
Loading…
Reference in New Issue
Block a user