Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Form\Element;
use Laminas\Json\Json;
use Laminas\Mvc\MvcEvent;
use Laminas\View\Renderer\PhpRenderer;

/**
Expand Down Expand Up @@ -46,6 +47,33 @@ public function getConfig()
);
}

public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);

// Log uncaught fatal php errors through the standard logger.
// Unlike mvc exceptions handled by Omeka\Mvc\ExceptionListener, fatal
// errors don't go through mvc layer, so they are only displayed and
// never recorded.
// See the job fatal handling in Job\DispatchStrategy\Synchronous.
$services = $event->getApplication()->getServiceManager();
register_shutdown_function(function () use ($services): void {
$error = error_get_last();
$fatals = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
if (!$error || !($error['type'] & $fatals)) {
return;
}
try {
$services->get('Omeka\Logger')->err(
'Fatal error: {message} in {file}:{line}', // @translate
['message' => $error['message'], 'file' => $error['file'], 'line' => $error['line']]
);
} catch (\Throwable $e) {
// The logger may be unavailable during shutdown.
}
});
}

public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
Expand Down
4 changes: 2 additions & 2 deletions application/src/Job/DispatchStrategy/Synchronous.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function send(Job $job)
public function handleFatalError(Job $job, EntityManager $entityManager)
{
$lastError = error_get_last();
$errors = [E_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR];
if ($lastError && in_array($lastError['type'], $errors)) {
$fatals = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
if ($lastError && ($lastError['type'] & $fatals)) {
$job->setStatus(Job::STATUS_ERROR);
$job->addLog(sprintf("Fatal error: %s\nin %s on line %s",
$lastError['message'],
Expand Down
40 changes: 30 additions & 10 deletions application/src/View/Helper/Trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,40 @@ public function __construct(EventManagerInterface $eventManager, ControllerPlugi
public function __invoke($name, array $params = [], $filter = false, ?array $ids = null)
{
$controller = $this->controllerPluginManager->getController();
if (!$controller) {
return $filter ? $params : null;
}
$routeMatch = $controller->getEvent()->getRouteMatch();
if (!$routeMatch) {
// Without a route match this request is 404. No need to trigger.
return $filter ? $params : null;
}
$routeMatch = $controller ? $controller->getEvent()->getRouteMatch() : null;
if ($filter) {
$params = $this->events->prepareArgs($params);
}
$event = new Event($name, $this->getView(), $params);
$this->events->setIdentifiers($ids ?: [$routeMatch->getParam('controller')]);
$this->events->triggerEvent($event);
// Fall back to the error identifier when no route matched (404, error
// page), so modules attached to "view.layout" can still observe event.
$isError = !$routeMatch;
$this->events->setIdentifiers($ids ?: [$isError ? 'Omeka\Controller\Error' : $routeMatch->getParam('controller')]);
// Avoid cascaded error or blank page hiding original failure above.
Comment on lines +52 to +56
// Nevertheless, add a second message in that case and error_log().
if ($isError) {
try {
$this->events->triggerEvent($event);
} catch (\Throwable $e) {
try {
$this->getView()->getHelperPluginManager()->getServiceLocator()
->get('Omeka\Logger')->err(
'Listener exception while triggering "{name}" on error page: {message} in {file}:{line}',
['name' => $name, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]
);
} catch (\Throwable $loggerError) {
error_log(sprintf(
'Omeka Trigger: listener exception while triggering "%s" on error page: %s in %s:%d',
$name,
$e->getMessage(),
$e->getFile(),
$e->getLine()
));
}
}
} else {
$this->events->triggerEvent($event);
}
if ($filter) {
return $params;
}
Expand Down
Loading