add ExceptionHandlingMiddleware
This commit is contained in:
		
							parent
							
								
									73bfcccc9c
								
							
						
					
					
						commit
						88ccaea826
					
				
							
								
								
									
										38
									
								
								AB-API/Middleware/ExceptionHandlingMiddleware.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								AB-API/Middleware/ExceptionHandlingMiddleware.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | |||||||
|  | using AB.Domain.Exceptions; | ||||||
|  | using System.Text.Json; | ||||||
|  | 
 | ||||||
|  | namespace AB_API.Middleware | ||||||
|  | { | ||||||
|  |     public class ExceptionHandlingMiddleware : IMiddleware | ||||||
|  |     { | ||||||
|  |         public async Task InvokeAsync(HttpContext context, RequestDelegate next) | ||||||
|  |         { | ||||||
|  |             try | ||||||
|  |             { | ||||||
|  |                 await next(context); | ||||||
|  |             } | ||||||
|  |             catch (Exception ex) | ||||||
|  |             { | ||||||
|  |                 await HandleExceptionAsync(context, ex); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private async Task HandleExceptionAsync(HttpContext httpContext, Exception ex) | ||||||
|  |         { | ||||||
|  |             httpContext.Response.ContentType = "application/json"; | ||||||
|  | 
 | ||||||
|  |             httpContext.Response.StatusCode = ex switch | ||||||
|  |             { | ||||||
|  |                 NotFoundException => StatusCodes.Status404NotFound, | ||||||
|  |                 _ => StatusCodes.Status400BadRequest | ||||||
|  |             }; | ||||||
|  | 
 | ||||||
|  |             var response = new | ||||||
|  |             { | ||||||
|  |                 error = ex.Message | ||||||
|  |             }; | ||||||
|  | 
 | ||||||
|  |             await httpContext.Response.WriteAsJsonAsync(response); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -3,6 +3,7 @@ using AB.Persistence; | |||||||
| using AB.Persistence.Repos; | using AB.Persistence.Repos; | ||||||
| using AB.Services; | using AB.Services; | ||||||
| using AB.Services.Abstractions; | using AB.Services.Abstractions; | ||||||
|  | using AB_API.Middleware; | ||||||
| using Microsoft.EntityFrameworkCore; | using Microsoft.EntityFrameworkCore; | ||||||
| using Microsoft.OpenApi.Models; | using Microsoft.OpenApi.Models; | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
| @ -31,6 +32,8 @@ builder.Services.AddDbContextPool<RepoDbContext>(builder => | |||||||
|     builder.UseInMemoryDatabase("test"); |     builder.UseInMemoryDatabase("test"); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | builder.Services.AddTransient<ExceptionHandlingMiddleware>(); | ||||||
|  | 
 | ||||||
| var app = builder.Build(); | var app = builder.Build(); | ||||||
| 
 | 
 | ||||||
| // Configure the HTTP request pipeline. | // Configure the HTTP request pipeline. | ||||||
| @ -40,6 +43,8 @@ if (app.Environment.IsDevelopment()) | |||||||
|     app.UseSwaggerUI(); |     app.UseSwaggerUI(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | app.UseMiddleware<ExceptionHandlingMiddleware>(); | ||||||
|  | 
 | ||||||
| app.UseHttpsRedirection(); | app.UseHttpsRedirection(); | ||||||
| 
 | 
 | ||||||
| app.UseAuthorization(); | app.UseAuthorization(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user