Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup Update Paypal invokeApi to only throw exceptions. #16072

Merged
merged 1 commit into from
Dec 11, 2019

Conversation

eileenmcnaughton
Copy link
Contributor

Overview

Standardises error handling within paypal internal function

Before

Currently invokeAPI will return an error for a curl function and throw a processorException for an outcome other than success.

After

invokeAPI always throws an exception if it fails.

Technical Details

Failed transactions result in exceptions being thrown in invokeAPI. However, in the rarer scenario where the error is a curl error CRM_Core_Error objects are returned & (almost always) converted into exceptions & thrown

In just about every instance where it is called the Core_Error is converted into an exception anyway - this
can all be cleaned up as a follow up. The exceptions are

doQuery - called by doPayment
doDirectPayment - this is called by doPayment which converts it into an exception. Calling doDirectPayment directly was deprecated in 4.6
createRecurringPayments - called by doExpressCheckout which in already throws exceptions in other scenarios,
updateSubscriptionBillingInfo - we can update the one place that calls this in core as a follow up like #15676

However, note that an exception was already thrown in all these places in the more common error scenario of the action not succeeding as opposed
to the rare curl error scenario

Follow ons to consider

  1. introduce more nuance into PaymentProcessorExceptions - extend the exception class with PaymentProcessorConfigException, PaymentProcessingException or similar to differentiate
  2. remove all the handling for invokeApi to have returned an error object.
  3. fix the code that calls updateSubscriptionBillingInfo
  4. ensure docs reflect that doPayment should throw an exception. AFAIK we have not yet documented other 'do' functions & I'd prefer to stdise them first

Comments

Anything else you would like the reviewer to note

@civibot
Copy link

civibot bot commented Dec 10, 2019

(Standard links)

@civibot civibot bot added the master label Dec 10, 2019
@eileenmcnaughton
Copy link
Contributor Author

test this please

@eileenmcnaughton
Copy link
Contributor Author

@mattwire @artfulrobot - in reviewing Rich's last change I concluded we should do this - since it makes no sense to return an error for a curl error & throw an exception for a processor error - esp given the former is almost always converted to the latter

curl_error($ch)
);
return $e;
throw new PaymentProcessorException(curl_errno($ch));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we want to include curl_error($ch) output as well as error no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess it could be

throw new PaymentProcessorException(curl_error($ch), curl_errno($ch));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated per ^^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be tempted to prefix it with "Network error: " too, as Curl's errors are not very user friendly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - the errno is now duplicated because I have a feeling just putting it as error_code it might get swallowed & more info rather than less is probably helpful if there is a curl error

@artfulrobot
Copy link
Contributor

(CiviCRM Review Template DEL-1.2)

  • General standards
    • Explain (r-explain)
      • PASS : The goal/problem/solution have been adequately explained in the PR.
    • User impact (r-user)
      • PASS: The change would be intuitive or unnoticeable for a majority of users who work with * Documentation (r-doc)
      • PASS: The changes do not require documentation.
  • Run it (r-run)
    * UNREVIEWED
  • Developer standards
    • Technical impact (r-tech)
      • PASS: The change potentially affects compatibility, but the risks have been sufficiently managed.
    • Code quality (r-code)
      • ISSUE: Something was unclear to me. - see comment
    • Maintainability (r-maint)
      • PASS: makes code cleaner
    • Test results (r-test)
      • UNREVIEWED

@eileenmcnaughton eileenmcnaughton force-pushed the paypal_extract branch 2 times, most recently from d1dbd71 to 94ce42c Compare December 11, 2019 10:52
@artfulrobot
Copy link
Contributor

Code quality (r-code) PASS

Looks good to me, thanks @eileenmcnaughton

Currently invokeAPI will return an error for a curl function and throw a processorException for an outcome other than success.

In just about every instance where it is called the Core_Error is converted into an exception anyway - this
can all be cleaned up as a follow up. The exceptions are

doQuery -  called by doPayment
doDirectPayment - this is called by doPayment which converts it into an exception. Calling doDirectPayment directly was deprecated in 4.6
createRecurringPayments - called by doExpressCheckout which in already throws exceptions in other scenarios,
updateSubscriptionBillingInfo - we can update the one place that calls this in core as a follow up like civicrm#15676

However, note that an exception was already thrown in all these places in the more common error scenario of the action not succeeding as opposed
to the rare curl error scenario

Follow ons to consider
1) introduce more nuance into PaymentProcessorExceptions - extend the exception class with PaymentProcessorConfigException, PaymentProcessingException or similar to differentiate
2) remove all the handling for invokeApi to have returned an error object.
3) fix the code that calls updateSubscriptionBillingInfo
4) ensure docs reflect that doPayment should throw an exception. AFAIK we have not yet documented other 'do' functions & I'd prefer to stdise them first
@eileenmcnaughton
Copy link
Contributor Author

Added MOP per @artfulrobot review

@seamuslee001 seamuslee001 merged commit 005b96f into civicrm:master Dec 11, 2019
@seamuslee001 seamuslee001 deleted the paypal_extract branch December 11, 2019 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants