[99s-extend] REST handler failure

Daniel Goertzen daniel.goertzen at gmail.com
Wed Oct 30 16:46:37 CET 2013


That's what I was looking for, thanks!

Dan.


On Wed, Oct 30, 2013 at 10:25 AM, Tilman Holschuh <tilman.holschuh at gmail.com
> wrote:

> Why not let resource_exists/2 return false when your resource does not
> exist? You will get 404 on false. This way you separate the implementation
> for turning your data to json from checking if the request went to the
> correct resource.
>
> - Tilman
>
> On 2013-10-30, at 7:58 AM, Daniel Goertzen wrote:
>
> > Well, this sort of works.  I tried this in the response handler:
> >
> >             {ok, Req2} = cowboy_req:reply(404, [] , <<"this is the body
> that gets used">>, Req1),
> >             {<<"this body gets ignored">>, Req2, State};
> >
> >
> >
> > The client receives a 404 response, but cowboy crashes:
> >
> > =ERROR REPORT==== 8-Sep-2013::22:22:03 ===
> > Error in process <0.131.0> with exit value:
> {function_clause,[{cowboy_req,reply,[200,[],<<31
> bytes>>,{http_req,#Port<0.1208>,ranch_tcp,keepalive,<0.131.0>,<<3
> bytes>>,'HTTP/1.1',{{192,168,1,187},51017},<<12 bytes>>,undefined,80,<<24
> bytes>>,undefined,<<0 bytes>>,undefined,[{channel_num,3}],[{<<10
> bytes>>,<<11 bytes>>},{<<4 bytes>>,<<12 bytes>>},{<<6 bytes>>,<<3
> bytes>>}],[{<<17 bytes>>,undefined},{<<13 bytes>>,undefined},{<<19
> bytes>>,undefined},{<<8 bytes>>,undefined},{<<6 bytes>>,[...
> >
> >
> >
> > The issue is that the REST wrapper wants to do the cowboy_req:reply(),
> and when we do the call we cause the wrapper's call to fail.
> >
> > Dan.
> >
> >
> >
> > On Tue, Oct 29, 2013 at 4:01 PM, Ivan uemlianin <ivan at llaisdy.com>
> wrote:
> > Sorry for terse but I only have a phone. Why can't you return a 404
> here?  Using something like cowboy:reply(404, ...
> >
> > Ivan
> >
> > --
> > festina lente
> >
> >
> > On 29 Oct 2013, at 21:25, Daniel Goertzen <daniel.goertzen at gmail.com>
> wrote:
> >
> >> My situation is that I have a rest handler that may fail due to invalid
> url segments.  Example situation:
> >>
> >>
> >> init(_Transport, _Req, _Opts) ->
> >>         {upgrade, protocol, cowboy_rest}.
> >>
> >> content_types_provided(Req, State) ->
> >>     {[{<<"application/json">>, get_json}], Req, State}.
> >>
> >> get_json(Req0, State) ->
> >>     {Params, Req1} = lists:mapfoldl(fun cowboy_req:binding/2, Req0,
> [param1, param2, param3, ....]),
> >>
> >>     case catch other_module:request(Params) of
> >>         {'EXIT', {badarg, _}} ->
> >>             hmmm, Params were bad and I would like to return a 404 code
> now.
> >>         Result ->
> >>             {jiffy:encode(Result), Req1, State}
> >>     end.
> >>
> >>
> >>
> >> So I would like to return a 404 code when my underlying request
> function fails, but it appears my choices are:
> >>
> >> - return a 200 (ok) response with data.
> >> - crash and cause a 500 (Internal Server Error) response to be
> returned.  Not exactly the sentiment I want.
> >>
> >>
> >> Is there some other way to cause a 404 response?
> >>
> >> I realize I could add path constraint functions, but I will be
> replicating logic from my underlying request function.  Furthermore, the
> constraint functions consider parameters in isolation, so that won't work
> if the validity of parameters is coupled.
> >>
> >> Thanks,
> >> Dan.
> >> _______________________________________________
> >> Extend mailing list
> >> Extend at lists.ninenines.eu
> >> http://lists.ninenines.eu:81/listinfo/extend
> >
> > _______________________________________________
> > Extend mailing list
> > Extend at lists.ninenines.eu
> > http://lists.ninenines.eu:81/listinfo/extend
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20131030/3ea4ac64/attachment.html>


More information about the Extend mailing list