How do I override the not found page in CodeIgniter? - php

I have a CodeIgniter application that's generally working how I'd like it to, but occasionally a user will go to a page that does not exist and is greeted with an unfriendly error. I'd like to detect the error automatically and display useful information to the user (not PHP errors). I read the user guide of CodeIgniter, but I couldn't find any relevant section.
How do I handle a page-not-found error in CodeIgniter and display my own custom content?

If you're looking at handling errors with your own custom page, you can modify the error templates found in application/errors. If you have a reason to based on your own code, you can manually send the user to one of these pages using show_404 or show_error - check out the Error Handling page in the official docs.

Try these codeigniter functions
show_404('Your error message');
show_error('Your error message');
you can find more detail at http://codeigniter.com/user_guide/general/errors.html
example:
if ($some_error) //condition
{
show_error('Error');
}

You should test for error return values and catch exceptions. This is a general programming concept - not something specific to Conigniter or PHP.
Testing for error return values:
if (!sort($array))
{
echo "Could not sort $array.";
}
Catching exceptions:
try
{
$someFunction($data);
}
catch (Exception $e)
{
echo "Something went wrong";
}
Of course write useful error messages with pertinent info that helps the user find their problem, and/or helps you fix your bug. You could get advanced and use something like set_error_handler():
http://php.net/manual/en/function.set-error-handler.php
I found this interesting article:
http://www.derekallard.com/blog/post/error-handling-in-codeigniter/
I'm not sure it reflects the current CI release as it's from 2007.

Related

catch exceptions from yodlee api

Currently I'm working with the yodlee API. As specified in the documentation the url response may throw InvalidCredentails or UserAccountLocked. I'm using PHP and I get the following response when the username or password is incorrect.
{
Error: [
{
errorDetail: "Invalid Cobrand Credentials"
}
]
}
So to check if the error occurs I want to write some code that checks if errorDetail has a value of Invalid Cobrand Credentials.
So far ok.
But the there may be so many types of errors, and each error name is different. My question is: Can I get the list of these errorDetail values
so that I can make it work without checking if the code is forcebly throwing the errors.
You can just check for the Error index and access it's value for throwing the errors. Something like this should work for you (not tested). From the repo page:
yodleeAPI.getAccounts(accessToken)
.then(function(response) {})
.catch(function(error) {});
Edit:
As far as I could tell there wasn't any exception list. So you're either going to have to go through all the exceptions manually or create a generic error message for users. I would just advice to catch the exception message and use that for the user view(if there isn't any security information in there). You can accomplish that by following the above code.
If you do feel the need to go through every exception yourself I managed to at least get the list of all methods that throw exceptions (search: exception). You'll have to go through it yourself, and parse the error message yourself. But you can find that here

Handling facebook exceptions

maybe it is simple, but i do not know how to do it, so i ask.
Sometimes i get an exception from facebook e.g. posting an action has failed. Normally it works but sometimes dont. I use the php sdk and when there is sth. wrong i get an facebook exception and the whole site stops running.
Is there a way i can ignore these exceptions and store it in database instead, so that the rest of the website works?
I think it is in the base_facebook.php, hopefully someone knows what line i must change.
Thank you very much
Ruven
You don't need to touch the Facebook SDK. You can catch the exceptions and do whatever you like with them (such as storing them in a database).
Example:
try {
// Facebook code here
} catch (Exception $e) {
// Display the exception (or whatever else you like)
echo $e->getType()." ".$e->getMessage();
}

Yii - PHP error after content saying that LogDetailFilter couldn’t be found

Ok, I can't figure this one out for the life of me. I'm using Yii 1.1.8.
If I do this:
function actionEdit()
{
$this->render('//user/edit');
}
I get a PHP error after the content saying that LogDetailFilter couldn’t be found when it was trying to autoload it in Yiibase.php on line 421.
But if I do this:
function actionEdit()
{
$this->render('//user/edit');
die;
}
I have no problems. Any thoughts how I could fix this?
It's an error in your config file related to what / how you are logging things. Yii runs all of the Yii::log / Yii::trace calls after it has rendered content (using the onApplicationEnd event), which is why using die prevents your logging from having errors.
If you post your logging info, we can point out the exact spot if you need it

change php error output to custom message with link

How do I change the error output of a php error? For example if its a syntax error, or a server side time out, i want to echo a message that allows the user to refresh the page.
Heres the code I want to add my error message to:
$XML->registerXPathNamespace('tree','www.tree.com'); <--occasionally errors here, so I want to output my own error message.
Use the function set_error_handler to define a custom function to be called when there is an error. You can then decide to do whatever you want within that function with the error.
If you only want it for a specific duration, you can restore it afterwards with restore_error_handler.
set_error_handler('yourHandler');
...
$XML->registerXPathNamespace('tree','www.tree.com');
...
restore_error_handler();
function yourHandler(int $errno , string $errstr) {
//show link to refresh page, whatever. full signature can be found on PHP manual page
}
Well, quick check in php manual for registerXPathNamespace shows that it returns TURE or FALSE. Which is perfect for my solution.
if (!#$XML->registerXPathNamespace('tree','www.tree.com')) {
echo '<b>ERROR:</b> Could not register xml path. Please reload the page!';
}
Not that, if it returns FALSE, then the message will be displayed. And the # in front of $XML will disable the original errors that, that action may cause.

PHP using exit()

I am using Cakephp but this is a MVC/php doubt
letting the view display the message
vs
echo 'Invalid Data'; exit;
I would like to know is there any pitfalls in the second case like memory leak etc.. Which one is better
EDIT
In case of a ajax call is exit good. and what about memory leak and other issues . Are all variables deallocated
You should use a custom ExceptionHandler (set_error_handler / set_exception_handler) and throw an Exception if you encounter any errors (CakePHP should already provide an ExceptionHandler). Make some space in your view and if the ExceptionHandler/ErrorHandler has a message, show it there to let the user know.
Your second code will just produce a blank page containing the little text. Every user will appreciate if you show the message inside your usual page layout instead of producing a blank page (which looks broken to most people).
The Cake tools to signal errors to the user are session messages and error views.
For "passive" actions like view actions, you should throw a 404 or similar, possibly more specialized error, e.g. if the requested model does not exist:
function view($id) {
$data = $this->Model->read(null, $id);
if (!$data) {
$this->cakeError('error404');
}
...
}
See Error Handling with CakePHP.
For any POST action, you should return the user to the view and display an error message using $this->Session->setFlash('Error!') and appropriate error messages for each invalid form field. That's the default behavior of baked views and controllers.
Terminating the whole script with exit makes for a miserable user experience.
In general, you should avoid exit. Exit is an abnormal termination, and programs should not terminate abnormally. Even if an error occurs, there are still many things that needs to be done - cleanup, logging, notifying the user etc. After all, your operating system doesn't reboot every time it cannot open a file.
performance-wise (AJAX cals)
Use exit().
user experience-wise (standard site nav)
Show the error in a proper formated page keeping the user within your site.

Categories