Line | |
---|
1 | using System.Net;
|
---|
2 | using Microsoft.AspNetCore.Diagnostics;
|
---|
3 | using Microsoft.AspNetCore.Mvc;
|
---|
4 | using Microsoft.Extensions.Logging;
|
---|
5 |
|
---|
6 | namespace Farmatiko.Controllers
|
---|
7 | {
|
---|
8 | [ApiController]
|
---|
9 | public class ErrorController : Controller
|
---|
10 | {
|
---|
11 | private readonly ILogger<ErrorController> _logger;
|
---|
12 |
|
---|
13 | public ErrorController(ILogger<ErrorController> logger)
|
---|
14 | {
|
---|
15 | _logger = logger;
|
---|
16 | }
|
---|
17 | [Route("/Error")]
|
---|
18 | public IActionResult Error()
|
---|
19 | {
|
---|
20 | var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
|
---|
21 | var statusCode = exception.Error.GetType().Name switch
|
---|
22 | {
|
---|
23 | "ArgumentException" => HttpStatusCode.BadRequest,
|
---|
24 | "Exception" => HttpStatusCode.InternalServerError,
|
---|
25 | /*"NotFoundResult" => HttpStatusCode.NotFound,*/
|
---|
26 | _ => HttpStatusCode.ServiceUnavailable
|
---|
27 | };
|
---|
28 | _logger.LogInformation(statusCode.ToString() + " " + exception.ToString());
|
---|
29 | return Problem(detail: exception.Error.Message, statusCode: (int)statusCode);
|
---|
30 | }
|
---|
31 | }
|
---|
32 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.