-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCompanyHostBuilderErrorHandler.cs
More file actions
108 lines (94 loc) · 3.6 KB
/
Copy pathCompanyHostBuilderErrorHandler.cs
File metadata and controls
108 lines (94 loc) · 3.6 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Concurrent;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using ViennaNET.WebApi.Abstractions;
using ViennaNET.WebApi.Data;
namespace ViennaNET.WebApi
{
public sealed partial class CompanyHostBuilder
{
private readonly ConcurrentDictionary<Type, int> _exceptionMapper = new();
/// <inheritdoc />
public ICompanyHostBuilder AddExceptionHandler<TException>(int statusCode) where TException : Exception
{
if (!_exceptionMapper.TryAdd(typeof(TException), statusCode))
{
throw new InvalidOperationException(
$"Exception handler not registered. Exception: {typeof(TException)}; StatusCode: {statusCode}");
}
ConfigureApp(ConfigureExceptionHandler);
return this;
}
/// <summary>
/// Добавить обработку ошибки
/// </summary>
/// <typeparam name="TException">Тип исключения</typeparam>
/// <param name="statusCode">HTTP-код ошибки</param>
/// <returns></returns>
public ICompanyHostBuilder AddExceptionHandler<TException>(HttpStatusCode statusCode) where TException : Exception
{
return AddExceptionHandler<TException>((int)statusCode);
}
/// <summary>
/// Добавить обработку ошибки
/// </summary>
/// <param name="exception">Тип исключения</param>
/// <param name="statusCode">HTTP-код ошибки</param>
/// <returns></returns>
public ICompanyHostBuilder AddExceptionHandler(Type exception, int statusCode)
{
var isExceptionType = exception.IsSubclassOf(typeof(Exception)) ||
exception.IsAssignableFrom(typeof(Exception));
if (!isExceptionType)
{
throw new InvalidOperationException($"Type '{exception}' is not exception");
}
if (!_exceptionMapper.TryAdd(exception, statusCode))
{
throw new InvalidOperationException(
$"Exception handler not registered. Exception: {exception}; StatusCode: {statusCode}");
}
ConfigureApp(ConfigureExceptionHandler);
return this;
}
/// <summary>
/// Добавить обработку ошибки
/// </summary>
/// <param name="exception">Тип исключения</param>
/// <param name="statusCode">HTTP-код ошибки</param>
/// <returns></returns>
public ICompanyHostBuilder AddExceptionHandler(Type exception, HttpStatusCode statusCode)
{
return AddExceptionHandler(exception, (int)statusCode);
}
private void ConfigureExceptionHandler(IApplicationBuilder builder, IConfiguration configuration,
IHostEnvironment env, object container)
{
builder.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionFeature?.Error;
var exceptionType = exception?.GetType();
if (exceptionType != null)
{
if (_exceptionMapper.TryGetValue(exceptionType, out var statusCode))
{
context.Response.StatusCode = statusCode;
context.Response.ContentType = "application/json";
var error = new ErrorDto(exception.Message);
await context.Response.WriteAsync(JsonConvert.SerializeObject(error));
}
}
});
});
}
}
}