From e57f453c3638201acad1d06cead85de2496d6fc9 Mon Sep 17 00:00:00 2001 From: Alexander Rhodes <ironsizide@787980.no-reply.drupal.org> Date: Tue, 28 Feb 2017 10:25:14 -0500 Subject: [PATCH] Converts watchdog_exception calls to OO equivalent, part 2 --- .../src/Form/MappedObjectForm.php | 3 ++- .../tests/src/Unit/ClassesNotTested.txt | 1 - .../tests/src/Unit/MappedObjectTest.php | 14 ++++++---- .../salesforce_push/salesforce_push.module | 25 ++++++++++++++---- modules/salesforce_push/src/PushQueue.php | 26 ++++++++++++++++--- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/modules/salesforce_mapping/src/Form/MappedObjectForm.php b/modules/salesforce_mapping/src/Form/MappedObjectForm.php index 644e6e29..f62b3f64 100644 --- a/modules/salesforce_mapping/src/Form/MappedObjectForm.php +++ b/modules/salesforce_mapping/src/Form/MappedObjectForm.php @@ -10,6 +10,8 @@ use Drupal\salesforce\Exception; use Drupal\salesforce\Rest\RestClient; use Drupal\salesforce_mapping\SalesforceMappingStorage; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Utility\Error; +use Psr\Log\LogLevel; /** * Salesforce Mapping Form base. @@ -263,5 +265,4 @@ class MappedObjectForm extends ContentEntityForm { } return $drupal_entity; } - } diff --git a/modules/salesforce_mapping/tests/src/Unit/ClassesNotTested.txt b/modules/salesforce_mapping/tests/src/Unit/ClassesNotTested.txt index f06e0ccc..340a46a9 100644 --- a/modules/salesforce_mapping/tests/src/Unit/ClassesNotTested.txt +++ b/modules/salesforce_mapping/tests/src/Unit/ClassesNotTested.txt @@ -1,5 +1,4 @@ MappedObjectAccessControlHandler - no significant logic -MappedObjectStorage - no significant logic (all functions use loadByProperties method of tested parent object) MappedObjectStorageSchema - no significant logic (MappedObjectStorageSchema::getEntitySchema decorates tested parent method) MappingConstants - nothing but constants - nothing to test PushParams - no significant logic (all get methods) diff --git a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php index 7d9e6676..c101a239 100644 --- a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php +++ b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php @@ -126,7 +126,7 @@ class MappedObjectTest extends UnitTestCase { ->expects($this->any()) ->method('id') ->willReturn($this->entity_id); - + $this->entity ->expects($this->any()) ->method('isTranslatable') @@ -211,7 +211,7 @@ class MappedObjectTest extends UnitTestCase { ->willReturn(NULL); $this->assertNull($this->mapped_object->push()); } - + /** * @covers ::push */ @@ -320,7 +320,11 @@ class MappedObjectTest extends UnitTestCase { '@v' => $value, '@e' => $e->getMessage(), ]); - watchdog_exception(__CLASS__, $e); + \Drupal::logger(__CLASS__)->log( + LogLevel::ERROR, + '%type: @message in %function (line %line of %file).', + Error::decodeException($e) + ); continue; } } @@ -345,7 +349,7 @@ class MappedObjectTest extends UnitTestCase { ->save(); return $this; - + } -} \ No newline at end of file +} diff --git a/modules/salesforce_push/salesforce_push.module b/modules/salesforce_push/salesforce_push.module index 488c3b9a..9904e645 100644 --- a/modules/salesforce_push/salesforce_push.module +++ b/modules/salesforce_push/salesforce_push.module @@ -15,6 +15,8 @@ use GuzzleHttp\Exception\RequestException; use Drupal\salesforce_mapping\MappingConstants; use Drupal\salesforce\SalesforceEvents; use Drupal\salesforce_mapping\SalesforcePushOpEvent; +use Drupal\Core\Utility\Error; +use Psr\Log\LogLevel; /** * Implements hook_entity_insert(). @@ -74,7 +76,7 @@ function salesforce_push_entity_crud(EntityInterface $entity, $op) { } catch (\Exception $e) { // Do not allow any exception to prevent entity CRUD. - watchdog_exception('Salesforce Push', $e); + salesforce_push_log_error($e); } } } @@ -131,7 +133,7 @@ function salesforce_push_entity_crud_mapping(EntityInterface $entity, $op, Sales salesforce_push_enqueue_async($entity, $mapping, $mapped_object, $op); } catch (\Exception $e) { - watchdog_exception('Salesforce Push', $e); + salesforce_push_log_error($e); } return; } @@ -165,12 +167,12 @@ function salesforce_push_entity_crud_mapping(EntityInterface $entity, $op, Sales new SalesforcePushOpEvent($mapped_object, $op) ); - watchdog_exception('Salesforce Push', $e); + salesforce_push_log_error($e); try { salesforce_push_enqueue_async($entity, $mapping, $mapped_object, $op); } catch (\Exception $e) { - watchdog_exception('Salesforce Push', $e); + salesforce_push_log_error($e); } $mapped_object @@ -215,6 +217,19 @@ function salesforce_push_cron() { $queue->processQueues(); } catch (\Exception $e) { - watchdog_exception('Salesforce Push', $e); + salesforce_push_log_error($e); } } + +/** + * Helper watchdog_exception replacemnt function + * + * @param \Exception $e + */ +function salesforce_push_log_error(\Exception $e) { + \Drupal::logger('Salesforce Push')->log( + LogLevel::ERROR, + '%type: @message in %function (line %line of %file).', + Error::decodeException($e) + ); +} diff --git a/modules/salesforce_push/src/PushQueue.php b/modules/salesforce_push/src/PushQueue.php index feeaf31f..bcc70cdd 100644 --- a/modules/salesforce_push/src/PushQueue.php +++ b/modules/salesforce_push/src/PushQueue.php @@ -13,9 +13,11 @@ use Drupal\Core\Queue\DatabaseQueue; use Drupal\Core\Queue\RequeueException; use Drupal\Core\Queue\SuspendQueueException; use Drupal\Core\State\State; +use Drupal\Core\Utility\Error; use Drupal\salesforce_mapping\MappedObjectStorage; use Drupal\salesforce_mapping\SalesforceMappingStorage; use Drupal\salesforce\EntityNotFoundException; +use Psr\Log\LogLevel; /** * Salesforce push queue. @@ -309,14 +311,22 @@ class PushQueue extends DatabaseQueue { // Getting a Requeue here is weird for a group of items, but we'll // deal with it. $this->releaseItems($items); - watchdog_exception('Salesforce Push', $e); + \Drupal::logger(__CLASS__)->log( + LogLevel::ERROR, + '%type: @message in %function (line %line of %file).', + Error::decodeException($e) + ); } catch (SuspendQueueException $e) { // Getting a SuspendQueue is more likely, e.g. because of a network // or authorization error. Release items and move on to the next // mapping in this case. $this->releaseItems($items); - watchdog_exception('Salesforce Push', $e); + \Drupal::logger(__CLASS__)->log( + LogLevel::ERROR, + '%type: @message in %function (line %line of %file).', + Error::decodeException($e) + ); continue 2; } @@ -324,7 +334,11 @@ class PushQueue extends DatabaseQueue { // In case of any other kind of exception, log it and leave the item // in the queue to be processed again later. // @TODO: this is how Cron.php queue works, but I don't really understand why it doesn't get re-queued. - watchdog_exception('Salesforce Push', $e); + \Drupal::logger(__CLASS__)->log( + LogLevel::ERROR, + '%type: @message in %function (line %line of %file).', + Error::decodeException($e) + ); } finally { // If we've reached our limit, we're done. Otherwise, continue to next items. @@ -404,7 +418,11 @@ class PushQueue extends DatabaseQueue { return $update->execute(); } catch (\Exception $e) { - watchdog_exception('Salesforce Push', $e); + \Drupal::logger(__CLASS__)->log( + LogLevel::ERROR, + '%type: @message in %function (line %line of %file).', + Error::decodeException($e) + ); $this->catchException($e); // If the table doesn't exist we should consider the item released. return TRUE; -- GitLab