> For the complete documentation index, see [llms.txt](https://docs.sacruda.nl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sacruda.nl/developers/your-apps-code/error-handling.md).

# Error handling

Oh no! An exception occurred. What can you do about it? Well, it depends. Sacruda has error handling measures built-in to prevent it from crashing when *you* make a mistake. However, there is only so much I can do with the browser's JS engine being the way that it is. This page covers some best practices when it comes to error handling.

{% hint style="danger" %}
Starting in Sacruda **7.0.4**, the global error handling system for Sacruda has changed. Now, when a file of your app throws an unhandled error (like from a callback), Sacruda will detect that and intercept the system-wide crash by instead crashing the process immediately responsible, making it really hard to unintentionally crash Sacruda using your app's code.\
\
This system isn't perfect, because of many reasons, but the likelyhood of Sacruda crashing because of a bug in your application went from 80% to about 25% with this change.
{% endhint %}

### Callbacks

If you have a callback, like an HTML event or a user preferences update, please make sure to wrap the code *inside* that callback in a try/catch if it can possibly error. If you don't, Sacruda's global error handling will take over and declare it as a system-wide crash. That's not what you want, is it? Say we have this event listener:

```javascript
saveButton.addEventListener("click", () => {
    this.saveFile();
})
```

That `saveFile` call runs outside the initial scope of the App Renderer's error handling, so an error in this callback will crash Sacruda. This even happens if you wrap the entire `addEventListener` call:

<pre class="language-javascript"><code class="lang-javascript"><strong>try {
</strong>  saveButton.addEventListener("click", () => {
    this.saveFile();
  });
<strong>} catch (e) {
</strong><strong>  Debug(`Error: ${e}`);
</strong><strong>}
</strong></code></pre>

because of the same problem. Instead, you can either wrap the `saveFile` call inside of the callback, or you can make sure the `saveFile` method correctly handles errors. Say we have the `saveFile` method as such:

```javascript
async saveFile() {
  const path = this.path();
  const contents = this.contents();
  
  const prog = await this.userDaemon.FileProgress(
    {
      type: "size",
      caption: `Saving file...`,
      subtitle: path,
      icon: DriveIcon,
    },
    this.pid
  );

  await this.fs.writeFile(path, textToBlob(contents), async (progress) => {
    await prog.show();
    prog.setMax(progress.max);
    prog.setDone(progress.value);
  });

  await prog.stop();
}
```

Let's wrap the error-prone parts in a try/catch to prevent them from crashing the OS:

<pre class="language-javascript"><code class="lang-javascript">async saveFile() {
  const path = this.path();
  const contents = this.contents();
  
<strong>  let prog;
</strong><strong>  
</strong><strong>  try {
</strong>    prog = await this.userDaemon.FileProgress(
      {
        type: "size",
        caption: `Saving file...`,
        subtitle: path,
        icon: DriveIcon,
      },
      this.pid
    );

    await this.fs.writeFile(path, textToBlob(contents), async (progress) => {
      await prog.show();
      prog.setMax(progress.max);
      prog.setDone(progress.value);
    });
<strong>  } catch (e) {
</strong><strong>    this.saveFailed(e); // Gracefully handle the error
</strong><strong>  }
</strong>
  await prog?.stop();
}
</code></pre>

Ofcourse you would ideally display a message box or notification or something to the user to inform them that the operation failed, but hopefully you get the idea.
