出力ワークフロー内で起きたエラーに対処する際に、エラーの詳細な情報を失わないようにします。 次のような場合に使用: - ワークフローに 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.
ステップと評価器は 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 ブロックが既知のエラーのみを処理する場合は、常に他のすべてのエラーを再スローしてください。
ステップまたは評価器からワークフローコードに渡るエラーは、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 だけをチェックしないでください。
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 オプションで設定します。ワークフロー catch ブロックは、このポリシーを消費した後に残ったエラーのみを処理します。
export default workflow( {
name: 'company_lookup',
fn: async input => lookupCompany( input ),
options: {
activityOptions: {
retry: {
initialInterval: '1s',
maximumAttempts: 3
}
}
}
} );
hasErrorType を使用してくださいError で置き換えないでください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:
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.
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.
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.
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 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
}
}
}
} );
hasErrorType for specific errors originating in steps or evaluators.Error.原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。