Skip to content

Commit

Permalink
streaming_data_service: improve error message
Browse files Browse the repository at this point in the history
When the channel closes which the sender uses to transport the file
data. Ask the `StreamingDataService` what the current error state is
rather then just responding to the client with a generic answer.
  • Loading branch information
svenrademakers committed Nov 1, 2023
1 parent 7b7c9e5 commit 4681af1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/api/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,15 @@ async fn handle_file_upload(

while let Some(Ok(chunk)) = field.next().await {
if sender.send(chunk).await.is_err() {
return Err((StatusCode::GONE, "upload cancelled").into());
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
ss.status()
.await
.error_message()
.unwrap_or("transfer cancelled")
.to_string(),
)
.into());
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/api/streaming_data_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ pub enum StreamingState {
Error(String),
}

impl StreamingState {
/// returns the error message when self == `StreamingState::Error(msg)`.
/// Otherwise returns `None`.
pub fn error_message(&self) -> Option<&str> {
if let StreamingState::Error(msg) = self {
return Some(msg);
}
None
}
}

impl Display for StreamingState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down

0 comments on commit 4681af1

Please sign in to comment.