npm package: @outloud/adonisjs-sentry
Description: Sentry integration for AdonisJS 6/7.
Latest version: 1.0.0

# AdonisJS Sentry

Sentry integration for [AdonisJS](https://adonisjs.com/) version 6/7.

<p>

[![typescript-image]][typescript-url]
[![npm-image]][npm-url]
[![npm-download-image]][npm-download-url]
[![license-image]][license-url]

</p>

---
## Getting Started

Install the package from the npm registry and configure it.

```bash
node ace add @outloud/adonisjs-sentry
```

See `config/sentry.ts` for available configuration options.

## Usage

The package will automatically register a middleware and configure the Sentry SDK.

```ts
import type { HttpContext } from '@adonisjs/core/http'
import sentry from '@outloud/adonisjs-sentry/service'

export default class HelloController {
  greet({ params, response}: HttpContext) {
    sentry.captureMessage(`Hello, ${params.name}!`)

    return response.ok({ message: `Hello, ${params.name}!` })
  }
}
```

The SDK is automatically scoped to the current request.

```ts
import { inject } from '@adonisjs/core'
import sentry from '@outloud/adonisjs-sentry/service'

@inject()
export class GreetingService {  
  greet(name: string) {
    sentry.captureMessage(`Hello, ${name}!`)
    
    return `Hello, ${name}!`
  }
}
```

### Capturing Errors

You can capture errors by calling the `captureException` method on the SDK inside your exception handler.

```ts
import sentry from '@outloud/adonisjs-sentry/service'

export default class HttpExceptionHandler extends ExceptionHandler {
  // ...

  async report(error: unknown, ctx: HttpContext) {
    if (this.shouldReport(error as any)) {
      sentry.captureException(error)
    }

    return super.report(error, ctx)
  }
}
```

### Assigning User Context

You can assign user context to the Sentry SDK by calling the `setUser` method on the SDK once you are logged in.

```ts
import sentry from '@outloud/adonisjs-sentry/service'

export default class SilentAuthMiddleware {
  async handle(ctx: HttpContext, next: NextFn) {
    // We are authenticating the user
    await ctx.auth.check()

    // If the user is authenticated, we assign the user context to Sentry
    if (ctx.auth.isAuthenticated) {
      const user = ctx.auth.getUserOrFail()
      
      sentry.setUser({
        id: user.id, 
        email: user.email, 
        username: user.username,
      });
    }
    
    return await next();
  }
}
```

### Adding Integrations

Sentry provides multiple integrations to enhance the data captured by the SDK. You can add integrations by changing the `integrations` array inside the configuration `config/sentry.ts`.

For example, if you want to add profiling to your application, you can add the `Profiler` integration.

```sh
npm install @sentry/profiling-node
```

```ts
// config/sentry.ts

import { nodeProfilingIntegration } from '@sentry/profiling-node';

export default defineConfig({
  // ...
  integrations: [nodeProfilingIntegration()],
  profilesSampleRate: 0.2,
})
```

[npm-image]: https://badgen.net/npm/v/@outloud/adonisjs-sentry/latest
[npm-url]: https://npmjs.org/package/@outloud/adonisjs-sentry "npm"

[npm-download-image]: https://badgen.net/npm/dm/@outloud/adonisjs-sentry
[npm-download-url]: https://npmcharts.com/compare/@outloud/adonisjs-sentry?minimal=true "downloads"

[typescript-image]: https://img.shields.io/badge/TypeScript-007ACC?logo=typescript&logoColor=white
[typescript-url]: https://www.typescriptlang.org "TypeScript"

[license-image]: https://img.shields.io/npm/l/@outloud/adonisjs-sentry.svg?sanitize=true
[license-url]: LICENSE.md "license"