Skip to main content

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.

tip

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

NameTypeDescription
messagestring?The error message
innerExceptionException?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.

tip

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

NameTypeDescription
fieldstring?The key of the setting field causing the error
messagestring?The error message
innerExceptionException?The inner exception

Example

throw new PluginSettingsException("update_frequency", "Invalid update frequency", null);