ErrorController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using FMSAdmin.Helpers;
  3. using Microsoft.AspNetCore.Diagnostics;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Data.SqlClient;
  8. using Microsoft.Extensions.Logging;
  9. namespace FMSAdmin.Controllers {
  10. [ApiController]
  11. public class ErrorController : ControllerBase {
  12. private readonly ILogger<ErrorController> _logger;
  13. public ErrorController(ILogger<ErrorController> logger) {
  14. _logger = logger;
  15. }
  16. [Route("/error-local-development")]
  17. public IActionResult ErrorLocalDevelopment(
  18. [FromServices] IWebHostEnvironment webHostEnvironment) {
  19. if (webHostEnvironment.EnvironmentName != "Development") {
  20. throw new InvalidOperationException(
  21. "This shouldn't be invoked in non-development environments.");
  22. }
  23. var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
  24. if (context.Error is ServiceException) {
  25. return BadRequest(context.Error.Message);
  26. } else if (context.Error is SqlException) {
  27. var sqlException = context.Error as SqlException;
  28. if (HttpMethods.IsDelete(HttpContext.Request.Method) && sqlException.Number == 547) return BadRequest("이미 다른 화면에서 사용중이므로 삭제할 수 없습니다.");
  29. return BadRequest(context.Error.Message);
  30. } else if (context.Error.InnerException is SqlException) {
  31. var sqlException = context.Error.InnerException as SqlException;
  32. if (HttpMethods.IsDelete(HttpContext.Request.Method) && sqlException.Number == 547) return BadRequest("이미 다른 화면에서 사용중이므로 삭제할 수 없습니다.");
  33. return BadRequest(context.Error.InnerException.Message);
  34. }
  35. return Problem(
  36. type: context.Error.GetType().ToString(),
  37. detail: context.Error.StackTrace,
  38. title: context.Error.Message);
  39. }
  40. [Route("/error")]
  41. public IActionResult Error(
  42. [FromServices] IWebHostEnvironment webHostEnvironment) {
  43. var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
  44. if (context.Error is ServiceException) {
  45. return BadRequest(context.Error.Message);
  46. } else if (context.Error is SqlException) {
  47. var sqlException = context.Error as SqlException;
  48. if (HttpMethods.IsDelete(HttpContext.Request.Method) && sqlException.Number == 547) return BadRequest("이미 다른 화면에서 사용중이므로 삭제할 수 없습니다.");
  49. return BadRequest(context.Error.Message);
  50. } else if (context.Error.InnerException is SqlException) {
  51. var sqlException = context.Error.InnerException as SqlException;
  52. if (HttpMethods.IsDelete(HttpContext.Request.Method) && sqlException.Number == 547) return BadRequest("이미 다른 화면에서 사용중이므로 삭제할 수 없습니다.");
  53. return BadRequest(context.Error.InnerException.Message);
  54. }
  55. return Problem(
  56. type: context.Error.GetType().ToString(),
  57. title: context.Error.Message);
  58. }
  59. [Route("/error-default")]
  60. public IActionResult ErrorDefault() => Problem();
  61. }
  62. }