<div dir="ltr"><div class="gmail_extra"><span style="font-family:arial,sans-serif;font-size:13px">Hi, Loïc.</span><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
Thanks for having taken the time to reply. In some of my questions I think I didn't explain myself correctly so I'll give it another go.<br><div class="gmail_extra"><br><br><div class="gmail_quote"><div class="im">
On 20 May 2014 19:46, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Hi,<div><br><br>On 05/20/2014 06:27 PM, Paulo F. Oliveira wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Hello.<br><br>First of all, thanks for the great work you've done with cowboy. I've<br>been using it with a fait amount of success and I'm a fairly new Erlang<br>developer. I'm mainly interested in the REST "interface" of the<br>
application and its way of doing RESTful things, and I like the way you<br>did it (what with all the content_types_provided, service_available,<br>etc. functions). I've tested the way the system reacted to the different<br>
Accept, Content-Type, etc. headers and always got very well-opinionated<br>responses (406, 415, ...).<br><br>A couple of questions remain though (I'm sorry if they've been asked<br>already but I've searched the web for answers and read the available<br>
docs and couldn't find them):<br><br>1. is it expected that, if I use cowboy_req:reply/2 in a GET handler<br>(coming from content_types_provided), the onresponse/4 hook be called<br>twice? I guess one is due to the reply and the other one due to the<br>
workflow of the request, but is there a way to prevent the second execution?<br></blockquote><br></div>If you reply from a callback you must call {halt, Req, State} to stop processing.</blockquote><div><br></div></div><div>
Got it!</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
2. if I want to JSON-parse ALL my requests should I a) use the<br>onrequest/1 hook or b) do this on a per-request basis? Because I'd like<br>to reply with a 400 ASAP but keep going if the JSON validates (I'm going<br>
to use JSON-schema for validating input); and, if possible, have the<br>JSON-parsed body stored somewhere for future manipulation.<br></blockquote><br></div>It makes little sense to do it before the accept callback you define. Not only because you will duplicate content-type checks and whatnot, but also because you don't actually win anything from doing this. If you are using JSON, then JSON processing will take infinitely more resources than the REST code.</blockquote>
<div><br></div></div><div>OK, I'll probably stick with a "helper" function that'll do this for me and reply in case there are validation errors.</div><div>I only found the flow diagrams for the requests today after I had sent this message, and they helped a lot.</div>
<div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
3. I haven't seen examples that made use of the State (from the function<br>returns). When should I use this instead of the Request metadata? I'd<br>like to be able to set a generic error state for a request (either in<br>
meta ou State) and that have a "standard" error response be created at a<br>later time (in a unique function, for example - e.g. onresponse/4).<br></blockquote><br></div>State is for the functions within the current module. Look at cowboy_static for an example.</blockquote>
<div><br></div></div><div>State allows me to, well, keep state, for a request "travelling" through functions, right, and I can change it whenever I want just before returning from a function that is executed prior to another one (the only function for which this doesn't seem to make since is the last one cowboy calls before actually replying to the client)? At the same time, so does the request meta, from what I understood from the manual. So what is the difference between one and the other and when would you recommend one or the other.</div>
<div class="im"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
4. is there anything like a catch-all exception handler? I'd like to<br>catch exceptions that occur anywhere so I could log them and analyze<br>them at a later moment.<br></blockquote><br></div>You can add your own error_logger handler, or use something like lager. All errors end up sending a message to error_logger.</blockquote>
<div><br></div></div><div>I'll do this, thanks.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">I'm probably abusing the onresponse/onrequest hooks already, so your<br>
answers should help me clarify this.<br></blockquote><br></div>Sounds like it!<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Thanks.<br><br>- Paulo<br><br><br>______________________________<u></u>_________________<br>Extend mailing list<br><a href="mailto:Extend@lists.ninenines.eu" target="_blank">Extend@lists.ninenines.eu</a><br><a href="https://lists.ninenines.eu/listinfo/extend" target="_blank">https://lists.ninenines.eu/<u></u>listinfo/extend</a><br>
<br><font color="#888888"></font></blockquote><font color="#888888"><br>-- <br>Loïc Hoguin<br><a href="http://ninenines.eu/" target="_blank">http://ninenines.eu</a><br></font></blockquote></div></div><br></div></div><div class="gmail_extra" style="font-family:arial,sans-serif;font-size:13px">
Thanks.</div></div></div>