From d0c88f69381ed6d8c4325234b8ef63a07c8069e2 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 16 Feb 2023 22:45:35 +0100 Subject: [PATCH] Change `sse::Event::json_data` to use `axum_core::Error` as its error type I don't think it was intentional to use `serde_json::Error` and we don't generally use that elsewhere we do JSON things. Discovered this with --- axum/CHANGELOG.md | 4 +++- axum/Cargo.toml | 1 - axum/src/response/sse.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 9a6b8eb9ef..d50d1e620d 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **breaking:** Change `sse::Event::json_data` to use `axum_core::Error` as its error type ([#1762]) + +[#1762]: https://github.com/tokio-rs/axum/pull/1762 # 0.6.6 (12. February, 2023) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index d69448fad4..2c79b08684 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -122,7 +122,6 @@ allowed = [ "http_body", "hyper", "serde", - "serde_json", "tower_layer", "tower_service", ] diff --git a/axum/src/response/sse.rs b/axum/src/response/sse.rs index e66411aba6..b246f6d514 100644 --- a/axum/src/response/sse.rs +++ b/axum/src/response/sse.rs @@ -210,7 +210,7 @@ impl Event { /// /// [`MessageEvent`'s data field]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data #[cfg(feature = "json")] - pub fn json_data(mut self, data: T) -> serde_json::Result + pub fn json_data(mut self, data: T) -> Result where T: serde::Serialize, { @@ -219,7 +219,7 @@ impl Event { } self.buffer.extend_from_slice(b"data:"); - serde_json::to_writer((&mut self.buffer).writer(), &data)?; + serde_json::to_writer((&mut self.buffer).writer(), &data).map_err(axum_core::Error::new)?; self.buffer.put_u8(b'\n'); self.flags.insert(EventFlags::HAS_DATA);