Error Handling
Which errors the core catches, and how plugins should handle errors in methods they export.
Plugify handles errors differently in two places: while it loads and drives extensions, and while plugins call each other.
Errors the core handles
The core checks everything it does to an extension: reading the manifest, resolving dependencies, loading, exporting, starting, updating and stopping. A language module reports a failure by returning an error from the matching ILanguageModule method; an exception thrown from these methods is caught and treated the same way. The extension then gets the state Failed (or Corrupted, Unresolved, Disabled) with the reason, the extensions that depend on it are Skipped, and the rest keep working. After loading the core logs a report with the reason for every extension that did not start.
Calls between plugins
When a plugin calls a method of another plugin, the call goes straight to the native function of that method. The core is not in between, which is what keeps these calls fast, but it also means the core does not check them. What happens on an error depends on the language of the method that was called:
- Native code (C++, Go, Rust): a crash in the method is a crash of the process. A C++ exception must not leave an exported function.
- Languages with a runtime (C#, Python, JavaScript, Lua): the language module catches the error of the plugin code, logs it and returns a default value (zero, an empty string, and so on) to the caller. The caller does not learn that the call failed.
Recommendations for exported methods
Validate the arguments
Check ranges, sizes and handles at the start of every exported method. Another plugin may pass anything the signature allows.
Report failure in the return value
Because an error does not travel to the caller, make it part of the signature: return bool or an error code, and pass results through ref parameters.
Do not let errors escape
In C++, catch exceptions inside the exported function. In languages with exceptions, catch what you can handle and return a result the caller can check.
Document error conditions
Write in the description of the method and its parameters what it returns when it fails. Binding generators copy these descriptions into the code other plugins see.