public async Task <IActionResult> GetBusinessByBRCCode(string certificateNumber) { try { var business = await _repo.Business.GetBusinessByBRCCode(certificateNumber); if (business == null) { _logger.LogError($"Business with BRCCode: {certificateNumber} not Found. Action: GetBusinessById"); var exception = new CustomNotFoundException($"Business with CertificateNumber: {certificateNumber} not found"); return(NotFound(new NotFoundError(exception))); } else { _logger.LogInfo($"Retrieved Business with BRCCode: {business.BRCCode} from DB. GetBusinessByBRCCode Action"); var businessResult = _mapper.Map <BusinessDto>(business); return(Ok(businessResult)); } } catch (Exception ex) { _logger.LogError($"Error happened in Action: GetBusinessByBRCCode. Error: {ex.Message}"); return(new ObjectResult(new InternalServerError(ex))); } }
public async Task <IActionResult> GetInspectionById(int id) { try { var inspection = await _repo.Inspection.GetInspectionById(id); if (inspection == null) { _logger.LogError($"Inspection with ID: {id} not Found. Action: GetInspectionById"); var exception = new CustomNotFoundException($"Inspection with ID: {id} not found"); return(NotFound(new NotFoundError(exception))); } else { _logger.LogInfo($"Retrieved Inspection with ID: {inspection.Id} from DB. GetInspectionById Action"); var inspectionResult = _mapper.Map <InspectionDto>(inspection); return(Ok(inspectionResult)); } } catch (Exception ex) { _logger.LogError($"Error happened in Action: GetInspectionById. Error: {ex.Message}"); return(new ObjectResult(new InternalServerError(ex))); } }
private static Task HandleExceptionAsync(HttpContext context, Exception ex) { var code = ex switch { CustomNotFoundException _ => HttpStatusCode.NotFound, _ => HttpStatusCode.InternalServerError }; var result = JsonConvert.SerializeObject(new { error = ex.Message }); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)code; return(context.Response.WriteAsync(result)); } }