piyush
piyush

Reputation: 976

Show Custom Error Message in Codeigniter

I am running a php codeigniter based web application. I wanted to show custom error pages and custom error messages in my application.

So I extented the codeigniter's core Exception class with my custom exception class. I added a file MY_Exceptions.php in application/core folder.

It seems like the show_error method is overwritten but the show_php_error is not overwritten by my custom class. It is still executing the show_php_error method from system/core/Exceptions.php instead of application/core/MY_Exceptions.php (my custom class).

The MY_Exceptions.php file is as below -

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions
{
public function __construct()
{
    parent::__construct();
}

function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
    if($template!='error_404'){
        $template = 'custom_error_page';
        $heading = "An Error Occured";
        $message = "We're sorry, but we can not complete the action you requested. A notification has been sent to our customer service team. Please try again later.";
    }else{
        $template = 'custom_error_page';
        $heading = "404 Page Not Found";
        $message = "We're sorry, but we can not load the page you requested. A notification has been sent to our customer service team. Please try again later..";
    }
    set_status_header($status_code);

    $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

    if (ob_get_level() > $this->ob_level + 1)
    {
        ob_end_flush();
    }
    ob_start();
    include(APPPATH.'errors/'.$template.'.php');
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}

function show_php_error($severity, $message, $filepath, $line)
{
    $heading = "An Error Occured";
    $message = " We're sorry, but we can not complete the action you requested. A notification has been sent to our customer service team. Please try again later.";
    echo $this->show_error($heading, $message, 'custom_error_php', 404);
    exit;
}

}

Upvotes: 0

Views: 4182

Answers (1)

Laurence
Laurence

Reputation: 60048

Why dont you just changed the "error_404.php" and "error_general.php" files in /application/errors?

That way you can style the pages however you want, and put your generic error messages there, rather than extending a core class?

Upvotes: 1

Related Questions