• Projects
  • Service
  • About
  • branding.bz
  • Podcast
  • Tips
  • FAQ
  • Recruit
  • Download
  • Contact
  • branding.bz(ブランド構築SaaS)
  • DESIGN NOW(デザインメディア)
  • X
  • LinkedIn
  • Spotify
  • Facebook

213-0011 神奈川県川崎市高津区久本3-6-7-303

© 2026 ID INC. All rights reserved

claude-skills/スキル
SKILLOfficialdevelopment

output-error-try-catch

プラグイン
outputai
ソース
GitHub で見る ↗
説明

出力ワークフロー内で起きたエラーに対処する際に、エラーの詳細な情報を失わないようにします。 次のような場合に使用: - ワークフローに try/catch ロジック(実行結果の監視と異常処理)を追加する - エラー時の代替処理を設定する - 一部のステップだけが失敗した状況に対応する - 独自のエラークラスを作成する - ステップ実行やデータ検証時に投げられたエラーの型チェックを行う

原文を表示

Handle errors caught in Output workflows without losing failure context. Use when adding workflow try/catch logic, fallbacks, partial-failure handling, custom Error classes, or typed checks for errors thrown by steps and evaluators.

ユースケース
  • ワークフローにtry/catchロジックを追加する
  • エラー時の代替処理を設定する
  • 一部のステップだけが失敗した状況に対応する
  • エラーの型チェックを行う
本文(日本語訳)

ワークフローのエラー処理

ワークフロー内の catch ブロックが実行される仕組みを理解する

ステップと評価器は Temporal アクティビティ(ワークフロー実行システムの処理単位)として動作します。Temporal は設定されたリトライポリシー(失敗時の再試行ルール)を適用した後、成功結果を返すか最終的なエラーをワークフローコードにスローします。

ステップの周囲にあるワークフローの catch は、そのステップが Activity のリトライをすべて消費した後にのみ実行されます。このエラーをキャッチすることは、Activity リトライポリシーを無効化したりバイパスしたりしません。

catch ブロックは、ワークフローが最終的なエラーに対してどう対応するかを決定します:

  • フォールバック値(代替値)を返す
  • 別のステップを実行する
  • 部分的な失敗を記録して続行する
  • 予期されたエラータイプを処理する
  • エラーを再スローしてワークフローを失敗させる

予期しないエラーは伝播させる

エラーをラップしたり単にログ出力するだけの catch ブロックを追加しないでください。Temporal の失敗タイプ、エラー連鎖、リトライのメタデータを失うことになります。

// 避けるべき例:元の Temporal エラー連鎖を置き換える
try {
  return await fetchData( input );
} catch ( error ) {
  throw new Error( `Fetch failed: ${error.message}` );
}

ワークフローが復旧できない場合は、ステップのエラーをそのまま伝播させます:

export default workflow( {
  name: 'fetch_workflow',
  fn: async input => fetchData( input )
} );

catch ブロックが既知のエラーのみを処理する場合は、常に他のすべてのエラーを再スローしてください。

hasErrorType で特定のエラータイプをチェックする

ステップまたは評価器からワークフローコードに渡るエラーは、Temporal エラー連鎖(複数のエラーが関連付けられた構造)にシリアライズされます。元の JavaScript オブジェクトの識別情報は保持されないため、error instanceof CustomError は信頼できません。

@outputai/core から hasErrorType を使用してください。これはエラー連鎖全体を走査して、ネイティブなインスタンスと Temporal がシリアライズした type および name フィールドにマッチします。

import { hasErrorType, workflow } from '@outputai/core';
import { lookupCompany } from './steps.js';
import { CompanyNotFoundError } from './types.js';

export default workflow( {
  name: 'company_lookup',
  fn: async input => {
    try {
      return await lookupCompany( input );
    } catch ( error ) {
      if ( hasErrorType( error, CompanyNotFoundError ) ) {
        return null;
      }

      throw error;
    }
  }
} );

カスタムエラークラスはワークフローとステップの両方がインポートする共有モジュールで定義してください:

export class CompanyNotFoundError extends Error {}

固定個数の .cause レベルを検査したり、外側の ActivityFailure だけをチェックしないでください。

有効な try/catch パターン

フォールバックステップ

const fetchWithFallback = async input => {
  try {
    return await fetchFromPrimarySource( input );
  } catch {
    return fetchFromSecondarySource( input );
  }
};

これは、プライマリソースのすべての失敗がフォールバックをトリガーすべき場合にのみ使用してください。特定の失敗のみが復旧可能な場合は hasErrorType を使用してください。

部分的な失敗処理

