3

I just need to ask is octobercms has only capable of setting two types of errors ???

  1. 404 [by creating a page with /404 url ]

  2. 500 [by creating a page with /error url ]

how can we set 400, 401, 402, 403, 503, 400 etc ??????

Zakir hussain
  • 621
  • 3
  • 12

2 Answers2

5

Those 2 are special cases where author already wrote code in Cms Main Controller but for others we need to write code on our own :)

Hmm all are related to HttpException so this are the codes actually we set and throw like this,

App::abort(403, 'Access is forbidden to the requested page.' /* message */);

Now these all are HttpException, We can intercept them using App::error. You need to add this code to your plugin boot method.

//\App::error(function(\Exception $exception) { // for handling all Exceptions

// for handling http related exceptions
\App::error(function(
    \Symfony\Component\HttpKernel\Exception\HttpException $exception) {

    dd($exception->getStatusCode()); /* 403 */ // 400, 401, 402, 403, 503, 400 etc            
    // Handle the exception...
});

You can check status-code here and handle them as you like. Reference: https://octobercms.com/docs/services/error-log#http-exceptions

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
0

If anyone still has issues with passes status codes to the error page, simply add this to the boot method in Plugin.php

App::error(function(\Symfony\Component\HttpKernel\Exception\HttpException $exception) {
    $controller = new \Cms\Classes\Controller(\Cms\Classes\Theme::getActiveTheme());
    $controller->setStatusCode($exception->getStatusCode());
    return $controller->run('/error');
});

And check the status code on /error using

{% if this.controller.getStatusCode() == 403 %}