Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit 1e7b497

Browse files
committed
Implementata exception status code 503 - close #3
1 parent 9c99e1c commit 1e7b497

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace CustomLibrary.ProblemDetails.Exception;
2+
3+
public class ServiceUnavailableException : System.Exception
4+
{
5+
public ServiceUnavailableException()
6+
{
7+
}
8+
9+
public ServiceUnavailableException(string message) : base(message)
10+
{
11+
}
12+
13+
public ServiceUnavailableException(string message, System.Exception innerException) : base(message, innerException)
14+
{
15+
}
16+
}

src/CustomLibrary.ProblemDetails/ResponseException.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,32 @@ public static ObjectResult BadGateway(HttpContext httpContext, System.Exception
365365

366366
return result;
367367
}
368+
369+
public static ObjectResult ServiceUnavailable(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
370+
{
371+
var statusCode = StatusCodes.Status503ServiceUnavailable;
372+
var problemDetails = new CustomProblemDetails
373+
{
374+
Status = statusCode,
375+
Detail = exc.Message,
376+
Type = $"https://httpstatuses.com/{statusCode}",
377+
Instance = httpContext.Request.Path,
378+
Title = "ServiceUnavailable"
379+
};
380+
381+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
382+
//problemDetails.Extensions.Add("errors", exc.Message);
383+
384+
if (validationError?.Any() ?? false)
385+
{
386+
problemDetails.Extensions.Add("errors", validationError);
387+
}
388+
389+
var result = new ObjectResult(problemDetails)
390+
{
391+
StatusCode = statusCode
392+
};
393+
394+
return result;
395+
}
368396
}

0 commit comments

Comments
 (0)