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
Related
I want to build an authentication system for a website using IMAP. Users should be able to log in with the same credentials as the email account.
Everthing works fine, except if the input data is wrong or didn't match with the email account credentials. If so, there is obviously an error...
imap_open(): Couldn't open stream {domain.com:993/imap/ssl}INBOX
Is there any way to set an custom callback if the imap_open() fails? (Like redirect back with error message, ...)
This is warning level message so you should be able to use error handler, for example:
set_error_handler(function() { /* this will be executed on error */ });
imap_open();
restore_error_handler();
Here is similar question: https://stackoverflow.com/a/1241751/3470670
Here you can find complete documentation about setting custom error handler http://php.net/manual/en/function.set-error-handler.php
I am using Paypal-PHP. SDK, a I want to get code errors. When I put wrong credit card data, I am getting this JSON as answer:
{
"name":"CREDIT_CARD_REFUSED",
"message":"Credit card was refused",
"information_link":"https://developer.paypal.com/webapps/developer/docs/api /#CREDIT_CARD_REFUSED",
"debug_id":"63a9cf220d272"
}
or this:
{"name":"VALIDATION_ERROR",
"details":[
{"field":"payer.funding_instruments[0].credit_card.expire_month","issue":"Must not be blank"},
{"field":"payer.funding_instruments[0].credit_card.expire_year","issue":"Must not be blank"},
{"field":"payer.funding_instruments[0].credit_card.cvv2","issue":"Must be numeric"}],
"message":"Invalid request - see details","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR","debug_id":"8e61a15a1bf4a"}
But I need error code. How Can I get it?
Thanks
REST API usually do not return error codes like classic API. There are no error codes associated with the error message.
It just returns the error object with name debug_id message information_link details
I am looking to make error reporting from an app more friendly to users. I am going to replace the 'Whoops' screen (in production) with a form allowing the user to submit the problem. I am wondering if there is a simple way to add a specific ID# (unique integer) to the stack trace in the error log so that we can easily view specific errors which were generated on production.
The simple way to do this is in app/start/global.php. Under the Application Error Handler you want to log your error, and generate a GUID (or something similar).
App::error(function(Exception $exception, $code)
{
// Generate a unique ID for this error...
$unique_id = uniqid();
// log the error
Log::error(str_repeat('-', 40));
Log::error("Exception for $unique_id");
Log::error($exception);
// return error form
return View::make('whoops_error_form')->with('unique_id', $unique_id);
});
Your whoops_error_form template would have a hidden form somewhere where you will be able to submit the application error. All reports would be logged in app/storage/logs.
By returning the view on App::error, you will disable the other exception handlers (such as Whoops!)
This might be a n00bish question, but whatever. Is okay to use exceptions for form validation? Let's say I have a form which asks users for their name and email, is right to do the following?
try {
if (empty($_POST["name"])) {
throw new UserRegistrationException("Your name cannot be empty.");
}
if (filter_var($_POST["email"])) {
throw new UserRegistrationException("Invalid email");
}
// Save new user into database
} catch (UserRegistrationException $e) {
// Show errors on screen
}
Also -if this is in fact the correct way to do it- if the user submits both an empty name and an invalid email, would both of the exceptions execute or only the one that appears first (the name one in this case)?
I'm using PHP by the way.
I personally like to use exceptions for anything that should stop or alter program flow. In other words, if validation of a particular field changes how data is processed, or requires the process to be repeated, then I always use exception for error handling.
If it's trivial, or I'm simply compiling a list of error messages, then I do not trigger exceptions.
To answer questions, two exceptions cannot be thrown at the same time. The first throw statement that is reached will be thrown. That's not to say that sometimes it doesn't make sense to rethrow as another type of exception.
The use case for exceptions is for exceptional conditions. In this case, do you expect the username and password fields to be blank? If you're displaying a web form, I'd argue that, yes, you do expect blank username and password fields, and so you should be checking for that condition explicitly, rather than throwing an exception.
To answer your specific question, both exceptions will not be thrown if an error is encountered. The throw statement will send the program into the catch block. From there control will flow as normal.
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.