croservices / cro

Development tools for building services and distributed systems in Raku using the Cro libraries.
https://cro.services/
Artistic License 2.0
88 stars 35 forks source link

running Cro::HTTP::Request example fails #29

Closed tbrowder closed 7 years ago

tbrowder commented 7 years ago

In "http://cro.services/docs/reference/cro-http-router" there is a section named "Accessing the Cro::HTTP::Request instance" with this example:

my $app = route {
    get -> {
        say "Request headers:";
        for request.headers {
            say "{.name}: {.value}";
        }
    }
}

When I try to use it I get the following exception:

Request headers:
  Host: localhost
  Connection: close
Cannot unbox a type object (Cro::HTTP::Response::StatusCode) to int.
  in block  at /usr/local/rakudo.d/share/perl6/site/sources/9E898BFBAC00FE9363AE6D27527AD02192D19DDD (Cro::HTTP::ResponseSerializer) line 13
  in block  at /usr/local/rakudo.d/share/perl6/site/sources/88931D797A7005CE2FD56A6EF6A37BBD2E8D6CFA (Cro::HTTP::Internal) line 45
  in block  at /usr/local/rakudo.d/share/perl6/site/sources/1DC86B9F9A31CA5BEE35765E177159BAEDB91AAA (Cro::HTTP::Router) line 82

System: Debian 8, 64-bit.

$ perl6 -v This is Rakudo version 2017.08-110-g5f33506 built on MoarVM version 2017.08.1-156-g49b90b9 implementing Perl 6.c.

Complete code is at gist: https://gist.github.com/tbrowder/fc5d4c628561451a9c9ea0ef1f7a2a93

Altai-man commented 7 years ago

Well, it is not precisely a bug, more of a bad error message and a bit of incomplete docs. The problem is that your route doesn't serve any content, hence the error. You can try it as

        say "Request headers:";
        for request.headers {
            say "  {.name}: {.value}";
        }
        content 'plain/text', 'Response';

and it will work.

I will look into prettier error message now.

Altai-man commented 7 years ago

Made a better error with https://github.com/croservices/cro-http/commit/d6ff33c5a81400e37d1be73a2243db146748835a

tbrowder commented 7 years ago

Thank you! I admit my knowledge of http client/server architecture is very weak. But CRO seems to be the best and easiest to use I've seen!

Altai-man commented 7 years ago

Oh, no problem with that. Thanks a lot for your words!

I have added a response line into the example, to prevent this kind of confusion. Can the issue be closed?

jnthn commented 7 years ago

Perhaps we should just make the default response be a 204 (No Content), which would be a reasonable default when implementing PUT and DELETE in REST APIs, but also would have made examples like the one we see here work sufficiently well.

tbrowder commented 7 years ago

On Mon, Sep 25, 2017 at 15:11 Altai-man notifications@github.com wrote:

Oh, no problem with that. Thanks a lot for your words!

I have added a response line into the example, to prevent this kind of confusion. Can the issue be closed?

Yes, thanks.

Altai-man commented 7 years ago

@jnthn we can do it like so, though I dunno is it common enough. If you're suggesting it, we may go with another issue for this case.

jnthn commented 7 years ago

PUT and DELETE with a 204 response are common enough when doing a REST API implementation. So I think it's probably a sensible default. Let's go with it.

Altai-man commented 7 years ago

Done. Closing~