Thank you, much appreciated, your response helped.

Here's what I've learned....

1) In the Mojo callback function (/price route), invoking 
Mojo::Exception->throw(message) triggers the rendering of the 
exception.development.html.ep template from the __DATA__ section.  In the 
template, the thrown message can be accessed as <%= $exception->message %>.
2) The rendered exception html (with the thrown message) is returned (with 
a 500 response code) to the javascript / jquery script, caught by the.ajax. 
fail handler, and accessable in the jqXHR object as jqXHR.responseText
3) The .ajax.fail handler can output the jcXHR.responseText to a message 
area on that same page and/or display a new page.

In hindsight, this is normal ajax behavior.

Yes, I agree that the logic needs to distinguish between a server error and 
an exception intentionally thrown in a mojo callback.

A more general question, is this a good way to handle exceptions caught in 
mojo callback functions?   An alternative would be to not throw an 
exception and instead, render error data that gets returned to the .ajax 
.done handler (200 response code).  The .done handler is then responsible 
to detect the error values and display an error message to the user.  This 
would not generate the 500 response code which could still be thrown by the 
http server.

Still have more work to do... but am making progress, improving my 
understanding...


On Wednesday, March 23, 2016 at 7:44:56 AM UTC-4, Heiko Jansen wrote:
>
> Throwing the exception leads to a response code "500" for the ajax 
> request; since code 500 signals an error the "fail" handler for the ajax 
> request get´s triggered.
>
> That of course does not mean that the content returned from the mojo 
> server is rendered in your page; it was an ajax request after all, so 
> rendering any content in the browser would be the responsibility of your 
> javascript code in myapp_fail.
>
> By the way: blindly rendering the response from the server to your user 
> imho does not make much sense: something unexpected may have gone wrong 
> server side so you cannot be sure what kind of error you got and if it was 
> one you triggered on purpose like in your code show above.
>
> If you add "location.href='/exception';" in the myapp_fail function then 
> this happens:
> 0) You make your ajax request to the server
> 1) the exception template is rendered because of your "throw" 
> 2) your server sends a code 500 response with the content from the 
> exception template
> 3) your javascript library receives a response with a status code 
> indicating a server side processing error and calls the fail handler
> 4) your ajax fail handler receives the rendered output of the exception 
> template and does something with it (or not)
> 5) you modify  "location.href"
> 6) the browser now makes a regular (non-ajax) request to the new URL 
> in location.href which happens to be "/exception"
> 7) your server receives the request
> 8) something goes wrong and the exception template is rendered because of 
> some internal error
> 9) the browser receives the output and displays it
>
> Of course the response your browser receives in step 9 is in no way 
> connected to the exception you threw in step 0.
>
> After step 7 I´m not completely sure what happens; apparently you did 
> create a callback for the route "/exception" (at least I believe so, 
> because of the "in /exception" debug log message) but I don´t think that 
> there´s any automatic connection between a route of that name and the 
> internal exeception handling template. 
> That´s probably the reason why the server generates a "501 Not 
> Implemented" response.
>
> In the end I believe the problem is less with using mojolicious but more 
> so with some lack of understanding how ajax works and about the 
> statelessnes of HTTP.
>
> Hope I could point you in the right direction
> - heiko
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to