Error Handling
Oftentimes, pesky users attempt to access resources they’re not supposed to access. Or maybe they’re just users being users and not paying attention to where they’re clicking. In any case, we’ve all met at least one user like this. As developers, it is in our best interest to ensure our code gives those users meaningful messages when they inevitably encounter an error. For the sake of our sanity and future selves, we should discuss errors in SvelteKit.
All of the errors we just created with handleError()
are considered unexpected errors. That is because we did not use SvelteKit’s error module. Errors created using this module are considered expected errors. By importing the module, we can send custom status codes and error messages to SvelteKit, which can then be captured and used by +error.svelte
components. This gives us even greater control over how our messaging displays to users.
In Chapter 4, we briefly examined how the...