2

I have using a version of GoCardless's API in PHP to process payments on my website. However when their API returns an error I would like to display the user more effective errors.

I have got half way there but I was wondering if there is anyway I could do the following:

If I have the following error:

Array ( [error] => Array ( [0] => The resource has already been confirmed ) )

Is there anyway to extract just the The resource has already been confirmed part with PHP?

My Code:

    try{
        $confirmed_resource = GoCardless::confirm_resource($confirm_params);
    }catch(GoCardless_ApiException $e){
        $err = 1;
        print '<h2>Payment Error</h2>
        <p>Server Returned : <code>' . $e->getMessage() . '</code></p>';
    }

Thanks.

UPDATE 1:

Code that triggers the exception:

$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_response_code < 200 || $http_response_code > 300) {

  // Create a string
  $message = print_r(json_decode($result, true), true);

  // Throw an exception with the error message
  throw new GoCardless_ApiException($message, $http_response_code);

}

UPDATE 2 :-> print_r($e->getMessage()) Output:

Array ( [error] => Array ( [0] => The resource has already been confirmed ) )

David Passmore
  • 6,089
  • 4
  • 46
  • 70

3 Answers3

1

The method $e->getMessage() appears to return an array with an index 'error' wich is an array again that contains the message text. If you ask me this is bad API design

However you can access the message text like this:

try{
    $confirmed_resource = GoCardless::confirm_resource($confirm_params);
}catch(GoCardless_ApiException $e){
    $err = 1;
    $message = $e->getMessage();
    $error = $message['error'];
    print '<h2>Payment Error</h2>
    <p>Server Returned : <code><' . $error[0] . "</code></p>";
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 3
    +1 for the correct answer. But bad on the api devs for returning an array in place of what should be a string. – prodigitalson Mar 31 '13 at 22:38
  • 2
    Yeah! I'm currently searching for docs. Maybe I'll find an explanation for this – hek2mgl Mar 31 '13 at 22:39
  • Even if you find an explanation its irrelevant IMO... They should have had get message return a string of all errors somehow not an array and should have made a new method to get an array of messages... Or something along those lines. – prodigitalson Mar 31 '13 at 22:41
  • Thanks all I made a mistake in the question please see me edit :) – David Passmore Mar 31 '13 at 22:43
  • 2
    @prodigitalson Yes, I'm with you. I browsed the source code (for short) and I figured out, that `GoCardless_ApiException->getMessage()` is designed to return a string - but don't check argument types in it's constructor. So the problem must be in the code that triggers the excpetion. Hard to say where exactly without having more info. – hek2mgl Mar 31 '13 at 22:44
  • @DavidPassmore Sorry it's a little bit unclear how your array looks (after so much updating the question). Can you just add the output of `print_r($e->getMessage());` to question? Whats the meaning of the code you posted at bottom of your question? – hek2mgl Mar 31 '13 at 22:53
  • @hek2mgl Thats where the code triggers the exception according to server trace. thought it might help :) – David Passmore Mar 31 '13 at 22:57
  • 1
    Have you checked my recent update? it should work. (If I'm not blind ;) – hek2mgl Mar 31 '13 at 23:02
  • Still returning : Payment Error Server Returned : `A` – David Passmore Mar 31 '13 at 23:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27290/discussion-between-david-passmore-and-hek2mgl) – David Passmore Mar 31 '13 at 23:12
1

If you look into the GoCardless_ApiException class code you'll see that there's a getResponse() method that you could use to access the error element of the response array...

$try{
    $confirmed_resource = GoCardless::confirm_resource($confirm_params);
}catch(GoCardless_ApiException $e){
    $err = 1;
    $response = $e->getResponse();

    print '<h2>Payment Error</h2>
    <p>Server Returned : <code>' . $response['error'][0] . "</code></p>";
}
TobyG
  • 1,692
  • 3
  • 21
  • 36
0

I discovered the problem, the output from $e->getMessage() was a plain string, NOT an array.

So I edited the Request.php file to the following:

$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_response_code < 200 || $http_response_code > 300) {

  // Create a string <<-- THE PROBLEM -->>
  // $message = print_r(json_decode($result, true), true);

  $message_test = json_decode($result, true);

  // Throw an exception with the error message
  // OLD - throw new GoCardless_ApiException($message, $http_response_code);
  throw new GoCardless_ApiException($message_test[error][0], $http_response_code);

}

and then my php file :

try{
    $confirmed_resource = GoCardless::confirm_resource($confirm_params);
}catch(GoCardless_ApiException $e){
    $err = 1;
    $message = $e->getMessage();

    print '<h2>Payment Error</h2>
    <p>Server Returned : <code>' . $message . "</code></p>";
}

and the page outputs:

Payment Error

Server Returned : The resource has already been confirmed

David Passmore
  • 6,089
  • 4
  • 46
  • 70