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

UHF-10131: sentry #1515

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/cmi/core.extension.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
@@ -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.
*
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
namespace Drupal\grants_handler;

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

Original file line number Diff line number Diff line change
@@ -262,6 +262,10 @@ 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);

// Re-throw?
return FALSE;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

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
@@ -303,8 +303,9 @@ public function createNewProfile(
$this->logger
->error('Error fetching community data. Error: %error', [
'%error' => $e->getMessage(),
]
);
]);

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