Hono error tracking installation

Last updated:

|Edit this page|
  1. Install PostHog Node.js SDK

    Required

    Error tracking enables you to track, investigate, and resolve exceptions your customers face. Start by installing the posthog-node package using your package manager:

    Terminal
    npm install --save posthog-node
  2. Exception handling example

    Required

    Hono uses app.onError to handle uncaught exceptions. You can take advantage of this for error tracking. Remember to export your project API key as an environment variable.

    index.ts
    import { env } from 'hono/adapter'
    import { PostHog } from 'posthog-node'
    app.onError((err, c) => {
    const { POSTHOG_PUBLIC_KEY } = env<{ POSTHOG_PUBLIC_KEY:string }>(c)
    const posthog = new PostHog(POSTHOG_PUBLIC_KEY, { host: 'https://us.i.posthog.com' })
    posthog.captureException(new Error(err.message, { cause: err }), 'user_distinct_id_with_err_rethrow', {
    path: c.req.path,
    method: c.req.method,
    url: c.req.url,
    headers: c.req.header(),
    // ... other properties
    })
    posthog.shutdown()
    // other error handling logic
    return c.text('Internal Server Error', 500)
    })
  3. Verify error tracking

    Checkpoint
    Confirm events are being sent to PostHog

    Before proceeding, let's make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.


    Activity feed with events
    Check for exceptions in PostHog
  4. Upload source maps

    Required

    Great, you're capturing exceptions! If you serve minified bundles, the next step is to upload source maps to generate accurate stack traces.

    Let's continue to the next section.

    Upload source maps

Questions? Ask Max AI.

It's easier than reading through 698 pages of documentation

Community questions

Was this page useful?

Next article

Manual error tracking installation

Error tracking enables you to track, investigate, and resolve exceptions your customers face. If you're using a different SDK or the API, you can manually capture errors by using the capture API or calling the capture method to capture an $exception event with the following properties: Property Description $exception_list A list of exception objects with detailed information about each error $exception_fingerprint (Optional) The identifier used to group issues. If not set, a unique hash…

Read next article