handlePlatformException static method
Handles PlatformExceptions and converts them to appropriate YOLO exceptions.
e
The PlatformException to handle
context
Optional context string for more specific error messages
Returns the appropriate YOLOException based on the error code
Implementation
static YOLOException handlePlatformException(
PlatformException e, {
String? context,
}) {
final contextPrefix = context != null ? '$context: ' : '';
switch (e.code) {
case 'MODEL_NOT_FOUND':
return ModelLoadingException(
'${contextPrefix}Model file not found: ${e.message}',
);
case 'INVALID_MODEL':
return ModelLoadingException(
'${contextPrefix}Invalid model format: ${e.message}',
);
case 'UNSUPPORTED_TASK':
String taskName = 'unknown';
if (context != null && context.contains('task ')) {
final match = RegExp(r'task (\w+)').firstMatch(context);
if (match != null) {
taskName = match.group(1) ?? 'unknown';
}
}
return ModelLoadingException(
'${contextPrefix}Unsupported task type: $taskName',
);
case 'MODEL_FILE_ERROR':
return ModelLoadingException(
'${contextPrefix}Failed to load model: ${e.message}',
);
case 'MODEL_NOT_LOADED':
return ModelNotLoadedException(
'${contextPrefix}Model has not been loaded. Call loadModel() first.',
);
case 'INVALID_IMAGE':
return InvalidInputException(
'${contextPrefix}Invalid image format or corrupted image data',
);
case 'IMAGE_LOAD_ERROR':
return InferenceException(
'${contextPrefix}Platform error during inference: ${e.message}',
);
case 'INFERENCE_ERROR':
return InferenceException(
'${contextPrefix}Error during inference: ${e.message}',
);
default:
return InferenceException(
'${contextPrefix}Platform error: ${e.message}',
);
}
}