2013-03-24 26 views
6

私はServiceStack RESTサービスを持っており、カスタムエラー処理を実装する必要があります。 AppHostBase.ServiceExceptionHandlerをカスタム関数に設定することで、サービスエラーをカスタマイズすることができました。ServiceStack RESTサービスのカスタム例外処理

ただし、検証エラーなどの他の種類のエラーの場合、これは機能しません。すべてのケースをどのようにカバーできますか?非サービス・エラー(検証)

  • 含めて、ポップアップ表示可能性がある例外の種類ごとに自分のHTTPステータスコードを設定し

    1. :つまり

      は、私は2つのことを達成しようとしていますすべてのエラータイプに対して自分のカスタムエラーオブジェクト(デフォルトのResponseStatusではなく)を返します

    これを達成するにはどうすればいいですか?

  • 答えて

    11

    AppHostBase.ServiceExceptionHandlerグローバルハンドラは、のサービス例外のみを処理します。あなたがaccess and use the correct serializer for the request from IAppHost.ContentTypeFiltersに必要となる非サービスExceptionHandlerで応答ストリームにDTOを作成し、シリアライズするには

    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); 
        }; 
    } 
    

    :あなたがグローバルAppHostBase.ExceptionHandlerハンドラ、例えばを設定することができ、サービスの外で発生した例外を処理する 。

    詳細は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の最新バージョンを反映しています。 –