How to handle a Bad Request

43 views
Skip to first unread message

Nicholas Smith

unread,
Aug 1, 2018, 11:43:07 AM8/1/18
to ninja-framework
After reading documentation, it seems I should be able to do this from my controller methods.

// If user is not authorized to perform this action:
throw new ninja.exceptions.BadRequestException("You do not have permission to delete this record.");


I would have expected this to return the "views/system/403forbidden.ftl.html" template. 
:(
Oops. That's a bad request and all we know.
Back to homepage

<#import "../layout/defaultLayout.ftl.html" as layout>
<@layout.myLayout "Error. Forbidden.">
<div class="container">
   
<div class="row">
       
<div class="col-md-12">
           
<div class="error-template">
               
<h1>
                    Oops!
</h1>
               
<h2>
                    403 Forbidden
</h2>
               
<div class="error-details">
                    Sorry, an error has occured. Requested page is forbidden!
               
</div>
           
</div>
       
</div>
   
</div>
</div>
</@layout.myLayout>


The actual page being returned is:

:(
Oops. That's a bad request and all we know.
Back to homepage

How can I get it to actually use the template for the 403 Forbidden result? And, is there a way in that template to display the message contained in the exception?  I would like to show "You do not have permission to delete this record.", or whatever message we include from the controller.




Nicholas Smith

unread,
Aug 1, 2018, 11:44:37 AM8/1/18
to ninja-framework
Sorry for the typo, I included the actual rendered result twice, but the code shows what's contained in the forbidden template, and that's what I would expect to see rendered to the user.

Jonathan Lannoy

unread,
Aug 31, 2018, 9:05:42 PM8/31/18
to ninja-framework
Sorry I did not understood you request the first time I read it... Now it's clear, it's just that the BadRequestException is not the 403 forbidden code... But the 400 (bad request). 

You can override the onException() method of your conf.Ninja to grab the exception text message instead of the default one.
Reply all
Reply to author
Forward
0 new messages