queryClient.ensureQuery and throwing to ErrorBoundary
Is there a way to make queries throw other things than Errors?
I'm using queryClient.ensureQuery with React Router's
loader like this:
I expected to catch this in my React Router ErrorComponent, but it's not an Error object so it's ignored and ensureQuery returns.7 Replies
variable-lime•3y ago
throw - JavaScript | MDN
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.
correct-apricot•3y ago
not sure, maybe its a router thing that it doesn't catch things that aren't errors?
I would say its discouraged to throw things that are non-errors though. You could create your own error subclass and attach the response to it
in v5, we'll also default the error type to
Error because everybody only throws errors and having unknown is just a huge burden for most usersextended-salmonOP•3y ago
Throwing
Responses from the loader and action functions and catching them in the route ErrorComponent/errorElement is one of the things Michael and Ryan have brought to React Router from Remix in version 6. Throwing a new Response in the loader or a new Error in the query renders my ErrorComponent.extended-salmonOP•3y ago
This is rather unhelpful. What is your point? I know the difference between the return and exception flows. Exceptions are not synonymous with errors. Though it is unconventional to throw other things than errors, it's not the sole intent of the exception program flow.
variable-lime•3y ago
Like Dominik said: throw an error… written by MDN
extended-salmonOP•3y ago
Their reasoning doesn't apply in this case, and it's not an answer to my question.
variable-lime•3y ago
Most times people ask the wrong question….