웹 API 메소드에서 TRY CATCH를 사용하지 않는 것이 좋습니다.웹 API 응답을 전송하는 적절한 방법
다음 메소드를 호출하여 메소드가 제대로 작동하면 상태 200과 함께 Employee 객체를 반환하고 싶습니다. 데이터베이스 호출이 실패합니다. 상태 500을 반환하려고합니다. 코드를 처리하는 올바른 방법은 무엇입니까?
[HttpPost]
public async Task<IHttpActionResult> PostNewEmployeeAsync(Employee emp)
{
var newEmployee = await RegisterEmployee(emp);
return Ok(emp);
// What if I had a database error in RegisterEmployee method. How do I detect the error and send InternalServerError()
}
private async Task<Employee> RegisterEmployee(Employee emp)
{
// Call DB to register new employee, then return Employee object
}
네, 그게 정확히 내가 뭘하고 있었는지는 잘 모르겠지만 웹 API 메소드에서 잡으려고 시도하는 것은 나쁜 생각입니다. – Sam
잘못하면 더 위험합니다. 따라서 착취를 막기 위해 자체 포함 예외 처리를 사용해야합니다. –