refactor: for maintenance mode, return the standard error

This commit is contained in:
Polianin Nikita 2024-12-22 05:51:11 +03:00
parent 7bafbb95c4
commit 7b94f9cc1f
3 changed files with 21 additions and 7 deletions

View File

@ -0,0 +1,8 @@
using System;
namespace Mirea.Api.Endpoint.Common.Exceptions;
public class ServerUnavailableException(string message, bool addRetryAfter) : Exception(message)
{
public bool AddRetryAfter { get; } = addRetryAfter;
}

View File

@ -71,6 +71,14 @@ public class CustomExceptionHandlerMiddleware(RequestDelegate next, ILogger<Cust
problemDetails.Type = "https://tools.ietf.org/html/rfc9110#section-15.5.2";
problemDetails.Title = "Unauthorized access.";
break;
case ServerUnavailableException unavailableException:
problemDetails.Status = StatusCodes.Status503ServiceUnavailable;
problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc9110#section-15.6.4";
problemDetails.Title = "Server unavailable.";
problemDetails.Detail = unavailableException.Message;
if (unavailableException.AddRetryAfter)
context.Response.Headers.RetryAfter = "600";
break;
}
if (problemDetails.Status == StatusCodes.Status500InternalServerError)

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Mirea.Api.Endpoint.Common.Attributes;
using Mirea.Api.Endpoint.Common.Exceptions;
using Mirea.Api.Endpoint.Common.Interfaces;
using System.Threading.Tasks;
@ -25,13 +26,10 @@ public class MaintenanceModeMiddleware(RequestDelegate next, IMaintenanceModeSer
string error;
if (maintenanceModeService.IsMaintenanceMode)
{
context.Response.Headers.RetryAfter = "600";
error = "The service is currently undergoing maintenance. Please try again later.";
}
else
error =
"The service is currently not configured. Go to the setup page if you are an administrator or try again later.";
throw new ServerUnavailableException("The service is currently undergoing maintenance. Please try again later.", true);
throw new ServerUnavailableException(
"The service is currently not configured. Go to the setup page if you are an administrator or try again later.", false);
await context.Response.WriteAsync(error);
}