-
As the remix docs say:
Like below: export async function loader() {
throw json('error')
} But whart if i return a export async function loader() {
const promise = new Promise().then(p=>{throw json('error')})
return defer({promise})
} it will cause server error and print |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I can only move them to clientLoader() now. |
Beta Was this translation helpful? Give feedback.
-
This is not an issue with Remix but with how HTTP works. When you send an HTTP response, you send something like this:
Notice that the response starts with the version and the status code, then the headers and finally after a blank line, the body. The body of a request can be streamed, which means the server can send the first part of the response and then send the body as it's being generated, this is what Remix is doing when you use defer to send a promise, it's sending something like this:
The problem comes when you do What you need to do instead is to return data, and handle the error client side using the export async function loader() {
let promise = doSomethingAsync().then(() => {
throw new Error("error");
});
return defer({ promise });
}
export default function Component() {
let { promise } = useLoaderData<typeof loader>();
return (
<Suspense fallback={<LoadingFallback />}>
<Await resolve={promise} errorElement={<ErrorFallback />}>
{(data) => <DataComponent data={data} />}
</Await>
</Suspense>
);
} And inside ErrorFallback you can use the useAsyncError hook to get the error. |
Beta Was this translation helpful? Give feedback.
-
I tried your solution above and it doesn't seem to work. I am actively trying to test my error scenarios and for some reason it just doesn't render the errorElement prop of . Whenever I throw an error, it resolves to an unhandled rejection error instead. I also added this bit of code in my entry.server.tsx file as referenced in this thread #9178 to avoid my server to crash but still I can't seem to force the UI to revert to when an error is encountered. @sergiodxa , it would be super helpful and awesome if you could dive a bit deeper on how to solve this. I don't know whether this is a bug with defer or lack of skill on my part. |
Beta Was this translation helpful? Give feedback.
This is not an issue with Remix but with how HTTP works. When you send an HTTP response, you send something like this:
Notice that the response starts with the version and the status code, then the headers and finally after a blank line, the body.
The body of a request can be streamed, which means the server can send the first part of the response and then send the body as it's being generated, this is what Remix is doing when you use defer to send a promise, it's sending something like this:
The problem come…