> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/ai-vibe-coding/zh/integration/she-zhi-sentry.md).

# 设置 Sentry

在应用中 **Sentry**接入 Sentry 后，就可以自动检测并监控 JavaScript 中发生的错误。通过这样做，可以提高应用的稳定性，并为用户提供更好的体验。

{% hint style="info" %}
**WebView 中接入 Sentry**

[Sentry 官方指南](https://docs.sentry.io/platforms/javascript/)请参考进行接入。
{% endhint %}

### 1. Sentry 初始设置

[Sentry 官方指南](https://docs.sentry.io/platforms/react-native)请参考并在应用中初始化 Sentry。

由于在 Apps in Toss 环境中无法使用原生错误追踪功能， `enableNative` 选项 `false`必须设置为。

{% hint style="info" %}
**不支持原生错误追踪**

在 Apps in Toss 环境中只能追踪 JavaScript 错误。
{% endhint %}

```ts
import * as Sentry from '@sentry/react-native';

Sentry.init({
  // ...
  enableNative: false,
});
```

### 2. 安装 Sentry 插件

请在项目根目录中，根据所使用的包管理器执行相应命令来安装 Sentry 插件。

{% tabs %}
{% tab title="npm" %}

```sh
npm install @granite-js/plugin-sentry
```

{% endtab %}

{% tab title="pnpm" %}

```sh
pnpm install @granite-js/plugin-sentry
```

{% endtab %}

{% tab title="yarn" %}

```sh
yarn add @granite-js/plugin-sentry
```

{% endtab %}
{% endtabs %}

### 3. 配置插件

将已安装的 `@granite-js/plugin-sentry`添加到 `granite.config.ts` 文件的 `plugins` 项中。在 Apps in Toss 环境中， **`useClient` 选项必须 `false`设置为**。

{% hint style="info" %}
**为什么 `useClient` 要关闭该选项？**

`useClient`添加到 `false`设置为后，应用构建时不会自动将 source map 上传到 Sentry。在 Apps in Toss 环境中，构建后需要 **手动上传 source map**，因此需要关闭该选项。
{% endhint %}

```ts
import { defineConfig } from '@granite-js/react-native/config';
import { sentry } from '@granite-js/plugin-sentry'; // [!code highlight]
import { appsInToss } from '@apps-in-toss/framework/plugins';

export default defineConfig({
  // ...,
  plugins: [
    sentry({ useClient: false }), // [!code highlight]
    appsInToss({
      // ...
    }),
  ],
});
```

### 4. 发布应用

应用的发布方法请参考 [迷你应用发布](https://appsintoss.gitbook.io/appsintoss-docs/guide/operation/deploy) 文档。

### 5. 向 Sentry 上传 source map

为了准确追踪已发布迷你应用的错误，请将构建后生成的 **source map 上传到 Sentry**。

执行下面的命令后，source map 就会被上传。

{% hint style="info" %}
**输入值说明**

* `<API_KEY>`: 在 Apps in Toss 控制台中发放的 API 密钥。
* `<APP_NAME>`: 在 Sentry 中注册的服务名称。
* `<DEPLOYMENT_ID>`: 应用部署时使用的部署 ID。
  {% endhint %}

{% tabs %}
{% tab title="npm" %}

```sh
npx ait sentry upload-sourcemap \\
  --api-key <API_KEY> \\
  --app-name <APP_NAME> \\
  --deployment-id <DEPLOYMENT_ID>
```

{% endtab %}

{% tab title="pnpm" %}

```sh
pnpm ait sentry upload-sourcemap \\
  --api-key <API_KEY> \\
  --app-name <APP_NAME> \\
  --deployment-id <DEPLOYMENT_ID>
```

{% endtab %}

{% tab title="yarn" %}

```sh
yarn ait sentry upload-sourcemap \\
  --api-key <API_KEY> \\
  --app-name <APP_NAME> \\
  --deployment-id <DEPLOYMENT_ID>
```

{% endtab %}
{% endtabs %}

执行命令后，会要求输入 Sentry 的组织（Org）、项目（Project）和认证令牌。输入所有信息后，该服务的 source map 就会上传到 Sentry。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers-apps-in-toss.toss.im/ai-vibe-coding/zh/integration/she-zhi-sentry.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
