2013-03-24 3 views
6

ServiceStack REST 서비스가 있으며 사용자 정의 오류 처리를 구현해야합니다. AppHostBase.ServiceExceptionHandler를 사용자 지정 함수로 설정하여 서비스 오류를 사용자 지정할 수있었습니다.ServiceStack REST 서비스의 사용자 정의 예외 처리

그러나 유효성 검사 오류와 같은 다른 유형의 오류의 경우에는 작동하지 않습니다. 모든 케이스를 어떻게 커버 할 수 있습니까? 비 서비스 오류 (검증)

  • 을 포함하여, 팝업 수있는 예외의 모든 종류에 대한 내 자신의 HTTP 상태 코드를 설정

    1. : 즉

      , 나는 두 가지를 달성하기 위해 노력하고있어 모든 오류 유형에 대한 내 자신의 사용자 정의 오류 객체 (기본 ResponseStatus가 아님)를 반환하십시오.

    어떻게해야합니까?

  • 답변

    11

    AppHostBase.ServiceExceptionHandler 글로벌 처리기 만 서비스 예외 만 처리합니다. 는 예컨대 전역 AppHostBase.ExceptionHandler 핸들러를 설정할 수 있습니다 서비스의 외부, 발생하는 예외 처리하려면 :

    public override void Configure(Container container) 
    { 
        //Handle Exceptions occurring in Services: 
        this.ServiceExceptionHandler = (request, exception) => { 
    
         //log your exceptions here 
         ... 
    
         //call default exception handler or prepare your own custom response 
         return DtoUtils.HandleException(this, request, exception); 
        }; 
    
        //Handle Unhandled Exceptions occurring outside of Services, 
        //E.g. in Request binding or filters: 
        this.ExceptionHandler = (req, res, operationName, ex) => { 
         res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message)); 
         res.EndServiceStackRequest(skipHeaders: true); 
        }; 
    } 
    

    작성을하고 access and use the correct serializer for the request from IAppHost.ContentTypeFilters해야ExceptionHandler비 서비스 응답 스트림에 DTO를 직렬화.

    자세한 내용은 Error Handling wiki page에 있습니다.

    +0

    나는 그것이 번거로울 것 같았습니다. 비 서비스 예외에 대한 HTTP 상태 코드를 수정하려는 경우 어떻게해야합니까? 각 Exception을 평가하고 HTTP 상태 코드를 설정하는 어딘가에 함수를 연결할 수 있습니까? –

    +0

    당신은 HttpResponse를'res' 할 수 있으므로 원하는 응답 특성을 설정할 수 있습니다. 예외를 상태 코드에 매핑하는 방법은 오류 페이지를 참조하십시오. – mythz

    +0

    좋아, 알았다. 도와 주셔서 다시 한 번 감사드립니다! –

    4

    @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. 
        }); 
    } 
    
    +0

    이것은 ServiceStack의 최신 버전을 반영합니다. –

    관련 문제