diff --git a/conf/cmi/core.extension.yml b/conf/cmi/core.extension.yml index 22d09b7a5..6d902bec8 100644 --- a/conf/cmi/core.extension.yml +++ b/conf/cmi/core.extension.yml @@ -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 diff --git a/public/modules/custom/grants_admin_applications/src/Form/AtvFormBase.php b/public/modules/custom/grants_admin_applications/src/Form/AtvFormBase.php index 30a0f35e8..63a4a3f33 100644 --- a/public/modules/custom/grants_admin_applications/src/Form/AtvFormBase.php +++ b/public/modules/custom/grants_admin_applications/src/Form/AtvFormBase.php @@ -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] + ); + } + } diff --git a/public/modules/custom/grants_admin_applications/src/Form/ResendApplicationsForm.php b/public/modules/custom/grants_admin_applications/src/Form/ResendApplicationsForm.php index aaf6143b4..abe6d2b07 100644 --- a/public/modules/custom/grants_admin_applications/src/Form/ResendApplicationsForm.php +++ b/public/modules/custom/grants_admin_applications/src/Form/ResendApplicationsForm.php @@ -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); } } @@ -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); } } @@ -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. * diff --git a/public/modules/custom/grants_admin_applications/src/Form/SubmittedApplicationsForm.php b/public/modules/custom/grants_admin_applications/src/Form/SubmittedApplicationsForm.php index 9b61b295f..44aa1ecd3 100644 --- a/public/modules/custom/grants_admin_applications/src/Form/SubmittedApplicationsForm.php +++ b/public/modules/custom/grants_admin_applications/src/Form/SubmittedApplicationsForm.php @@ -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; /** @@ -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); } } @@ -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) { } diff --git a/public/modules/custom/grants_handler/src/ApplicationException.php b/public/modules/custom/grants_handler/src/ApplicationException.php index 217c95ab4..c73861f7d 100644 --- a/public/modules/custom/grants_handler/src/ApplicationException.php +++ b/public/modules/custom/grants_handler/src/ApplicationException.php @@ -3,7 +3,7 @@ namespace Drupal\grants_handler; /** - * General error in applicatoin process. + * General error in application process. */ class ApplicationException extends \Exception { diff --git a/public/modules/custom/grants_handler/src/ApplicationUploaderService.php b/public/modules/custom/grants_handler/src/ApplicationUploaderService.php index 64a651ed5..38d88ae06 100644 --- a/public/modules/custom/grants_handler/src/ApplicationUploaderService.php +++ b/public/modules/custom/grants_handler/src/ApplicationUploaderService.php @@ -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; } } diff --git a/public/modules/custom/grants_handler/src/Plugin/WebformHandler/GrantsHandler.php b/public/modules/custom/grants_handler/src/Plugin/WebformHandler/GrantsHandler.php index c97c6f007..35dfb6208 100644 --- a/public/modules/custom/grants_handler/src/Plugin/WebformHandler/GrantsHandler.php +++ b/public/modules/custom/grants_handler/src/Plugin/WebformHandler/GrantsHandler.php @@ -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); @@ -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); } } @@ -1659,6 +1650,8 @@ public function confirmForm( ] ) ); + + \Sentry\captureException($e); } } diff --git a/public/modules/custom/grants_logger/grants_logger.info.yml b/public/modules/custom/grants_logger/grants_logger.info.yml index de39d2fc6..d49026460 100644 --- a/public/modules/custom/grants_logger/grants_logger.info.yml +++ b/public/modules/custom/grants_logger/grants_logger.info.yml @@ -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 diff --git a/public/modules/custom/grants_logger/grants_logger.services.yml b/public/modules/custom/grants_logger/grants_logger.services.yml index 9877d00fe..4a9cf5d5b 100644 --- a/public/modules/custom/grants_logger/grants_logger.services.yml +++ b/public/modules/custom/grants_logger/grants_logger.services.yml @@ -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: ~ diff --git a/public/modules/custom/grants_logger/src/EventSubscriber/SentryEventSubscriber.php b/public/modules/custom/grants_logger/src/EventSubscriber/SentryEventSubscriber.php new file mode 100644 index 000000000..12b69d326 --- /dev/null +++ b/public/modules/custom/grants_logger/src/EventSubscriber/SentryEventSubscriber.php @@ -0,0 +1,49 @@ + '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()); + } + +} diff --git a/public/modules/custom/grants_logger/src/Logger/GrantsLogger.php b/public/modules/custom/grants_logger/src/Logger/GrantsLogger.php deleted file mode 100644 index f8fc036a1..000000000 --- a/public/modules/custom/grants_logger/src/Logger/GrantsLogger.php +++ /dev/null @@ -1,72 +0,0 @@ -connection = $connection; - $this->parser = $parser; - $this->helfiHelsinkiProfiiliUserdata = $helfiHelsinkiProfiiliUserdata; - } - - /** - * {@inheritdoc} - */ - public function log($level, $message, array $context = []): void { - - if ($this->helfiHelsinkiProfiiliUserdata->isAuthenticatedExternally()) { - $userData = $this->helfiHelsinkiProfiiliUserdata->getUserData(); - if ($userData) { - $message = $message . (' (HP UUID: @helfi_hp_uid)'); - $context['@helfi_hp_uid'] = $userData["sub"]; - } - } - - parent::log($level, $message, $context); - } - -} diff --git a/public/modules/custom/grants_profile/src/GrantsProfileService.php b/public/modules/custom/grants_profile/src/GrantsProfileService.php index 614c21390..b030cd405 100644 --- a/public/modules/custom/grants_profile/src/GrantsProfileService.php +++ b/public/modules/custom/grants_profile/src/GrantsProfileService.php @@ -303,8 +303,9 @@ public function createNewProfile( $this->logger ->error('Error fetching community data. Error: %error', [ '%error' => $e->getMessage(), - ] - ); + ]); + + \Sentry\captureException($e); } return $newProfile; }