Revize e37f5a26
Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)
Backend/Backend/Exceptions/BadRequestException.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace RestAPI.Exceptions |
|
8 |
{ |
|
9 |
public class BadRequestException : Exception |
|
10 |
{ |
|
11 |
public BadRequestException() { } |
|
12 |
public BadRequestException(string message) : base(message) { } |
|
13 |
} |
|
14 |
} |
Backend/Backend/Exceptions/InternalErrorException.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace RestAPI.Exceptions |
|
8 |
{ |
|
9 |
public class InternalErrorException : Exception |
|
10 |
{ |
|
11 |
public InternalErrorException() { } |
|
12 |
public InternalErrorException(string message) : base(message) { } |
|
13 |
} |
|
14 |
} |
Backend/Backend/Middleware/ErrorMiddleware.cs | ||
---|---|---|
1 |
using RestAPI.Exceptions; |
|
2 |
using System.Net; |
|
3 |
using System.Text.Json; |
|
4 |
|
|
5 |
namespace RestAPI.Middleware |
|
6 |
{ |
|
7 |
public class ErrorMiddleware |
|
8 |
{ |
|
9 |
private readonly RequestDelegate _next; |
|
10 |
|
|
11 |
public ErrorMiddleware(RequestDelegate next) |
|
12 |
{ |
|
13 |
_next = next; |
|
14 |
} |
|
15 |
|
|
16 |
public async Task Invoke(HttpContext context) |
|
17 |
{ |
|
18 |
try |
|
19 |
{ |
|
20 |
await _next(context); |
|
21 |
} |
|
22 |
catch (Exception error) |
|
23 |
{ |
|
24 |
var response = context.Response; |
|
25 |
response.ContentType = "application/json"; |
|
26 |
|
|
27 |
switch (error) |
|
28 |
{ |
|
29 |
case InternalErrorException e: |
|
30 |
// Server side exception |
|
31 |
response.StatusCode = (int)HttpStatusCode.InternalServerError; |
|
32 |
break; |
|
33 |
case BadRequestException e: |
|
34 |
// Bad request from client |
|
35 |
response.StatusCode = (int)HttpStatusCode.BadRequest; |
|
36 |
break; |
|
37 |
default: |
|
38 |
// Unhandled error |
|
39 |
response.StatusCode = (int)HttpStatusCode.InternalServerError; |
|
40 |
break; |
|
41 |
} |
|
42 |
|
|
43 |
var result = JsonSerializer.Serialize(new { message = error?.Message }); |
|
44 |
await response.WriteAsync(result); |
|
45 |
} |
|
46 |
} |
|
47 |
} |
|
48 |
} |
Backend/Backend/Program.cs | ||
---|---|---|
1 | 1 |
using System.Text.Json.Serialization; |
2 | 2 |
using Core.Authentication; |
3 | 3 |
using Core.Contexts; |
4 |
using Core.Entities; |
|
5 | 4 |
using Core.Seeding; |
6 | 5 |
using Core.Services; |
7 | 6 |
using Microsoft.AspNetCore.Authentication.JwtBearer; |
... | ... | |
54 | 53 |
builder.Services.AddScoped<ClientInfo>(provider => |
55 | 54 |
ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext)); |
56 | 55 |
|
56 |
|
|
57 | 57 |
// Database |
58 | 58 |
builder.Services.AddDbContext<DatabaseContext>(); |
59 | 59 |
|
... | ... | |
86 | 86 |
// JWT Authentication |
87 | 87 |
app.UseMiddleware<JwtMiddleware>(); |
88 | 88 |
|
89 |
/* |
|
90 |
app.UseAuthentication(); |
|
91 |
app.UseAuthorization(); |
|
92 |
*/ |
|
93 |
|
|
89 |
// Error handling middleware |
|
90 |
app.UseMiddleware<ErrorMiddleware>(); |
|
94 | 91 |
|
95 | 92 |
app.MapControllers(); |
96 | 93 |
|
Také k dispozici: Unified diff
Added global error handling middleware
Two custom exceptions - InternalErrorException for server-side problems and BadRequestException for client-side problems