2015-02-12 17 views
5

Tôi đang cố gắng sử dụng dịch vụ tự lưu trữ của mình bằng cách sử dụng Nancy để trả về lỗi định dạng json khi ngoại lệ chưa được thực hiện. Tuy nhiên, tôi luôn nhận được phản hồi:NancyFX - Trả về phản hồi lỗi tùy chỉnh về ngoại lệ không bắt buộc

{"readyState":4,"status":404,"statusText":"error"} 

(bên dưới là sự kết hợp của một số ví dụ trên mạng).

bootstrapper My chứa sau:

 pipelines.OnError.AddItemToEndOfPipeline((ctx, exc) => 
     { 
      if (exc is Exception) 
      { 
       // this is always executed upon failure to handle an exception. 

       Log.Error("Unhandled error on request: " + context.Request.Url + " : " + exc.Message, exc); 

       JsonResponse response = new JsonResponse(string.Format("{0}:{1}", exc, exc.Message), new DefaultJsonSerializer()); 
       response.StatusCode = HttpStatusCode.InternalServerError; 

       return response; 
      } 

      return HttpStatusCode.InternalServerError; 
     }); 

Tôi có một StatusCodeHandler:

public class JsonErrorStatusCodeHandler : IStatusCodeHandler 
{ 
    public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) 
    { 
     return statusCode == HttpStatusCode.InternalServerError; 
    } 


    public void Handle(HttpStatusCode statusCode, NancyContext context) 
    { 
     var exception = context.GetException(); 

     if (exception != null) 
     { 
      // never executed 
     } 

     // this is executed 

     JsonResponse response = new JsonResponse("wtf"), new DefaultJsonSerializer()); 
     response.StatusCode = HttpStatusCode.InternalServerError; 

     context.Response = response; 
    } 

Mặc dù tôi đã xác minh rằng mã trong OnErrorHandle được thực hiện (xem chú thích), khách hàng của tôi vẫn nhận được 404. Tôi cũng đã thử sử dụng

 var exception = context.Items[NancyEngine.ERROR_EXCEPTION] as Exception; 

thay vì

 var exception = context.GetException(); 

không có may mắn.

Trả lời

6

Rất tiếc, vì vậy đây là vấn đề về CORS.

Tôi đang tự động thêm các tiêu đề CORS để phản ứng:

protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) 
    { 
     pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => 
     { 
      ctx.Response.WithHeader("Access-Control-Allow-Origin", "*") 
       .WithHeader("Access-Control-Allow-Methods", "POST,GET") 
       .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"); 
     }); 

     pipelines.OnError.AddItemToEndOfPipeline((ctx, exc) => 
     { 
      if (exc != null) 
      { 
       throw exc; 
      } 

      return HttpStatusCode.InternalServerError; 
     }); 

     base.RequestStartup(container, pipelines, context); 
    } 

Nhưng khi phản ứng là thay trong xử lý mã trạng thái của tôi, tôi cần phải thiết lập các tiêu đề một lần nữa:

public class JsonErrorStatusCodeHandler : IStatusCodeHandler 
{ 
    public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) 
    { 
     if (statusCode != HttpStatusCode.InternalServerError) 
     { 
      return false; 
     } 

     var exception = context.GetException(); 

     return exception != null; 
    } 


    public void Handle(HttpStatusCode statusCode, NancyContext context) 
    { 
     var exception = context.GetException(); 

     JsonResponse response = new JsonResponse(string.Format("{0}:{1}", exception, exception.Message), new DefaultJsonSerializer()); 

     response.StatusCode = HttpStatusCode.InternalServerError; 

     context.Response = response; 

     context.Response.WithHeader("Access-Control-Allow-Origin", "*") 
      .WithHeader("Access-Control-Allow-Methods", "POST,GET") 
      .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"); 
    } 
} 
Các vấn đề liên quan