I am wanting to throw an error within the onMount function when the page loads that will then take the page to the __error.svelte page for the error to be handled.
I am using the custom error class
class CustomError extends Error {
constructor(message) {
super(message); // (1)
this.status = 401; // (2)
}
}
The code I have so far is, which throws the error to the console only and does not redirect the page to __error.svelte.
onMount(()=> {
throw new CustomError("Test");
})
The code that does what I am after is. This works when run outside of any functions and redirects the page to __error.svelte with the message:
throw new CustomError("Test");
Is there a way to structure this so that it will throw the error within the page rather than the function? I have tried a try/catch statement which doesn't work.
Or is there an alternative method to target the __error.svelte file with the custom error message I am wanting?