Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch URI parse errors during routing #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Cro/HTTP/Router.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ module Cro::HTTP::Router {
my @*BIND-FAILS;
my $log-timeline-task = $request.annotations<log-timeline>;
my $routing-outcome = Cro::HTTP::LogTimeline::Route.log: $log-timeline-task, -> {
CATCH {
when X::Cro::Uri::ParseError {
emit Cro::HTTP::Response.new(:500status, :$request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be 400 (Bad Request), since it's the client's fault.

next;
}
}
$request.path ~~ $!path-matcher
}
with $routing-outcome {
Expand Down
6 changes: 6 additions & 0 deletions t/http-router.t
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ throws-like { bad-request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'bad-r
ok $r ~~ Cro::HTTP::Response, 'No matching route gets a HTTP response';
is $r.status, '404', 'Status code when no matching route is 404';
}

$source.emit(Cro::HTTP::Request.new(:method<GET>, :target<example[1].jpg>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'No matching route gets a HTTP response';
is $r.status, '500', 'Status code uri is invalid is 500';
}
}

{
Expand Down