-
Notifications
You must be signed in to change notification settings - Fork 198
Error Handling
Using save()
, update()
methods:
These methods usually return a detail about the causes of the error each of them with different code to be able to identify the incorrect parameters.
For this reason these method return a boolean value that tell us if the request has been processed correctly or not, then we will be able to inspect each of the causes or incorrect parameters and show the correct feedback to our users (Typically errors are due to validation issues)
// ...
if ($payment->save()) {
echo $payment->status;
} else {
echo $payment->error; // You can get just a brief error description
// or explore each cause
foreach($payment->error->causes as $cause) {
// You may show a custom error message according to our cause error codes.
echo $cause->code . ' ' . $cause->description;
}
}
Using search()
method:
In the search escenario we have two posible cases, return an empty array or return an exception because your are trying to search using unsupported params for searching.
If you want to handle a posible escenario when you are trying to look for a resource using an not supported param you have to use try catch like this.
try {
// ...
$payments = MercadoPago\Payment::search($filters);
} catch(Exception $e) {
$e->getMessage(); // some attribute is not a valid param to search
}
Using find_by_id()
method:
You are going to receive a null value when the resource can't be founded or an exception if you are passing bad params.
try {
$payment = MercadoPago\Payment::find_by_id("123456");
if ($payment == null) {
// 404 payment not found
}
} catch(Exception $e) {
$e->getMessage(); // Malformed request or API internal error
}
Developer Experience - MercadoPago