Skip to content

Commit

Permalink
UHF-10131: sentry (#1515)
Browse files Browse the repository at this point in the history
* UHF-10131: Repurpose grants_logger module for sentry logging

* UHF-10131: Fix typo

* UHF-10131: Code deduplication

* UHF-10131: Capture exceptions

* UHF-10131: Remove comment
  • Loading branch information
hyrsky authored Oct 22, 2024
1 parent 1773192 commit a5f741a
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 146 deletions.
1 change: 1 addition & 0 deletions conf/cmi/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module:
grants_club_section: 0
grants_front_banner: 0
grants_industries: 0
grants_logger: 0
grants_mandate: 0
grants_members: 0
grants_metadata: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,27 @@ public function sendApplicationToIntegrations(AtvDocument $atvDoc, string $appli
->error('Application resending failed: @error', ['@error' => $e->getMessage()]);
$this->messenger()
->addError($this->t('Application resending failed: @error', ['@error' => $e->getMessage()]));

\Sentry\captureException($e);
}
}

/**
* Handle exceptions.
*
* @param string $message
* Log message prefix.
* @param \Throwable $e
* The exception.
*/
protected function handleException(string $message, \Throwable $e): void {
$uuid = Uuid::uuid4()->toString();
$this->messenger()
->addError('Error has occurred and has been logged. ID: @uuid', ['@uuid' => $uuid]);
$this->logger(self::LOGGER_CHANNEL)->error(
"$message: @error, ID: @uuid",
['@error' => $e->getMessage(), '@uuid' => $uuid]
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,7 @@ public function getStatus(array $form, FormStateInterface $formState): void {
}
}
catch (\Exception $e) {
$uuid = Uuid::uuid4()->toString();
$this->messenger()
->addError('Error has occured and has been logged. ID: @uuid', ['@uuid' => $uuid]);
$this->logger(self::LOGGER_CHANNEL)->error(
'Error: status check: @error, ID: @uuid',
['@error' => $e->getMessage(), '@uuid' => $uuid]
);
$this->handleException("Error: status check", $e);
}
}

Expand Down Expand Up @@ -411,7 +405,7 @@ public function resendApplicationCallback(array $form, FormStateInterface $formS
$formState->setRebuild();
}
catch (GuzzleException | \Exception $e) {
$this->handleException($e);
$this->handleException("Error: Admin application forms - Resend error", $e);
}
}

Expand Down Expand Up @@ -449,25 +443,6 @@ private function handleApplicationNotFound(string $applicationId, FormStateInter
$formState->setRebuild();
}

/**
* Handle exceptions.
*
* @param \Exception $e
* The exception.
*
* @return void
* Void.
*/
private function handleException(\Exception $e): void {
$uuid = Uuid::uuid4()->toString();
$this->messenger()
->addError('Error has occurred and has been logged. ID: @uuid', ['@uuid' => $uuid]);
$this->logger(self::LOGGER_CHANNEL)->error(
'Error: Admin application forms - Resend error: @error, ID: @uuid',
['@error' => $e->getMessage(), '@uuid' => $uuid]
);
}

