← スキル一覧に戻る

error-handling-patterns
by AVRA-CADAVRA
⭐ 0🍴 0📅 2026年1月24日
SKILL.md
name: error-handling-patterns description: Enforces comprehensive error handling: explicit catch blocks, user-friendly messages, Result/Either patterns. Use when writing async code, service methods, or handling exceptions.
Error Handling Patterns
Mandatory Rules
✅ ALWAYS handle errors explicitly - never silently swallow exceptions
✅ Use try-catch blocks for async operations that can fail
✅ Provide user-friendly error messages in UI
✅ Log errors with context using developer.log()
❌ NEVER use empty catch blocks without logging
Basic Pattern
try {
await operation();
} on SpecificException catch (e) {
developer.log('Operation failed', error: e, name: 'ServiceName');
// Handle specific error
} catch (e, st) {
developer.log('Unexpected error', error: e, stackTrace: st, name: 'ServiceName');
// Handle generic error
}
Async Operations
Always handle errors in async methods:
Future<Result<String>> fetchData() async {
try {
final data = await repository.fetch();
return Result.success(data);
} on NetworkException catch (e) {
developer.log('Network error', error: e, name: 'DataService');
return Result.failure('Connection failed. Please check your internet.');
} on ValidationException catch (e) {
developer.log('Validation error', error: e, name: 'DataService');
return Result.failure('Invalid data format.');
} catch (e, st) {
developer.log('Unexpected error', error: e, stackTrace: st, name: 'DataService');
return Result.failure('Something went wrong. Please try again.');
}
}
Service Error Handling
class MyService {
final AppLogger _logger = const AppLogger(defaultTag: 'MyService');
Future<void> performOperation() async {
try {
await riskyOperation();
_logger.info('Operation completed successfully');
} on SpecificException catch (e) {
_logger.error('Specific error occurred', error: e);
throw ServiceException('User-friendly message');
} catch (e, st) {
_logger.error('Unexpected error', error: e, stackTrace: st);
throw ServiceException('Something went wrong');
}
}
}
BLoC Error Handling
Future<void> _onLoadData(
LoadData event,
Emitter<MyState> emit,
) async {
emit(MyLoading());
try {
final data = await useCase.getData();
emit(MyLoaded(data));
} on NetworkException catch (e) {
emit(MyError('Connection failed. Please check your internet.'));
} catch (e, st) {
developer.log('Unexpected error', error: e, stackTrace: st, name: 'MyBloc');
emit(MyError('Something went wrong. Please try again.'));
}
}
Result/Either Patterns
For operations that can fail, consider Result pattern:
// Result pattern
sealed class Result<T> {
const Result();
}
class Success<T> extends Result<T> {
final T value;
const Success(this.value);
}
class Failure<T> extends Result<T> {
final String error;
const Failure(this.error);
}
// Usage
Future<Result<User>> getUser(String id) async {
try {
final user = await repository.getUser(id);
return Success(user);
} catch (e) {
return Failure('Failed to load user: ${e.toString()}');
}
}
UI Error Display
Always show user-friendly messages:
// In widget
if (state is MyError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.message), // User-friendly message
backgroundColor: AppColors.error,
),
);
}
Anti-Patterns
❌ Silent Failure:
try {
await operation();
} catch (e) {
// Silent failure - BAD!
}
❌ Generic Catch Only:
try {
await operation();
} catch (e) {
// Missing specific exception handling - BAD!
}
❌ No Logging:
try {
await operation();
} catch (e) {
showError('Error'); // Missing error logging - BAD!
}
Best Practices
- Catch specific exceptions first, then generic
- Always log errors with context and stack trace
- Provide user-friendly messages (hide technical details)
- Use Result/Either patterns for operations that can fail
- Don't swallow exceptions without handling or logging
スコア
総合スコア
60/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です