[99s-extend] REST handler failure

Daniel Goertzen daniel.goertzen at gmail.com
Tue Oct 29 21:25:54 CET 2013


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20131029/a9204600/attachment.html>


More information about the Extend mailing list