classifyYamuxException static method
Classifies and wraps exceptions that occur in Yamux operations
Implementation
static YamuxException classifyYamuxException(
dynamic exception, // Changed to dynamic to handle both Exception and Error
StackTrace? stackTrace, {
int? streamId,
String? operation,
String? currentState,
Map<String, dynamic>? context,
}) {
final baseContext = <String, dynamic>{
'timestamp': DateTime.now().toIso8601String(),
'operation': operation ?? 'unknown',
if (streamId != null) 'streamId': streamId,
if (currentState != null) 'currentState': currentState,
...?context,
};
// Handle StateError specifically (most common Yamux stream error)
// StateError extends Error, not Exception
if (exception is StateError) {
final stateError = exception as StateError;
final message = stateError.message;
// Check for specific state-related errors
if (message.contains('reset') || message.contains('Reset')) {
return YamuxStreamStateException(
'Stream operation failed: stream is in reset state',
currentState: 'reset',
requestedOperation: operation ?? 'unknown',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
if (message.contains('closed') || message.contains('Closed')) {
return YamuxStreamStateException(
'Stream operation failed: stream is closed',
currentState: 'closed',
requestedOperation: operation ?? 'unknown',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
if (message.contains('closing') || message.contains('Closing')) {
return YamuxStreamStateException(
'Stream operation failed: stream is closing',
currentState: 'closing',
requestedOperation: operation ?? 'unknown',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
// Generic state error
return YamuxStreamStateException(
'Stream operation failed due to invalid state: $message',
currentState: currentState ?? 'unknown',
requestedOperation: operation ?? 'unknown',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
// Handle timeout exceptions
if (exception is TimeoutException) {
return YamuxStreamTimeoutException(
'Yamux stream operation timed out: ${exception.toString()}',
timeout: exception.duration ?? const Duration(seconds: 30),
operation: operation ?? 'unknown',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
// Handle socket exceptions (underlying transport issues)
if (exception is SocketException) {
return YamuxStreamProtocolException(
'Yamux stream socket error: ${exception.toString()}',
protocolError: 'socket_error',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
// Handle format exceptions (protocol parsing errors)
if (exception is FormatException) {
return YamuxStreamProtocolException(
'Yamux stream protocol format error: ${exception.toString()}',
protocolError: 'format_error',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}
// Generic Yamux exception for unclassified errors
return YamuxStreamProtocolException(
'Yamux stream error: ${exception.toString()}',
protocolError: 'unknown_error',
streamId: streamId ?? -1,
originalException: exception,
originalStackTrace: stackTrace,
context: baseContext,
);
}