Error Handling
Error handling in MoBro plugins follows practices similar to other .NET applications.
Plugins should catch and handle all exceptions and errors that do not render the plugin inoperable.
If an exception is raised and not caught by the plugin's implementation, MoBro will catch and log the exception, then
terminate the plugin. The plugin's status will change to Error
, and the exception's message will be displayed as the
error message.
For situations such as accessing external services or APIs where requests may occasionally fail, it is recommended to
implement retry policies or other error-handling mechanisms.
A single failed request, such as one caused by a temporary network issue, should not terminate the plugin.
There are two additional exception-handling options specific to MoBro plugins:
- Manually notifying the MoBro service of an unrecoverable error instead of throwing an exception.
- Throwing a custom
PluginException
(provided by the SDK) to include additional error details.
Manual Error Notification
The IMoBroService provides
dedicated Error functions to notify MoBro about unrecoverable
errors.
Calling these functions logs the error, terminates the plugin, and sets its state to Error
with the provided error
message.
Important: Calling any of these error functions triggers the same behavior as an uncaught exception.
PluginException
The plugin SDK includes several custom PluginException
classes for conveying additional error details to MoBro.
PluginException
Represents a generic error that occurs during plugin execution.
Parameters
Name | Type | Description |
---|---|---|
message | string? | A descriptive error message. |
innerException | Exception? | An inner exception providing additional details. |
Features
- Allows adding additional details using the
AddDetail
method. - Stores additional details in the
Details
dictionary, where keys and values can provide specific context.
Example
Adding custom details to a generic plugin exception:
try
{
// Plugin logic...
}
catch (Exception e)
{
throw new PluginException("A generic plugin error occurred", e)
.AddDetail("timestamp", DateTime.UtcNow.ToString("o"))
.AddDetail("pluginVersion", "1.2.3");
}
PluginDependencyException
This exception indicates that an error has occurred in an external dependency (e.g., another program, web API, etc.).
Parameters
Name | Type | Description |
---|---|---|
message | string? | The error message |
innerException | Exception? | An inner exception providing additional details |
Example
In this example, _apiClient
implements retries with exponential backoff:
try
{
return _apiClient.GetCoinMarkets(...);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to fetch data from CoinGecko API");
throw new PluginDependencyException("Failed to fetch data from CoinGecko: " + e.Message, e);
}
PluginSettingsException
This exception indicates that the plugin is missing a required setting or a provided setting value is invalid.
Use the IMoBroSettings.GetValue<T>(string): T
function to
automatically raise an appropriate PluginSettingsException
when a setting with the given key does not exist or lacks a
valid value.
Parameters
Name | Type | Description |
---|---|---|
field | string? | The key of the setting field causing the error |
message | string? | The error message |
innerException | Exception? | An inner exception providing additional details |
Example
throw new PluginSettingsException("update_frequency", "Invalid update frequency", null);
MoBroItemValidationException
Indicates that a passed IMoBroItem
was invalid.
Parameters
Name | Type | Description |
---|---|---|
mobroItemId | string | The ID of the invalid IMoBroItem . |
field | string? | The name of the invalid field (if applicable). |
message | string? | A descriptive error message. |
innerException | Exception? | An inner exception providing additional details. |
Features
- Adds additional details to the exception using
itemId
and optionallyfield
.
Example
try
{
ValidateItem(item);
}
catch (Exception e)
{
throw new MoBroItemValidationException("item_123", "fieldName", "Invalid field value", e);
}
MetricValueValidationException
Indicates that an invalid value was passed for a metric.
Parameters
Name | Type | Description |
---|---|---|
metricId | string | The ID of the metric causing the exception. |
message | string? | A descriptive error message. |
innerException | Exception? | An inner exception providing additional details. |
Features
- Adds additional details to the exception using
metricId
.
Example
try
{
LogMetricValue(metricId, value);
}
catch (Exception e)
{
throw new MetricValueValidationException("metric_456", "Invalid value for metric", e);
}