In your current code you'd have to check what $result is (eg, if ($result instanceof whatever-redirect()-classname-gives) { return $result; }. You might be better returning an error message or true from the charge() method though - then you can do something like if ($result !== true) { return redirect()->back()->with('danger', $result); } which would save you a lot of replicated code.
May 10, 2016
9
Level 8
Exception handling
I have problem with error handling. Please see code bellow. When exception is thrown I want to stop code, because like now I am getting error message as well I am returning success result and $billing->givepoints() executing.
How to check in controller is any error returning my method charge or stop executing code if error returning.
<?php
/**
* Created by PhpStorm.
* User: Freel
* Date: 09/05/16
* Time: 06:55
*/
namespace Acme\Billing;
use Exception;
use Stripe\Stripe;
use Illuminate\Support\Facades\Config;
class StripeBilling implements BillingInterface {
// Function
public function __construct(){
Stripe::setApiKey(Config::get('stripe.secret_key'));
}
public function charge(array $data){
try{
return \Stripe\Charge::Create([
'amount' => $data['amount'],
'currency' => 'gbp',
'description' => $data['description'],
'customer' => $data['customer']
]);
} catch (\Stripe\Error\RateLimit $e) {
return redirect()->back()->with('danger','Too many requests made to the API too quickly');
} catch (\Stripe\Error\InvalidRequest $e) {
return redirect()->back()->with('danger','Invalid parameters were supplied to Stripe\'s API');
} catch (\Stripe\Error\Authentication $e) {
return redirect()->back()->with('danger','Authentication with Stripe\'s API failed (maybe you changed API keys recently)');
// (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
return redirect()->back()->with('danger','Network communication with Stripe failed');
} catch (\Stripe\Error\Base $e) {
return redirect()->back()->with('danger','Error: '.$e->getMessage());
// Display a very generic error to the user, and maybe send
// yourself an email
}catch(Exception $e) {
return redirect()->back()->with('danger','Error: '.$e->getMessage());
// Something else happened, completely unrelated to Stripe
}
}
}
class PaymentController extends Controller{
// Function Buy items
public function Buy(Request $request){
$billing = \App::make('Acme\Billing\BillingInterface');
$result = $billing->charge([
'amount' => $request->packages*1000,
'description' => Auth::user()->email,
'customer' => $request->customer
]);
$billing->givepoints();
return redirect()->back()->with('success', 'Payment done succesiful.');
}
}
thx a lot for help.
Level 13
@freel You need to take advantage of the full Exception handling pattern.
try {
...some code
catch (Exception-Type $e) {
... handle Exception
} catch (Another Exception-Type $e) {
} finally {
... return some value or error
}
Regardless of if an Exception is thrown or the code in the initial try block succeeds the finally block will execute.
So for example:
class CreateCvgCategory extends DbCommand implements IDbCommand
{
public function exec($shortName, $longName)
{
$ret = null;
try {
$this->doThrow();
$m = new CoverageCat($this->sql); // Call Model.
$ret = $m->create($shortName, $longName);
$ret = new Message($ret, Message::STATUS_OK, 402); // Record created message.
} catch (InvalidArgException $e) {
$ret = $e;
} catch (DBConnectionException $e) {
$ret = $e;
} catch (SQLException $e) {
$ret = $e;
} catch (\Exception $e) {
$ret = $e;
} finally {
$this->close();
return $ret;
}
}
}
Please or to participate in this conversation.