# Crash SDK設定

IMQA Crashには様々なオプションが存在します。ユーザーが希望する設定を追加することで、より詳細なクラッシュレポートを受け取ることができます。

## 1. Custom Crash発生

IMQA Crash Agent で以下のようなユーザー定義エラーを収集することができます。Exceptionオブジェクトといっしょに特定のタグ、エラーランクを指定することができます。

```java
 try {
  ...
} catch (SomeException e) {
  IMQACrashAgent.SendException(e, "ERROR TAG", ErrorRank.Major);
}
```

エラーランクは特に定められていないため、アプリの構成によって変更して使用することができます。

<table><thead><tr><th width="218.2087241113939">Rank</th><th width="413.3333333333333">Description</th></tr></thead><tbody><tr><td>ErrorRank.Unhandle</td><td>基本のエラーランクに分類できないクラッシュに使用</td></tr><tr><td>ErrorRank.Critical</td><td>アプリに大きな影響を与えるクラッシュ</td></tr><tr><td>ErrorRank.Major</td><td>アプリに影響を与えるクラッシュ</td></tr><tr><td>ErrorRank.Minor</td><td>アプリにほとんど影響を与えないクラッシュ</td></tr></tbody></table>

## 2. Custom Web Crash 発生

IMQA Webview Agentでは、詳細なWebエラー分析に必要なエラーメッセージを指定してカスタムエラーを収集できます。

