9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCode上からSlackにエラー報告してみた

Last updated at Posted at 2025-12-01

VSCodeで開発している時、よくターミナルにエラーが出ますよね。
エラーが出たらSlackで質問を送ると思うのですが、まずターミナルのエラーをコピーして、slackを開いて、エラー内容をコードブロックにして、、という工程が少々面倒くさいのでVSCode上で簡単に送れるように拡張機能を作ってみました。

開発手順

VSCode拡張機能を作成するための基本テンプレートを生成するため、まずYeomanとそのジェネレーターをインストールします。

npm install -g yo generator-code

インストールするとおじさんが出てきて可愛いです。

スクリーンショット 2025-12-01 1.55.40.png

インストール後、以下のコマンドでプロジェクトの雛形を作成します。

yo code

対話形式の選択肢が出てくるので、以下のように選択してください。
入力を終えると、ファイルを自動で生成してくれます。

スクリーンショット 2025-11-23 11.24.05.png

拡張機能を実装

拡張機能の中身はsrc/extention.tsに書きます。
まずWebhookURLを設定します。
WebhookURLについてはSlackAppを作る際に後ほど説明します。

extention.ts
const SLACK_WEBHOOK_URL = 'XXXXX;

拡張機能実装に必須なactivate関数の中に、コマンドの登録と実際の処理を記述していきます。
まずVSCodeのターミナルでユーザーが選択しているテキストを、クリップボード経由で読み取り、変数 errorTextに格納する処理を書きます。

extention.ts
// slack-error-reporter.sendErrorとして呼び出せるようにする
let disposable = vscode.commands.registerCommand('slack-error-reporter.sendError', async () => {
        
    // ターミナルの選択範囲をクリップボードにコピーさせる
    await vscode.commands.executeCommand('workbench.action.terminal.copySelection');

    // クリップボードからエラーテキストを読み取る
    const errorText = await vscode.env.clipboard.readText();

次に、VSCodeの上部にある入力ボックスを連続して表示し、ユーザーからエラー発生時に実行したコマンドとエラーの状況に関する補足コメントの2種類のテキスト情報を収集する処理を書きます。

extention.ts
// ユーザーにエラーがおきた時に実行したコマンドを入力してもらう
const executedCommand = await vscode.window.showInputBox({
    placeHolder: '実行したコマンドを入力(例: npm install --save-dev typescript)',
    prompt: 'ターミナルで実行したコマンドを入力'
});

// ユーザーに一言コメントを入力してもらう
const userComment = await vscode.window.showInputBox({
    placeHolder: 'エラーの状況や補足を入力(例: 設定ファイルを変更後、発生した)',
    prompt: 'Slackに送るメッセージを入力'
});

Slack Block Kitの形式でSlackに投稿されるメッセージを作成します。
ここで自由にSlackに投稿する定型文を設定することができます。
お疲れ様です。
作業中に以下のエラーが出ました。
お手隙の際にご確認お願いします🙇
よく言いそうなこの3文にしました。お辞儀の絵文字もつけときました。

extention.ts
// Slackに送るデータを作成
const payload = {
    text: "エラー相談の連絡", // 通知ポップアップに出る文字
    blocks: [
        {
            type: "section",
            text: {
                type: "mrkdwn",
                text: "お疲れ様です。\n作業中に以下のエラーが出ました。\nお手隙の際にご確認お願いします🙇‍♂️"
            }
        },
        // 実行コマンドのセクション
        {
            type: "section",
            fields: [
                {
                    type: "mrkdwn",
                    text: `*▼ 実行コマンド:*\n\`${executedCommand || "不明"}\`` // コマンドをインラインコードで表示
                }
            ]
        },
        // 状況・コメントのセクション
        {
            type: "section",
            fields: [
                {
                    type: "mrkdwn",
                    text: `*▼ 状況・コメント:*\n${userComment || "特になし"}`
                }
            ]
        },
        {
            type: "divider"
        },
        // エラーログのセクション
        {
            type: "section",
            text: {
                type: "mrkdwn",
                text: `*▼ エラーログ:*\n\`\`\`\n${errorText}\n\`\`\`` // エラーログをコードブロックで表示
            }
        }
    ]
};

最後にSlackに送信します。

extention.ts
await axios.post(SLACK_WEBHOOK_URL, payload);

package.jsonに以下を追加します。

pacage.json
{
  "name": "slack-error-reporter",
  "displayName": "slack-error-reporter",
  "description": "Send terminal errors to Slack",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.90.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [],
  "main": "./dist/extension.js",
  "contributes": {
    "commands": [
      {
        "command": "slack-error-reporter.sendError",
        "title": "Slackで質問する"
      }
    ],
    "menus": {
      "terminal/context": [
        {
          "command": "slack-error-reporter.sendError",
          "group": "navigation"
        }
      ]
    },
    "configuration": {
      "title": "Slack Error Reporter",
      "properties": {
        "slack-error-reporter.webhookUrl": {
          "type": "string",
          "default": "",
          "description": "Slack Incoming Webhook URL"
        }
      }
    }
  },

SlackAppを作成

Slackに投稿するためのSlackAppを作ります。
https://api.slack.com/apps にアクセスし以下の手順で作成します。
Create New App → From Scratch → App名を入力

Appの各種設定手順

Appのページの左側の項目で以下の項目を設定します。

Incoming Webhooks

オンにし、Add New WebhookでWebhookURLを追加

エラーを質問してみる

デバッグして実際にエラーを質問してみます。
ターミナルで出たエラーを選択し右クリックします。
するとSlackで質問するという項目が表示されます。

スクリーンショット 2025-12-01 1.28.07.png

Slackで質問するを選択すると、VSCode上部にあるコマンドパレットが開きます。
エラーが出る前に実行したコマンドを入力するように求められるので、入力します。

スクリーンショット 2025-12-01 1.29.10.png

次に、補足情報を入力します。ここでエラーが出た時の詳細などを記入できます。

スクリーンショット 2025-12-01 1.29.58.png

全て入力するとSlackに送信されます。
実際に投稿されているかチャンネルを確認します。

スクリーンショット 2025-12-01 1.31.36.png

投稿が確認できました!
これで、質問やエラー報告にかかる時間と手間を大幅に削減できたはずです。

まとめ

今回はVSCode拡張機能とSlackAppを組み合わせてエラー報告を効率化してみました。
皆さんもぜひ試してみてください。

9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?