@mythz' answer으로 개선되었습니다.
public override void Configure(Container container) {
//Handle Exceptions occurring in Services:
this.ServiceExceptionHandlers.Add((httpReq, request, exception) = > {
//log your exceptions here
...
//call default exception handler or prepare your own custom response
return DtoUtils.CreateErrorResponse(request, exception);
});
//Handle Unhandled Exceptions occurring outside of Services
//E.g. Exceptions during Request binding or in filters:
this.UncaughtExceptionHandlers.Add((req, res, operationName, ex) = > {
res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message));
#if !DEBUG
var message = "An unexpected error occurred."; // Because we don't want to expose our internal information to the outside world.
#else
var message = ex.Message;
#endif
res.WriteErrorToResponse(req, req.ContentType, operationName, message, ex, ex.ToStatusCode()); // Because we don't want to return a 200 status code on an unhandled exception.
});
}
나는 그것이 번거로울 것 같았습니다. 비 서비스 예외에 대한 HTTP 상태 코드를 수정하려는 경우 어떻게해야합니까? 각 Exception을 평가하고 HTTP 상태 코드를 설정하는 어딘가에 함수를 연결할 수 있습니까? –
당신은 HttpResponse를'res' 할 수 있으므로 원하는 응답 특성을 설정할 수 있습니다. 예외를 상태 코드에 매핑하는 방법은 오류 페이지를 참조하십시오. – mythz
좋아, 알았다. 도와 주셔서 다시 한 번 감사드립니다! –