这是indexloc提供的服务,不要输入任何密码
Skip to content

Use SharedError for body streaming #4392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 31, 2023
Merged

Use SharedError for body streaming #4392

merged 5 commits into from
Mar 31, 2023

Conversation

jridgewell
Copy link
Contributor

Follow up to #4329, this removes BodyError (which was just a simple String and not an Error) and switches to SharedError.

I've implemented a basic PartialEq and Serialization for SharedError so that it doesn't infect everything that uses Body.

@jridgewell jridgewell requested a review from a team as a code owner March 29, 2023 22:45
@vercel
Copy link

vercel bot commented Mar 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

11 Ignored Deployments
Name Status Preview Comments Updated
examples-basic-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-cra-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-designsystem-docs ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-gatsby-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-kitchensink-blog ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-native-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-nonmonorepo ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-svelte-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-tailwind-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
examples-vite-web ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)
turbo-site ⬜️ Ignored (Inspect) Mar 29, 2023 at 10:52PM (UTC)

@github-actions
Copy link
Contributor

✅ This changes can build next-swc

@github-actions
Copy link
Contributor

github-actions bot commented Mar 29, 2023

🟢 CI successful 🟢

Thanks

Comment on lines +60 to +85
impl Serialize for SharedError {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut v = vec![self.to_string()];
let mut source = self.source();
while let Some(s) = source {
v.push(s.to_string());
source = s.source();
}
Serialize::serialize(&v, serializer)
}
}

impl<'de> Deserialize<'de> for SharedError {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error;
let mut messages = <Vec<String>>::deserialize(deserializer)?;
let mut e = match messages.pop() {
Some(e) => anyhow!(e),
None => return Err(Error::custom("expected at least 1 error message")),
};
while let Some(message) = messages.pop() {
e = e.context(message);
}
Ok(SharedError::new(e))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a good idea. It's not perfectly restoring error types. Maybe we should return an Serialization error when there is an non-anyhow error in the chain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter? The first value is guaranteed to be an anyhow::Error, and the sources are only guaranteed to be dyn std::error::Error implementors. I'm not sure how to even observe that the real struct changes between serialization and deserialization.

@jridgewell jridgewell merged commit 1414a69 into main Mar 31, 2023
@jridgewell jridgewell deleted the jrl-body-sharederror branch March 31, 2023 23:10
kodiakhq bot pushed a commit to vercel/next.js that referenced this pull request Apr 1, 2023
Pending vercel/turborepo#4392 landing in Turbopack (and #47476 landing here), this removes `BodyError` and switches to `SharedError`. That should allow us to preserve the source chain of errors for when we finally display it to the dev, aiding debugging.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
Follow up to vercel/turborepo#4329, this removes
`BodyError` (which was just a simple `String` and not an `Error`) and
switches to `SharedError`.

I've implemented a basic `PartialEq` and `Serialization` for
`SharedError` so that it doesn't infect everything that uses `Body`.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
Follow up to vercel/turborepo#4329, this removes
`BodyError` (which was just a simple `String` and not an `Error`) and
switches to `SharedError`.

I've implemented a basic `PartialEq` and `Serialization` for
`SharedError` so that it doesn't infect everything that uses `Body`.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
Follow up to vercel/turborepo#4329, this removes
`BodyError` (which was just a simple `String` and not an `Error`) and
switches to `SharedError`.

I've implemented a basic `PartialEq` and `Serialization` for
`SharedError` so that it doesn't infect everything that uses `Body`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants