Error handling
Error handling for MoBro plugins does not differ much from any other .NET application.
The plugin should catch and handle all exceptions and errors that do not render the plugin inoperable.
If an exception is raised that is not caught by the plugins implementation, MoBro will catch and log that exception and
terminate the plugin afterward. The plugins status will switch to Error
and the exceptions message is used as the
error message.
When accessing external services, APIs, etc. where requests/invocations may sometimes fail, it's best to implement some
sort of retry policy or other error handling.
The plugin shouldn't be terminated due to a single failed request caused by a temporary network error.
There are two additional options of dealing with exceptions 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
The IMoBroService exposes dedicated Error functions to notify MoBro that an unrecoverable error has
occurred.
Calling any of these functions will cause MoBro to log the error, terminate the plugin and switch its state to Error
using the provided error message.
Calling any of the error functions triggers the same behaviour as an uncaught exception.
PluginException
The plugin SDK includes several custom PluginExceptions that may be used to convey additional error details to MoBro.
PluginDependencyException
Indicates that an error occurred in an external dependency (e.g. another program, web API,...)
Parameters
Name | Type | Description |
---|---|---|
message | string? | The error message |
innerException | Exception? | The inner exception |
Example
Note that the _apiClient
in this example already implements a certain number of retries with an 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
Indicates that the plugin is missing a required setting or the provided setting value is invalid.
Using the IMoBroSettings.GetValue<T>(string): T
function will
automatically raise an appropriate PluginSettingsException
if the setting with the given key does not exist or has no
value.
Parameters
Name | Type | Description |
---|---|---|
field | string? | The key of the setting field causing the error |
message | string? | The error message |
innerException | Exception? | The inner exception |
Example
throw new PluginSettingsException("update_frequency", "Invalid update frequency", null);