Handling facebook exceptions - php

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();
}

Related

How do I get the last mysqli query executed before an exception occurs in PHP?

Here is some sample code:
try
{
$db->query($sql_q1);
$db->query($sql_q2);
$db->query($sql_q3);
}
catch (Exception $e)
{
echo $e->getMessage();
}
How can I see which query caused the error in catch so I can see it along with the error message? And a line number won't work do me, because my queries are often dynamically built.
Well, first of all, just like Usman Munir said, if you won't catch it, there will be more information than if you catch. Just try to remove that try..catch stuff from your code and you will see the first 15 characters of the query.
But in case you need the entire query, there is a way, though not that it will give it to you right away, like a designated variable that contains specifically "last executed query", but well technically you can
There is a thing called a stack trace, intended for the exact purpose. It can show you all the calls made in your code that led to the error, including all function parameters.
Change your code to this
try
{
$db->query($sql_q1);
$db->query($sql_q2);
$db->query($sql_q3);
}
catch (Exception $e)
{
var_dump($e->getTrace());
// or to get the full error info, just
var_dump($e);
}
and you will see your query in the full glory, though it will be quite a pain to get the actual query automatically if that's your goal. but if you want to just log/visually inspect the error information, it will do.
Not that obviously you should never put var_dump() inside try_catch() in any real life code. Instead, some handling code must be put into your error handler. Especially if your purpose is debugging. Hence, your real life code should be rather this
// somewhere in the bootstrap
include 'error_handler.php';
// anywhere in your code
$db->query($sql_q1);
$db->query($sql_q2);
$db->query($sql_q3);
and in the error_handler.php you can add a code to print the full stack trace. an example for such a file you can find in the article Usman Munir posted a link to (which incidentally I wrote).
Note that stack traces tend to grow really big, and can litter your logs significantly. So consider making extended output optional.
One possible solution if you are trying to know the previously executed query would be to store your SQL in a single variable and change it for each statement. Then if an error occurs with a query you can just echo out the contents of the variable. Unless there is a specific reason you need to have multiple variables to hold your queries.

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

Facebook php SDK - simple datafetch

I'm trying to integrate my facebook app with PHP. What I need to do is have users go to the app-page, authenticate the app and then somehow I want to end up with a variable that contains the info I need so I can store it in MySQL.
I downloaded the facebook SDK, but I cannot figure out how to make it work. All the examples I can find refer to a file called facebook.php - but there is no such file in the SDK (maybe the changed it?).
I managed to get it working with JavaScript, but I don't know how to get from JavaScript to php (the user object, that is).
I would prefer to just run it entirely through php. What I'm trying to do is very simple, but I don't understand what I'm doing wrong. The following, for instance, does not work:
<?php
FacebookSession::setDefaultApplication('appid', 'appsecret');
require facebook-php-sdk-v4-master\src\Facebook\FacebookCanvasLoginHelper.php;
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
echo "error";
} catch(\Exception $ex) {
echo "error 2";
}
if ($session) {
echo "logged in";
}
?>
Gives PHP Fatal error: Class 'FacebookSession' not found in
Where do I go? I don't think the Facebook getting started guide is helpful at all. It just starts out with "you have to do this" (what I did above) - but that doesn't work?
Looks like they changed the whole SDK for PHP 5.4.
Well there's a few options, you could go with the older SDK and all those tutorials you found will work for that. Or you could learn some new things!
I suggest that if you want to use the new Facebook SDK, you go ahead an learn about composer first, it makes including libraries a piece of cake and all you'd have to do is include the autoloader at the top of the file. Once done, you can just follow the example and it should work.
If you don't want to learn composer, you'll have to include all the required files manually. Here's a basic example.
In your example it looks like the error was because you were calling
FacebookSession::setDefaultApplication
Before you'd actually included the SDK

How do I override the not found page in CodeIgniter?

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.

Why not to use Array of errors instead of Exception Handling?

Why not using array of errors instead of throwing errors and check if it's not empty later in the code.... Exception handling is very confusing to me , I can not understand it's purpose ... can anybody enlighten me !?
they compare it with die() as better not to stop the execution of the code , why would I stop the execution of code if I don't want to !? ofcourse I do
I'm a beginner programmer , it might be confusing to you as well , or because i'm not very experienced in this ugly thing.
Please in the context of PHP only.
One reason you might decide to throw an exception is because you can add a jump in control flow without an explicit piece of syntax. What do I mean by that?
rather than
$returnVal = doSomeDiskFunction();
if($returnVal == $ERROR_CODEA)
{
// do some stuff
}
else if( $returnVal == $ERROR_CODEB)
{
//do some stuff
}
$returnVal = doSomeOtherDiskFunction();
if($returnVal == $ERROR_CODEA)
{
// do some stuff
}
else if( $returnVal == $ERROR_CODEB)
{
//do some stuff
}
you could just have
try{
doSomeDiskFunction();
doSomeOtherDiskFunction();
}
catch(ExceptionTyepA $exceptionA)
{
//do some stuff
}
catch(ExceptionTypeB $exceptionB)
{
//do some stuff
}
Seems a lot cleaner, yes??? It's also a formal way of alerting calling code that it needs to deal with potential error conditions if you choose to have the exception propogate upwards in the call stack.
Also, exceptions should be used for code that you don't expect to happen, like failing to connect to a database, not code that you DO expect to happen, like a user submitting bad data. The other poster made the point that you kind of expect a user to make a lot of errors filling out a form, so you wouldn't throw an exception when you come across some data that is in a bad user-entered format, because you expect user data to be poor quality.
It really depends what type of error you're talking about.
I always die(); for really bad errors, like if the application couldn't connect to the database.
But for anything that takes user input, like a form, it's better to use arrays of errors that the user can see all at once, instead of correcting one field at a time and repeatedly having to press submit.
(If you want to know in more detail, I often use an array of arrays to display errors, since one field could potentially have multiple things wrong with it. For example, if a user tries to register an account with username "1337", I will use a validator to check against multiple conditions and it will return an array with things like "username must be at least 5 characters long", "username must contain at least three letters", "that username has been disallowed by the administrator" and all those messages will be displayed simultaneously above that particular field)
There is a way to code completely without exception handling. For example, if you have a method that returns the length of an object, have it return -1 if there's been an error. This is how most C API's are built.
That said, when you're building complex systems, and you have a ton of "black box" code that might behave wrongly, exceptions help. The way exceptions work is like this: when someone "throws", folks on the call stack start getting notified. One step at a time. One of these methods can "Catch" the exception and handle it.
Why is this useful: you can have a DB layer that has lots of logic inside that throws exceptions. If something goes wrong there, your DB exception handling code can fail gracefully, showing a nice error message to the user; it can also send a text message to the admin, demanding attention.
You can even create a hierarchy of exception handlers: you can re-throw the exception after you've done something with it.
PHP's exceptions sucks hard, but they still have their basics benefits:
Exception provides much more information about "What went wrong?" in much better form (exception is an object)
They make your code clearer
They are the only reasonable way to break execution of the part of code (that doesn't work as it supposes to), fix (if possible) what need to be fixed and continue without complete failure.
Really it depends on the language. C is similar in that respect -- it doesn't force you to handle an error. Most functions return -1 if they had a problem; it's up to you to check 'errno' to see what happened.
Exceptions are generally a good thing though. You rarely want to blindly continue running if an error occurred (Never never NEVER say "On Error Resume Next" in Visual Basic. Please.). It's quite easy to catch the exception and do nothing if you are sure you don't need to do anything.

Categories