1
votes

I am using cakephp, I working on Error Handling

I have follow http://book.cakephp.org/1.3/en/view/1188/Error-Handling

I have create AppError My code is

app/app_error.php

<?php

    class AppError extends ErrorHandler {


        function error404() {
            //$this->controller->set('file', $params['file']);
            $this->_outputMessage('error404');
        }

    }
    ?>

I am calling this error404 from my controller

function userprofile($id = null) {
        $user = $this->Session->read('user');
        if($id != $user['User']['id'])
        {
            $this->cakeError('error404');
        }
}

but I found Erro Fatal error: Call to undefined method UsersController::cakeError() in D:\wamp\www\survey\app\Controller\UsersController.php on line 318

I miss some thing?

3

3 Answers

0
votes

I think you are using CakePHP 1.3 and extending ErrorHandler defined here :

....\cake\libs\error.php

and as per this error404 is defined as :

function error404($params) {
    extract($params, EXTR_OVERWRITE);

    if (!isset($url)) {
        $url = $this->controller->here;
    }
    $url = Router::normalize($url);
    $this->controller->header("HTTP/1.0 404 Not Found");
    $this->controller->set(array(
        'code' => '404',
        'name' => __('Not Found', true),
        'message' => h($url),
        'base' => $this->controller->base
    ));
    $this->_outputMessage('error404');
}

in your ..../app/app_error.php you have defined it as

function error404() {
        //$this->controller->set('file', $params['file']);
        $this->_outputMessage('error404');
}

and in php such type of method overloading is not allowed. I think you have got your answer.

what you can do is that you can create your own function in your extended AppError class and then pass error name or template name in that like

function customeError($errorMessage){
     $this->_outputMessage($errorMessage);
} 

and then on basis of different template names you can put conditions in _outputMessage() function and redirect user to different pages.

0
votes

Judging by the folder path returned in the error message, it looks like you're using CakePHP 2 whereas the documentation and example you have is CakePHP 1.3.

See error handling and exceptions in the 2.0 book (seriously, read them, there are tonnes of Exceptions available), I think it's little more complicated but a lot more flexible

0
votes

For CakePHP 2.X, Object::cakeError() has been removed. Instead it has been replaced with a number of exceptions. All of the core classes that previously called cakeError are now throwing exceptions. This lets you either choose to handle the errors in your application code, or let the built in exception handling deal with them.

There is more control than ever for error and exception handling in CakePHP 2.0. You can configure which methods you want to set as the default error handler, and exception handler using configure.

Here is the link of Error Configuration. You can easily achieve the same you needed.