Skip to content
This repository was archived by the owner on Jan 24, 2021. It is now read-only.

Commit bdc5cf4

Browse files
committed
Merge pull request #1668 from khellang/single-status-code-handler
Break when status code has been handled
2 parents eca21b4 + 7bac92f commit bdc5cf4

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Diff for: src/Nancy/NancyEngine.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,23 @@ private void CheckStatusCodeHandler(NancyContext context)
197197
return;
198198
}
199199

200-
foreach (var statusCodeHandler in this.statusCodeHandlers)
200+
var handlers = this.statusCodeHandlers
201+
.Where(x => x.HandlesStatusCode(context.Response.StatusCode, context))
202+
.ToList();
203+
204+
var defaultHandler = handlers
205+
.FirstOrDefault(x => x is DefaultStatusCodeHandler);
206+
207+
var customHandler = handlers
208+
.FirstOrDefault(x => !(x is DefaultStatusCodeHandler));
209+
210+
var handler = customHandler ?? defaultHandler;
211+
if (handler == null)
201212
{
202-
if (statusCodeHandler.HandlesStatusCode(context.Response.StatusCode, context))
203-
{
204-
statusCodeHandler.Handle(context.Response.StatusCode, context);
205-
}
213+
return;
206214
}
215+
216+
handler.Handle(context.Response.StatusCode, context);
207217
}
208218

209219
private Task<NancyContext> InvokeRequestLifeCycle(NancyContext context, CancellationToken cancellationToken, IPipelines pipelines)

0 commit comments

Comments
 (0)