Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

onFatalException Details

Started by Muut Archive 10 years ago · 5 replies · 356 views
10 years ago

When Grav fires the onFatalException event, I would like to send myself a notice of the error with details. The notification works, but I can't figure out where to grab exception details. Any ideas?

PHP
public static function getSubscribedEvents() {
    return [
        'onPageNotFound' => ['sendAlert', 0],
        'onFatalException' => ['sendAlert', 0]
    ];
}

/**
 * Convert error hook into alert mesage
 * Route message to alert mechanism (currently Slack)
 *
 * @param Event $event
 * @param string $hook
 */
public function sendAlert(Event $event, $hook) {
    $url = $this->slackFriendlyUrl($this->grav['uri']->url);

    if ($hook === "onPageNotFound") {
        $message = "```404: " . $url  . "```";
    } elseif ($hook === "onFatalException") {
        $message = "@channel: Grav Error ⚠️ ```Fatal Exception:
" . $url . "
" . $exception_string? . "```";
    } else {
        $message = "@channel: Grav Error ⚠️ ```Undefined Error:
" . $url . "```";
    }

$this->slackAlert($message);
}
10 years ago

To be honest i've not tried adding additional handlers for this event as the only place it's currently used (that I know of) is in the problems plugin. I'm not 100% sure that even works in that instance. I think Whoops might be handling the exception before this event is even fired.

Please create an issue for this onFatalException() event here: https://github.com/getgrav/grav/issues

10 years ago

Ok, i tested this a bit today. It is working fine after all. I created an onFatalExceptionevent in my testing plugin, and debugged it. It went in there fine. I was able to log out a message no problem. What I have added is the exception is now passed to the event so you can actually know what the exception was that cause the event in the first place: https://github.com/getgrav/grav/commit/d42786484f10a9ae23820400ace35867474498e0

Then you can use it like this:

PHP

    public function onFatalException(Event $e)
    {
        $exception = $e['exception'];
        $this->grav['log']->addCritical(">>> " . $exception->getMessage());
    }
---
10 years ago

Awesome, I made those changes and it does exactly what I want it to. Also, thanks for working on the weekend.

I see that commit is on the develop branch, are you planning on adding those index.php changes to an upcoming release?

10 years ago

Yes, they will be in the next RC release.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1368 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1125 9 years ago