スキル一覧に戻る
AVRA-CADAVRA

native-code-error-handling

by AVRA-CADAVRA

0🍴 0📅 2026年1月24日
GitHubで見るManusで実行

SKILL.md


name: native-code-error-handling description: Guides native code error handling patterns: exception conversion, error propagation to Dart, user-friendly messages. Use when implementing native code error handling, platform channel error handling, or FFI error handling.

Native Code Error Handling

Core Principle

Native code errors must be converted to Dart-friendly errors with user-friendly messages.

Platform Channel Error Handling

Swift/iOS

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    do {
        let resultData = try performOperation()
        result(resultData)
    } catch let error as SpecificError {
        result(FlutterError(
            code: "SPECIFIC_ERROR",
            message: "User-friendly message: \(error.localizedDescription)",
            details: nil
        ))
    } catch let error {
        result(FlutterError(
            code: "OPERATION_ERROR",
            message: "Operation failed: \(error.localizedDescription)",
            details: nil
        ))
    }
}

Kotlin/Android

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
    .setMethodCallHandler { call, result ->
        try {
            when (call.method) {
                "performOperation" -> {
                    val resultData = performOperation()
                    result.success(resultData)
                }
                else -> result.notImplemented()
            }
        } catch (e: SpecificException) {
            result.error(
                "SPECIFIC_ERROR",
                "User-friendly message: ${e.message}",
                null
            )
        } catch (e: Exception) {
            result.error(
                "OPERATION_ERROR",
                "Operation failed: ${e.message}",
                e.stackTraceToString()
            )
        }
    }

FFI Error Handling

Rust FFI

#[no_mangle]
pub extern "C" fn rust_operation() -> c_int {
    match perform_operation() {
        Ok(_) => 0, // Success
        Err(e) => {
            // Log error
            eprintln!("Error: {}", e);
            -1 // Error code
        }
    }
}

Dart FFI

int performOperation() {
  final result = rustOperation();
  if (result != 0) {
    throw PlatformException(
      code: 'OPERATION_ERROR',
      message: 'Operation failed with error code: $result',
    );
  }
  return result;
}

Error Code Standards

Use consistent error codes:

  • OPERATION_ERROR - Generic operation failure
  • PERMISSION_DENIED - Permission denied
  • NOT_AVAILABLE - Feature not available
  • INVALID_ARGUMENT - Invalid input
  • NETWORK_ERROR - Network failure
  • TIMEOUT - Operation timeout

User-Friendly Messages

Convert technical errors to user-friendly messages:

private func getUserFriendlyMessage(from error: Error) -> String {
    switch error {
    case is PermissionDeniedError:
        return "Please grant required permissions in Settings"
    case is NetworkError:
        return "Connection failed. Please check your internet."
    case is TimeoutError:
        return "Operation timed out. Please try again."
    default:
        return "Something went wrong. Please try again."
    }
}

Reference

  • Platform-specific error handling in iOS/Android code
  • Flutter Platform Channels error handling documentation

スコア

総合スコア

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

レビュー

💬

レビュー機能は近日公開予定です