Skip to content

Silencing errors

Falcion edited this page Oct 18, 2024 · 3 revisions

Error silencing is an error-handling strategy in UNITADE and a common feature in the plugin's settings, allowing you to "silence" all errors and redirect their notifications to the developer console. This fits into the broader philosophy called "shut up and just work" (SAJW). This feature ensures that errors encountered during the plugin's operation do not disrupt the user experience. Instead of displaying error messages to the user or halting execution, it silently handles these errors by logging them to the Obsidian Developer Console for debugging purposes.

This approach is particularly useful in scenarios where the errors originate from Obsidian's private or undocumented APIs—parts of the code that UNITADE depends on but are inaccessible or unstable. Silencing errors helps keep the plugin running smoothly, even when unexpected issues arise.

Practice

Below is a popular example where error silencing may be necessary, as discussed in issue #70. When typing data in settings, due to Obsidian's "degenerate" implementation of its so-called "API," any text input can work normally within the onChange() event, not the onChanged() event. As a result, the plugin tries to process the input in real time.

private __tryApply(filetype: string, view: string): void {
    if (!this.settings.markdown_overcharge && ['md', 'mdown', 'markdown'].includes(filetype))
        return;

    /**@ts-expect-error: not part of public API, accessing through runtime. */
    if (this.app.viewRegistry.isExtensionRegistered(filetype))
        return;

    try {
        this.registerExtensions([filetype], view);
    } catch (err: any) {
        /**@ts-expect-error: not part of public API, accessing through runtime. */
        const curr: string = this.app.viewRegistry.getTypeByExtension(filetype);

        let _msg: string;

        if (curr) {
            _msg = formatString(this.locale.getLocaleItem('ERROR_REGISTRY_EXTENSION')[0]!, filetype, view);
        } else {
            _msg = formatString(this.locale.getLocaleItem('ERROR_REGISTRY_EXTENSION')[1]!, filetype, view, err);
        }

        if (!this.settings.silence_errors) {
            new Notification(this.locale.getLocaleItem('ERROR_COMMON_MESSAGE')[0]!, { body: _msg });

            console.error(_msg);
        } else {
            console.debug(`[UNITADE-ERROR]: ERROR IS SILENCED, ERROR: ${_msg}`);
        }

        this._settings.errors[filetype] = _msg;
    }
}

In this snippet, when a file extension fails to register, the error is caught and logged in the console if "error eilencing" is active. If error silencing is disabled, a notification is shown to the user (via the OS), and the error is logged using console.error(), which also notifies the user through Obsidian's UI.

Note

Error silencing only applies to errors related to UNITADE and its codebase: it does not silence any external errors or critical system errors.

Clone this wiki locally