Skip to content
Snippets Groups Projects
Commit ab46b289 authored by Matej Vavrek's avatar Matej Vavrek
Browse files

Merge branch 'exception-handling-middleware' into 'Milestone-1'

Added exception handler middleware

See merge request !11
parents 823b6ba1 6d9c6205
No related branches found
No related tags found
2 merge requests!21Milestone-1,!11Added exception handler middleware
Pipeline #
using Microsoft.AspNetCore.Diagnostics;
namespace Api.Middleware
{
public class ExceptionHandlerMiddleware : IExceptionHandler
{
private readonly ILogger<ExceptionHandlerMiddleware> _logger;
public ExceptionHandlerMiddleware(ILogger<ExceptionHandlerMiddleware> logger)
{
_logger = logger;
}
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
httpContext.Response.StatusCode = 500;
httpContext.Response.ContentType = "application/json";
_logger.LogInformation($"[{DateTimeOffset.Now}] {exception.GetType().Name} at ID {httpContext.TraceIdentifier}, Method {httpContext.Request.Method}: \"{exception.Message}\"");
var error = new
{
StatusCode = 500,
Message = "The server encountered an unexpected error. Please try again later."
};
await httpContext.Response.WriteAsJsonAsync(error, cancellationToken: cancellationToken);
return true;
}
}
}
using Api.Middleware;
using DAL.Data; using DAL.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
...@@ -10,6 +11,8 @@ IConfigurationRoot configuration = new ConfigurationBuilder() ...@@ -10,6 +11,8 @@ IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.{envName}.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{envName}.json", optional: true, reloadOnChange: true)
.Build(); .Build();
builder.Services.AddLogging();
// Add services to the container. // Add services to the container.
builder.Services.AddDbContextFactory<RestaurantDBContext>(options => builder.Services.AddDbContextFactory<RestaurantDBContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("MSSQL")) options.UseSqlServer(configuration.GetConnectionString("MSSQL"))
...@@ -20,6 +23,7 @@ builder.Services.AddControllers(); ...@@ -20,6 +23,7 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddExceptionHandler<ExceptionHandlerMiddleware>();
var app = builder.Build(); var app = builder.Build();
...@@ -28,6 +32,11 @@ if (app.Environment.IsDevelopment()) ...@@ -28,6 +32,11 @@ if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment