-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchedulerErrorMiddleware.cs
53 lines (48 loc) · 1.56 KB
/
SchedulerErrorMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Nongomaza
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class SchedulerErrorMiddleware
{
private readonly RequestDelegate _next;
public SchedulerErrorMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (Exception ex)
{
await HandleExceptionAsync(httpContext, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var result = JsonConvert.SerializeObject(new
{
action = "error"
});
context.Response.ContentType = "application/json";
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
return context.Response.WriteAsync(result);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class SchedulerErrorMiddlewareExtensions
{
public static IApplicationBuilder UseSchedulerErrorMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<SchedulerErrorMiddleware>();
}
}
}