> 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/pei-zhi-sentry.md).

# 配置 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。

在 App in Toss 环境中无法使用原生错误追踪功能，因此 `enableNative` 选项 `false`需要设为 false。

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

在 App 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` 条目中。在 App in Toss 环境中， **`useClient` 选项必须 `false`设为**。

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

`useClient`的 `false`设置为 false 时，应用构建时不会自动将 source map 上传到 Sentry。在 App 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. 将 source map 上传到 Sentry

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

执行下面的命令即可上传 source map。

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

* `<API_KEY>`: 这是在 App 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