{% hint style="warning" %}
Webview Crash 収集のためには CrashWebviewBridge を追加する必要があります。 「[Android > MPM SDK設定 >  WebView HTTPリクエスト >  Android Agent設定](https://docs.imqa.io/imqa-guide/jpn/installation/mpm-sdk-setting#android-agent)」を参照してください。
{% endhint %}

### カスタムエラーの生成

IMQA Crash は、ユーザーが定義したエラーを検出して収集します。

{% hint style="warning" %}
他のエラーであっても同じエラータイプの場合、同じエラーとして認識してエラーが蓄積することがあります。
{% endhint %}

### カスタムエラーメッセージの指定

WebViewが収集するデフォルトのエラーメッセージを目的のエラーメッセージに変更して収集できます。 詳細なWebエラー分析のためにエラーメッセージを指定することをお勧めします。

{% hint style="warning" %}
エラーメッセージを指定した場合、同じエラータイプであっても別のエラーとして収集されます。
{% endhint %}

{% code title="js files to collect custom errors" %}

```javascript
...
// When using try-catch
try {
	...
}
catch(e){
	throw new Error("Custom error messages");
}

// When to generate default errors
new Error("Custom error messages");
```

{% endcode %}

以下のように、IMQA Crash サービスで Web クラッシュを詳細な情報とともに確認できます。

<figure><img src="https://lh7-us.googleusercontent.com/CW4QD8o6NvrtFpAORvtdBUR8Cxbht1HyBEAsmolCEGG1hyJ13RrUbqbF7KWB62PqL0BPg5hFbHqFjRJYtOaS8E9PVRdiuwIT93NGZN53NRITlp5cVNOei4TgQP5SVOHkUBqVVjL99rlmKYS88hPiS30" alt=""><figcaption></figcaption></figure>

**❶ キャッチされたエラーインスタンス**\
Webビューでエラーが生成された場合は、そのエラーのキャッチされたエラーインスタンス名を表示します。

**❷ ユーザー定義のエラーメッセージ**\
特定のエラータイプのエラーが作成されるとき、ユーザーが指定したメッセージを表示します。

```javascript
throw new Error("Custom error messages");
```

**❸ エラータイプ / コードの行番号**\
Webビューエラーの種類と当該エラーの行番号を確認することができます。

<table><thead><tr><th width="179" align="center">Error Type</th><th align="center">Description</th></tr></thead><tbody><tr><td align="center">Error</td><td align="center">ユーザーが設定したエラータイプです。</td></tr><tr><td align="center">EvalError</td><td align="center"><mark style="color:red;">eval()</mark>で発生するエラーです。</td></tr><tr><td align="center">RangeError</td><td align="center">変数の有効な範囲を外れた場合に作成されるエラーです。</td></tr><tr><td align="center">ReferenceError</td><td align="center">誤った参照をした場合に作成されるエラーです。</td></tr><tr><td align="center">SyntaxError</td><td align="center">誤った構文がある場合のエラーです。</td></tr><tr><td align="center">TypeError</td><td align="center">有効なデータ型でない場合におけるタイプのエラーです。</td></tr><tr><td align="center">URIError</td><td align="center"><mark style="color:red;">encodeURI()</mark>や<mark style="color:red;">decodeURI()</mark>の関数に無効なパラメーターを渡した場合に作成されるエラーです。</td></tr><tr><td align="center">AggregateError</td><td align="center">複数のエラーを一つのエラーで包むエラータイプです。</td></tr></tbody></table>

&#x20;

## 3. EventPath登録 <a href="#eventpath" id="eventpath"></a>

EventPathは、ユーザーのデバイスでアプリがどのようなルートで動作するかを把握するための機能です。これを使用するためには、クラスにEventPath追跡コードを入れる必要があります。追跡コードに「STEP」と「LABEL」に該当する内容を入れてください。

| STEP                                         | LABEL                                                    |
| -------------------------------------------- | -------------------------------------------------------- |
| 収集するコールスタックの段階、当該ステップのクラス名、関数名、コール位置を取り込みます。 | 収集されたEventPathの別称、クラッシュ情報ウィンドウにEventPathの各発生位置を別称で表示します。 |

```java
public void onClickSomething() {
      EventPathManager.CreateEventPath(STEP, LABEL);
  }
```

## 4. Custom User ID登録 <a href="#custom-user-id" id="custom-user-id"></a>

クラッシュ発生時にユーザーを区分するためのカスタムしたIDを与えることができます。ユーザー情報を登録すると、発生するクラッシュにユーザー情報が含まれて表示されます。

<table><thead><tr><th width="185.56548708822163">Class Name</th><th>Method Name</th><th>Description</th></tr></thead><tbody><tr><td>IdentifierCollector</td><td>setUserId(STRING)</td><td>ユーザーIDを登録してください。</td></tr><tr><td>IdentifierCollector</td><td>setUserName(STRING)</td><td>ユーザー名を登録してください。</td></tr><tr><td>IdentifierCollector</td><td>setUserMail(STRING)</td><td>ユーザーのEメールアドレスを登録してください。</td></tr></tbody></table>

```javascript
IdentifierCollector.setUserId("10"); // Register a user ID.
IdentifierCollector.setUserName("My User Name"); // Register a user name.
IdentifierCollector.setUserMail("My User Email"); // Register a user email.
```

## 5. Custom User Log/Key登録 <a href="#custom-user-log-key" id="custom-user-log-key"></a>

クラッシュが発生する前に、希望するログを開発者が直接追加して残すことができます。(最大100個)

<table><thead><tr><th width="235">Class Name</th><th width="189.33333333333331">Method Name</th><th>Description</th></tr></thead><tbody><tr><td>CustomLogCollector</td><td>log(STRING)</td><td>ログを一般的な文字列として残します。</td></tr><tr><td>CustomLogCollector</td><td>log(KEY, VALUE)</td><td>ログをキー：値形式で残します。</td></tr></tbody></table>

```javascript
CustomLogCollector.log("test log"); // Leaves the log as a plain string.
CustomKeyCollector.log("My Key", "My Log"); // Leaves the log in the form of key:value.
```

## 6. 初期化Option追加 <a href="#option" id="option"></a>

IMQAController初期化時のオプションを指定することができます。IMQA CrashモードをパラメータやOptionを利用して変更することができます。

<table><thead><tr><th width="144.7827459212473">Class Name</th><th width="241.76997658734825">Method Name</th><th></th></tr></thead><tbody><tr><td>IMQAOption</td><td>setDirectUploadFlag(BOOL)</td><td>アップロードモードを設定します。Falseに設定すると、debug modeでも動作します。</td></tr><tr><td>IMQAOption</td><td>setPrintLog(BOOL)</td><td>Log出力状態を設定します。Trueに設定すると、 IMQAのLogが出力されます。</td></tr><tr><td>IMQAOption</td><td>setAnrOn(BOOL)</td><td>ANRのモニタリング状態を設定します。Falseに設定すると、ANRはクラッシュ収集から除外されます。</td></tr><tr><td>IMQAOption</td><td>setAnrTimeoutIntercal(int)</td><td>ANRタイムアウトが発生する時間を設定します。(milliseconds単位)</td></tr></tbody></table>

{% code title="MyApplication.java" %}

```java
@Override
public void onCreate() {
    super.onCreate();
      
    io.imqa.core.IMQAOption imqaOption = new io.imqa.core.IMQAOption();

    /*  
     * Default value: true
     * Sets whether to collect ANR information. 
     * true: Monitors the ANR occurrence status.
     * false: Stops monitoring ANR occurrence.
    */
    imqaOption.setAnrOn(false);


    /*
     * Default value : 5000
     * anrTimeoutInveral : Sets the time when ANR timeout occurs (in milliseconds.)
     */
    imqaOption.setAnrTimeoutInterval(5000);


   /*
     * Default value : false
     * Sets whether to output the log. 
     * true: Outputs the IMQA Crash log.
    */
    imqaOption.setPrintCrashLog(true);

    /*
     * Default value : true
     * Uploads collected crash information as soon as the crash occurs.
     * true: Uploads as soon as the crash occurs.
     * false: Uploads when executing again after saving.
    */
    imqaOption.setCrashDirectUploadFlag(false);

    /*
     * Default value : false
     * Set the compression algorithm.
     * true : Using Zstandard (Zstd).
     * false : Using gzip algorithm
    */
    imqaOption.setCompressZstd(true);

    /*
     * Default value : 'https://collector.imqa.io'
     * Sets the crash server URL.
     * String: Server information
     */
    imqaOption.setCrashServerUrl('https://collector.imqa.io');

    io.imqa.crash.IMQACrashAgent.InitializeAndStartSession(
        this, // Application Context
        BuildConfig.FLAVOR, // Project Flavor Setting
        "PROJECT_KEY",  // Issued project key
        imqaOption // Add an object when the option is set.
    );
}
```

{% endcode %}