/**
* Ajax callback event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Drupal\grants_handler\Helpers;
use Drupal\helfi_atv\AtvDocument;
use GuzzleHttp\Exception\GuzzleException;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -194,12 +193,7 @@ public function resendApplicationCallback(array $form, FormStateInterface $formS
$this->sendApplicationToIntegrations($atvDoc, $transactionId);
}
catch (GuzzleException | \Exception $e) {
$uuid = Uuid::uuid4()->toString();
$this->messenger()->addError('Error has occured and has been logged. ID: @uuid', ['@uuid' => $uuid]);
$this->logger(self::LOGGER_CHANNEL)->error(
'Error: Admin application forms - Resend error: @error, ID: @uuid',
['@error' => $e->getMessage(), '@uuid' => $uuid]
);
$this->handleException('Error: Admin application forms - Resend error', $e);
}
}

Expand Down Expand Up @@ -289,12 +283,7 @@ function (array $doc) {
$formState->setRebuild();
}
catch (\Exception $e) {
$uuid = Uuid::uuid4()->toString();
$this->messenger()->addError('Error has occured and has been logged. ID: @uuid', ['@uuid' => $uuid]);
$this->logger(self::LOGGER_CHANNEL)->error(
'Error: status check: @error, ID: @uuid',
['@error' => $e->getMessage(), '@uuid' => $uuid]
);
$this->handleException('Error: status check', $e);
}
catch (GuzzleException $e) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\grants_handler;

/**
* General error in applicatoin process.
* General error in application process.
*/
class ApplicationException extends \Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ public function handleApplicationUploadViaIntegration(
catch (\Exception $e) {
$this->messenger->addError($this->t('Application saving failed, error has been logged.', [], $tOpts));
$this->logger->error('Error saving application: %msg', ['%msg' => $e->getMessage()]);

\Sentry\captureException($e);

return FALSE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1489,13 +1489,9 @@ public function postSaveSubmitForm(): void {
);
}
}
catch (\Exception $e) {
$this->getLogger('grants_handler')
->error('Error uploadind application: @error', ['@error' => $e->getMessage()]);
}
catch (GuzzleException $e) {
catch (\Exception | GuzzleException $e) {
$this->getLogger('grants_handler')
->error('Error uploadind application: @error', ['@error' => $e->getMessage()]);
->error('Error uploading application: @error', ['@error' => $e->getMessage()]);
}

$this->formLockService->releaseApplicationLock($this->applicationNumber);
Expand Down Expand Up @@ -1549,27 +1545,22 @@ public function postSave(WebformSubmissionInterface $webform_submission, $update

$this->postSaveHandleApplicationNumber($webform_submission);

// If triggering element is either draft save or proper one,
// we want to parse attachments from form.
if ($this->triggeringElement == '::submitForm') {
try {
try {
// If triggering element is either draft save or proper one,
// we want to parse attachments from form.
if ($this->triggeringElement == '::submitForm') {
$this->postSaveSubmitForm();
}
catch (GuzzleException $e) {
$this->messenger->addError($this->t('Error saving application. please contact support.'));
$this->getLogger('grants_handler')
->error('Error saving application: @error', ['@error' => $e->getMessage()]);
}
}
if ($this->triggeringElement == '::submit') {
try {
if ($this->triggeringElement == '::submit') {
$this->postSaveSubmit($webform_submission);
}
catch (GuzzleException $e) {
$this->messenger->addError($this->t('Error saving application. please contact support.'));
$this->getLogger('grants_handler')
->error('Error saving application: @error', ['@error' => $e->getMessage()]);
}
}
catch (GuzzleException $e) {
$this->messenger->addError($this->t('Error saving application. please contact support.'));
$this->getLogger('grants_handler')
->error('Error saving application: @error', ['@error' => $e->getMessage()]);

\Sentry\captureException($e);
}
}

Expand Down Expand Up @@ -1659,6 +1650,8 @@ public function confirmForm(
]
)
);

\Sentry\captureException($e);
}
}

Expand Down
8 changes: 5 additions & 3 deletions public/modules/custom/grants_logger/grants_logger.info.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: 'Grants Logger'
type: module
description: 'Grants logger override.'
description: 'Grants logging customization'
package: 'helfi'
core_version_requirement: ^9 || ^10
core_version_requirement: ^10 || ^11
dependencies:
- helfi_helsinki_profiili
- raven:raven
- helfi_atv:helfi_atv
- helfi_helsinki_profiili:helfi_helsinki_profiili
10 changes: 5 additions & 5 deletions public/modules/custom/grants_logger/grants_logger.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
logger.dblog:
class: Drupal\grants_logger\Logger\GrantsLogger
arguments: ['@database', '@logger.log_message_parser', '@helfi_helsinki_profiili.userdata']
tags:
- { name: logger }
_defaults:
autowire: true
autoconfigure: true

Drupal\grants_logger\EventSubscriber\SentryEventSubscriber: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Drupal\grants_logger\EventSubscriber;

use Drupal\helfi_atv\Event\AtvServiceExceptionEvent;
use Drupal\helfi_helsinki_profiili\Event\HelsinkiProfiiliExceptionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Forwards events to Sentry.
*/
final class SentryEventSubscriber implements EventSubscriberInterface {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
AtvServiceExceptionEvent::EVENT_ID => 'onAtvException',
HelsinkiProfiiliExceptionEvent::EVENT_ID => 'onHelsinkiProfiiliException',
];
}

/**
* Logs the event to sentry.
*
* @param \Drupal\helfi_atv\Event\AtvServiceExceptionEvent $event
* An exception event.
*/
public function onAtvException(AtvServiceExceptionEvent $event): void {
// Consider ignoring the event if $event is instanceof GuzzleException
// and http error status code is _some_status_code_ if, for example, 404
// errors cause too much error spam here.
\Sentry\captureException($event->getException());
}

/**
* Logs the event to sentry.
*
* @param \Drupal\helfi_helsinki_profiili\Event\HelsinkiProfiiliExceptionEvent $event
* An exception event.
*/
public function onHelsinkiProfiiliException(HelsinkiProfiiliExceptionEvent $event): void {
\Sentry\captureException($event->getException());
}

}
72 changes: 0 additions & 72 deletions public/modules/custom/grants_logger/src/Logger/GrantsLogger.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ public function createNewProfile(
$this->logger
->error('Error fetching community data. Error: %error', [
'%error' => $e->getMessage(),
]
);
]);

\Sentry\captureException($e);
}
return $newProfile;
}
Expand Down

0 comments on commit a5f741a

Please sign in to comment.