A Sentry account [(If you don’t have one, you can sign up here )]
A Sentry project
Overview
This guide will show you how to use SDK hooks to integrate error collection into a TypeScript SDK with the Sentry Node SDK, allowing you to insert custom actions at various stages of the SDK’s execution:
On SDK Initialization
Before a request is executed
After a successful response
After an error response
Adding the Sentry SDK to your project
To add the Sentry SDK to your project, you will need to add the dependency to your Speakeasy SDK’s gen.yaml file under the dependancies section:
After adding the dependency, the Sentry SDK will be included in your projects package.json file every time you generate your SDK.
Adding your first SDK hook
With the Sentry SDK included in your project, you can start adding SDK hooks.
Create a new file: In the src/hooks directory, create a file named error_hooks.ts.
Import hook types and Sentry SDK: In this file, import the necessary hook types and initialize the Sentry SDK with your project’s DSN.
Next create an ErrorHooks class to hold your hooks.
This hook allows us to inject custom code that runs on error responses and capture an SDK Error event to Sentry at the time the error occurs.
Here are the key points to the class outlined below:
Use the AfterErrorHook interface to define the type of the hook.
Capture the hookCtx, response, and error in the afterError method.
Return the response and error so that the SDK can continue to process the error.
Specific notes for using Sentry here:
Add a breadcrumb to Sentry to capture additional details regarding the error.
capturing the error in Sentry using Sentry.captureException(error).
Once this hook is implemented, you can now use the ErrorHooks class in your SDK.
In the src/hooks/registration.ts file import and register the class from the file you created the hooks in:
You can now regenerate your SDK and use the new hooks.
Running API calls with this SDK will send error events to your Sentry project, where you can review all the details for each error.
Review all the code outlined in this guide by visiting the SDK Hooks repository.