Framework hooks are essentially events that are emitted that you are able to "hook" into. If you want to add support for Sentry, which is an exception and error tracking solution, then you want to tie into the Exception Hook. When an exception is encountered it will look for your class in the container and execute it.
For the purposes of learning about Framework Hooks, we will walk through how to implement Sentry into a Masonite application by adding it to the Exception Hook.
The Exception Hook is fired when an exception is thrown in an application. Anytime you would normally see the debug view when developing is when this hook will fire. This hook may not be fired if the server does not boot up because of an exception depending on how far into the container the exception is thrown but any normal application exceptions encountered will fire this hook.
The exception hook to tie into is ExceptionHook. This means that the key in the container should end with ExceptionHook and Masonite will call it when the Exception Hook is thrown. We can load things into the container such as:
Notice here that our key ends with ExceptionHook and that we instantiated the object. Let's explore creating this entirly from scratch.
Let's explore how we can simply add Sentry to our application. This should take about 5 minutes.
Let's create a class called
SentryHook and put it into
class SentryHook:def __init__(self):passdef load(self, app):self._app = app
This should be the basic structure for a hook. All hooks require a load method. This load method will always be passed the application container so it always requires that parameter. From here we can do whatever we need to by making objects from the container.
But for this example we actually don't need the container so we can ignore it.
Ok great so now let's add sentry to our application. You can sign up with Sentry.io which will show you the basic 3 lines you need to add Sentry to any Python project.
This should be the finished hook:
from raven import Client client = Client( 'https://874..:firstname.lastname@example.org/1234567')class SentryHook:def __init__(self):passdef load(self, app):client.captureException()
That's it. The key in the client should be available through the Sentry.io dashboard.
Now let's walk through how we can simply tie this into the container so it will be called when an exception is thrown in our project.
We can create a new Service Provider to store our hooks so let's make one.
$ craft provider HookProvider
This will create a new Service Provider inside
app/providers/HookProvider.py that looks like:
from masonite.provider import ServiceProviderclass SentryServiceProvider(ServiceProvider):def register(self):passdef boot(self):pass
Now let's just add our hook to it:
from masonite.provider import ServiceProviderfrom ..hooks.sentry import SentryHookclass SentryServiceProvider(ServiceProvider):def register(self):self.app.bind('SentryExceptionHook', SentryHook())def boot(self):pass
And finally add the Service Provider to our
PROVIDERS constant in our
from app.providers.SentryServiceProvider import SentryServiceProvider...# Application ProvidersUserModelProvider,MiddlewareProvider,# Sentry ProviderSentryServiceProvider,...
That's it! Now everytime an exception is thrown, this will run our SentryHook class that we binded into the container and the exception should pop up in our Sentry.io dashboard.