const results = await Promise.all( input.items.map( async item => {
  try {
    return { item, value: await processItem( item ), ok: true };
  } catch ( error ) {
    const message = error instanceof Error ? error.message : String( error );
    return { item, error: message, ok: false };
  }
} ) );

ワークフロー出力スキーマが成功と失敗の両方のエントリをサポートしていることを確認してください。失敗を黙って省略しないでください。

Activity リトライを別途設定する

ステップと評価器のリトライ動作をワークフロー Activity オプションで設定します。ワークフロー catch ブロックは、このポリシーを消費した後に残ったエラーのみを処理します。

export default workflow( {
  name: 'company_lookup',
  fn: async input => lookupCompany( input ),
  options: {
    activityOptions: {
      retry: {
        initialInterval: '1s',
        maximumAttempts: 3
      }
    }
  }
} );

確認チェックリスト

  • ワークフローが定義された復旧手段を持つ場合のみキャッチしてください
  • ステップまたは評価器から発生する特定のエラーには hasErrorType を使用してください
  • マッチしなかったエラーは変更せずに再スローしてください
  • 既存の失敗を汎用的な Error で置き換えないでください
  • カスタムエラークラス名は安定した状態を保ってください。Temporal が失敗タイプとしてシリアライズします
  • リトライ設定をワークフロー catch 動作から独立して設定してください
原文(English)を表示

Handle Errors in Workflows

Understand when workflow catch blocks run

Steps and evaluators run as Temporal Activities. Temporal applies their configured retry policy before returning a successful result or throwing a final failure to workflow code.

A workflow catch around a step therefore runs only after the step has exhausted its Activity retries. Catching that failure does not disable or bypass the Activity retry policy.

The catch block decides what the workflow does with the final failure:

  • Return a fallback value.
  • Run an alternative step.
  • Record a partial failure and continue.
  • Handle an expected error type.
  • Rethrow the error and fail the workflow.

Let unexpected failures propagate

Do not add a catch block that only wraps or logs an error. It can discard Temporal's failure type, cause chain, and retry metadata.

// Avoid: replaces the original Temporal failure chain.
try {
  return await fetchData( input );
} catch ( error ) {
  throw new Error( `Fetch failed: ${error.message}` );
}

When the workflow cannot recover, let the step failure propagate:

export default workflow( {
  name: 'fetch_workflow',
  fn: async input => fetchData( input )
} );

If a catch block handles only known failures, always rethrow everything else.

Check specific error types with hasErrorType

Errors crossing from a step or evaluator into workflow code are serialized into a Temporal failure cause chain. The original JavaScript object identity is not preserved, so error instanceof CustomError is unreliable.

Use hasErrorType from @outputai/core. It walks the cause chain and matches native instances and Temporal's serialized type and name fields.

import { hasErrorType, workflow } from '@outputai/core';
import { lookupCompany } from './steps.js';
import { CompanyNotFoundError } from './types.js';

export default workflow( {
  name: 'company_lookup',
  fn: async input => {
    try {
      return await lookupCompany( input );
    } catch ( error ) {
      if ( hasErrorType( error, CompanyNotFoundError ) ) {
        return null;
      }

      throw error;
    }
  }
} );

Define custom Error classes in a shared module imported by both the workflow and step:

export class CompanyNotFoundError extends Error {}

Do not inspect a fixed number of .cause levels or check only the outer ActivityFailure.

Valid try/catch patterns

Fallback step

const fetchWithFallback = async input => {
  try {
    return await fetchFromPrimarySource( input );
  } catch {
    return fetchFromSecondarySource( input );
  }
};

Use this only when every primary-source failure should trigger the fallback. Use hasErrorType when only specific failures are recoverable.

Partial failures

const results = await Promise.all( input.items.map( async item => {
  try {
    return { item, value: await processItem( item ), ok: true };
  } catch ( error ) {
    const message = error instanceof Error ? error.message : String( error );
    return { item, error: message, ok: false };
  }
} ) );

Ensure the workflow output schema supports both successful and failed entries. Do not silently omit failures.

Configure Activity retries separately

Configure step and evaluator retry behavior through workflow Activity options. The workflow catch block handles only the failure that remains after this policy is exhausted.

export default workflow( {
  name: 'company_lookup',
  fn: async input => lookupCompany( input ),
  options: {
    activityOptions: {
      retry: {
        initialInterval: '1s',
        maximumAttempts: 3
      }
    }
  }
} );

Review checklist

  • Catch only when the workflow has a defined recovery path.
  • Use hasErrorType for specific errors originating in steps or evaluators.
  • Rethrow unmatched errors unchanged.
  • Do not replace an existing failure with a generic Error.
  • Keep custom Error class names stable because Temporal serializes them as failure types.
  • Configure retries independently from workflow catch behavior.

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。