Skip to content

Commit

Permalink
Add error codes to Response::Error variant
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb committed Jun 14, 2022
1 parent 4a89bfc commit d45abd2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
30 changes: 15 additions & 15 deletions async-nats/src/jetstream/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl Context {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while publishing message: {}, {}",
error.code, error.description
"nats: error while publishing message: {}, {}, {}",
error.code, error.status, error.description
),
))),

Expand All @@ -87,8 +87,8 @@ impl Context {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while publishing message: {}, {}",
error.code, error.description
"nats: error while publishing message: {}, {}, {}",
error.code, error.status, error.description
),
))),

Expand Down Expand Up @@ -131,8 +131,8 @@ impl Context {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while creating stream: {}, {}",
error.code, error.description
"nats: error while creating stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok(info) => Ok(Stream {
Expand All @@ -157,8 +157,8 @@ impl Context {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while getting stream: {}, {}",
error.code, error.description
"nats: error while getting stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok(info) => Ok(Stream {
Expand All @@ -177,12 +177,12 @@ impl Context {

let request: Response<StreamInfo> = self.request(subject, &()).await?;
match request {
Response::Err { error } if error.code == 404 => self.create_stream(&config).await,
Response::Err { error } if error.status == 404 => self.create_stream(&config).await,
Response::Err { error } => Err(Box::new(io::Error::new(
ErrorKind::Other,
format!(
"nats: error while getting or creating stream: {}, {}",
error.code, error.description
"nats: error while getting or creating stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok(info) => Ok(Stream {
Expand All @@ -205,8 +205,8 @@ impl Context {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while deleting stream: {}, {}",
error.code, error.description
"nats: error while deleting stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok(delete_response) => Ok(delete_response),
Expand All @@ -219,8 +219,8 @@ impl Context {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while updating stream: {}, {}",
error.code, error.description
"nats: error while updating stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok(info) => Ok(info),
Expand Down
7 changes: 6 additions & 1 deletion async-nats/src/jetstream/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ use serde::Deserialize;
/// An error description returned in a response to a jetstream request.
#[derive(Debug, Deserialize)]
pub struct Error {
/// Code
/// Error code
#[serde(rename = "err_code")]
pub code: u64,

/// Status code
#[serde(rename = "code")]
pub status: u16,

/// Description
pub description: String,
}
Expand Down
18 changes: 9 additions & 9 deletions async-nats/src/jetstream/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl Stream {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while creating stream: {}, {}",
error.code, error.description
"nats: error while creating stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok(info) => Ok(info),
Expand All @@ -72,8 +72,8 @@ impl Stream {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while getting consumer info: {}, {}",
error.code, error.description
"nats: error while getting consumer info: {}, {}, {}",
error.code, error.status, error.description
),
))),
}
Expand All @@ -100,7 +100,7 @@ impl Stream {
let subject = format!("CONSUMER.INFO.{}.{}", self.info.config.name, name);

match self.context.request(subject, &json!({})).await? {
Response::Err { error } if error.code == 404 => self
Response::Err { error } if error.status == 404 => self
.create_consumer(config.into_consumer_config())
.await
.map(|info| {
Expand All @@ -113,8 +113,8 @@ impl Stream {
Response::Err { error } => Err(Box::new(io::Error::new(
ErrorKind::Other,
format!(
"nats: error while getting or creating stream: {}, {}",
error.code, error.description
"nats: error while getting or creating stream: {}, {}, {}",
error.code, error.status, error.description
),
))),
Response::Ok::<Info>(info) => Ok(Consumer::new(
Expand All @@ -133,8 +133,8 @@ impl Stream {
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while deleting consumer: {}, {}",
error.code, error.description
"nats: error while deleting consumer: {}, {}, {}",
error.code, error.status, error.description
),
))),
}
Expand Down

0 comments on commit d45abd2

Please sign in to comment.