403 Error page not showing in Symfony - symfony1

I am using Symfony 1.4 for onr of my project and it is needed to show a custom 403 error page. I have given the following in settings.yml
.actions:
error_404_module: errors
error_404_action: error404
error_500_module: errors
error_500_action: error500
error_403_module: errors
error_403_action: error403
I also have the error modules and corresponding error success pages. But it is not showing the custom error pages. Instaed in some cases for ex: 404 it is showing a debug page . 403 page is returned with a default forbidden error message. My action file is like the following:
class errorsActions extends sfActions
{
/**
* Executes index action
*
* #param sfRequest $request A request object
*/
public function executeIndex(sfWebRequest $request)
{
//$this->forward('default', 'module');
}
public function executeError404(sfWebRequest $request)
{
}
public function executeError500(sfWebRequest $request)
{
}
public function executeError403(sfWebRequest $request)
{
}
}
EDIT : Adding settings.yml modifications
.actions:
error_404_module: errors
error_404_action: error404
error_500_module: errors
error_500_action: error500
secure_module: errors
secure_action: error403

In your case the settings.yml should be:
all:
.actions:
# page not found
error_404_module: errors # module "errors"
error_404_action: error404 # action "executeError404"
# 403 credential required
secure_module: errors # module "errors"
secure_action: error403 # action "executeError403"
For the 500 error page, create a directory error in your app/appname/config directory and put a error.html.php file in it (a complete html page).
About the 404 error page, this page (like the 500 error page) is overwritten in the dev environment with the debug page but it will show correctly in the prod environment.

That is not the correct way to override the 403 error pages. Use secure_module and secure_action.

you probably need to unsecure the 404 action:
see here: http://symfony-check.org/permalink/customize-the-oops-page-not-found-page

Related

symfony3 controller file gives a 404 error

install symfony3 on a wamp server was fine using composer
On the http://localhost:9080/SymfonyApp/web/
i get the welcome page, thats fine
Created todocontroller.php as shown below in C:\wamp\www\symfonyApp\src\AppBundle\Controller
#
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class TodoController extends Controller
{
/**
* #Route("/todos", name="todo_list")
*/
public function indexAction(Request $request)
{
die('TODOS');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' =>
realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
]);
}
}
#
when i go to http://localhost:9080/SymfonyApp/web/todos
I should see TODOS on the browser but instead i get a 404 error
Can anybody please tell me what is wrong with the route?

Grails databinding and allowed HTTP methods

I have the following action in a controller
class RegisterController {
static allowedMethods = [register: 'POST']
def register(User user) {
// action body omitted
}
}
If a user tries to invoke this action via GET /register/register/newUser, I get a databinding failure because newUser cannot be bound to the user's Long id property.
This seems reasonable at first, however IMO no attempt should ever be made to bind the user when it is invoked via HTTP GET, because I've declared that only POST is allowed.
Curiously, if I change the action to:
class RegisterController {
static allowedMethods = [register: 'POST']
def register() {
User user = new User(params)
}
}
and again try to invoke it with GET /register/register/newUser, then I get the expected HTTP method not allowed (405) error. It seems to me that databinding is happening before the HTTP request type is checked, and this is why I get a binding error in the first case and a 405 error in the second.
Shouldn't I get a 405 in both cases?
Yes, you should get a 405 in both cases. File a report at https://jira.grails.org/browse/GRAILS and we will get it straightened out. We have an AST transformation which adds the command object handling code and adds the allowedMethods handling code. It sounds like they may not be in the right order. Will get it straightened out. Thanks for the feedback.

RediretAction in MVC causes "The Web server is configured to not list the contents of this directory"

I have a contentController like this :
public class ContentController : Controller
{
public ActionResult Index()
{
var model = obj.GetContentlist();
return View(model);
}
[HttpPost]
public ActionResult Create(Content content)
{
obj.AddNewContent(content);
obj.Save();
return RedirectToAction("Index","content");
}
}
That as you can see contains create action .But in the last line ,when it is going to be executed RedirectToAction("Index","content"); i got this error :
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
Most likely causes:
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.
The default MVC template includes a "Content" folder. Since you're redirecting to content/index and index is most likely the default action, you actually get redirected to /content -- which is the content directory rather than your controller. If I were you I would choose another name for your controller to avoid the conflict.

asp.net mvc handle http 400 error

I have an ASP.NET MVC 4 application and I want to redirect all HTTP 400 errors to my custom error page. I was searching an hours to find a solution though HTTP 400 error isn't handled like 404 error. There are many solutions that show how to escape 400(bad request error), i.e. to allow using special characters for example in url. But I wouldn't able to find some solution to catch the exception.
Please help me to catch somehow all HTTP bad requests and redirect them to my error page.
Never redirect users in case of errors, instead return a response body for the failed request. The feature of IIS (and ASP.NET) to redirect to an error page, I believe, is fundamentally wrong, incorrect, and against the HTTP specification (because then the error is being returned for the error page resource itself, not the original request. And if it's a web-browser the user has no way of retrying, because reloading the page will return the error page again, not retrying their original failed request, which is what they want).
Anyway...
A HTTP 400 response must be generated by your application code, it isn't something that will be done automatically. A bad request is typically used when informing non-human agents (i.e. web service clients, not web browsers) that their HTTP request was missing required values or had malformed values.
You can do this in MVC by having a base controller class for all of your controllers like so:
public abstract class BaseController : Controller {
protected ActionResult Http400(String message) {
Response.StatusCode = 400;
return View(message); // you need to define a view file called "Http400.aspx" (or cshtml if you're using Razor) in your application's shared views folder
}
}
so in your application logic:
public ActionResult Foobar() {
if( IsBadRequest() ) return Http400("Bad request, try again");
}
You could do something as simple as adding adding this to your web.config
<customErrors mode="RemoteOnly">
<error statusCode="400" redirect="errorpage.html"/>
</customErrors>

WebAPI Controller error Handling

I have a issue in implementing Error handling in Web-APi
[HttpGet]
public UserAccount Get()
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("My Error Message"),
ReasonPhrase = "Critical Exception"
});
}
When I tried to call this method from my machine(site is hosted locally), it is working as expected. i.e I got the error message in response (plain text). But when I access the same url from another machine(LAN connected), response is IIS 500 internal error page. Here my problem is that, I am not getting the custom error message "My Error Message"
Any suggestions?
Add this to your web config:
<system.webServer>
<httpErrors errorMode="Detailed"/>

Resources