gpt4 book ai didi

c# - NancyFX - 对未捕获的异常返回自定义错误响应

转载 作者:太空狗 更新时间:2023-10-29 23:06:53 27 4
gpt4 key购买 nike

我正在尝试使用 Nancy 让我的自托管服务在出现未捕获的异常时返回 json 格式的错误。然而,我总是得到回应:

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

(以下是网络上几个例子的合并)。

我的 Bootstrap 包含以下内容:

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

我有一个 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;
}

尽管我已经验证了 OnErrorHandle 中的代码已执行(请参阅评论),但我的客户仍然收到 404。我也尝试过使用

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

代替

        var exception = context.GetException();

没有运气。

最佳答案

啊,所以这是一个 CORS 问题。

我自动将 CORS header 添加到响应中:

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

但是当响应在我的状态代码处理程序中被替换时,我需要再次设置这些 header :

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# - NancyFX - 对未捕获的异常返回自定义错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28487961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com