Handling uncaught controller errors with try/catch and the Error controller
Key Takeaway
If no code catches a controller error, the Error.js controller handles it, and the shopper gets a 410 status by default. A conditional catch such as catch (e if e instanceof IOError) catches only that type, so other exceptions can still end up at Error.js.
Controllers can throw errors like any B2C Commerce script. When no code catches the error, the Error.js controller handles it. By default, the shopper then gets a 410 response. To deal with an error yourself, wrap the risky code in a JavaScript try/catch statement. Your code then resolves the error, so shoppers don't see cryptic technical information. To troubleshoot well, you need to know which exceptions a catch block actually catches and where errors are logged.
Uncaught errors and the Error.js controller
Controllers are server-side scripts that handle storefront requests. They are CommonJS modules stored in the controllers folder at the top level of a cartridge. Like any B2C Commerce script, a controller can throw errors.
The Error.js controller services uncaught errors. By default, B2C Commerce returns a 410 status for them, which means the resource is no longer available.
You can return a 404 status for uncaught errors instead of the default 410. A 404 is fine only if the related ISML template doesn't include <isslot>, <iscomponent> or <isinclude> tags.