feat: add error log for grpc handler (#394)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2024-04-17 15:25:50 +08:00 committed by GitHub
parent c60dd7aa5f
commit 84a0e28c35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 26 deletions

View File

@ -164,9 +164,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
let request = request.into_inner(); let request = request.into_inner();
// Check whether the download is empty. // Check whether the download is empty.
let mut download = request let mut download = request.download.ok_or_else(|| {
.download error!("missing download");
.ok_or(Status::invalid_argument("missing download"))?; Status::invalid_argument("missing download")
})?;
// Generate the task id. // Generate the task id.
let task_id = self let task_id = self
@ -233,9 +234,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
// If download protocol is http, use the range of the request header. // If download protocol is http, use the range of the request header.
// If download protocol is not http, use the range of the download. // If download protocol is not http, use the range of the download.
if download.range.is_none() { if download.range.is_none() {
let content_length = task let content_length = task.content_length().ok_or_else(|| {
.content_length() error!("missing content length in the response");
.ok_or(Status::internal("missing content length in the response"))?; Status::internal("missing content length in the response")
})?;
// Convert the header. // Convert the header.
let request_header = let request_header =

View File

@ -337,9 +337,10 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
let request = request.into_inner(); let request = request.into_inner();
// Check whether the download is empty. // Check whether the download is empty.
let mut download = request let mut download = request.download.ok_or_else(|| {
.download error!("missing download");
.ok_or(Status::invalid_argument("missing download"))?; Status::invalid_argument("missing download")
})?;
// Generate the task id. // Generate the task id.
let task_id = self let task_id = self
@ -406,9 +407,10 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
// If download protocol is http, use the range of the request header. // If download protocol is http, use the range of the request header.
// If download protocol is not http, use the range of the download. // If download protocol is not http, use the range of the download.
if download.range.is_none() { if download.range.is_none() {
let content_length = task let content_length = task.content_length().ok_or_else(|| {
.content_length() error!("missing content length in the response");
.ok_or(Status::internal("missing content length in the response"))?; Status::internal("missing content length in the response")
})?;
// Convert the header. // Convert the header.
let request_header = let request_header =

View File

@ -112,7 +112,10 @@ impl Piece {
loop { loop {
// If offset is greater than content_length, break the loop. // If offset is greater than content_length, break the loop.
if offset >= content_length { if offset >= content_length {
let mut piece = pieces.pop().ok_or(Error::InvalidParameter)?; let mut piece = pieces.pop().ok_or_else(|| {
error!("piece not found");
Error::InvalidParameter
})?;
piece.length = piece_length + content_length - offset; piece.length = piece_length + content_length - offset;
pieces.push(piece); pieces.push(piece);
break; break;
@ -156,7 +159,10 @@ impl Piece {
loop { loop {
// If offset is greater than content_length, break the loop. // If offset is greater than content_length, break the loop.
if offset >= content_length { if offset >= content_length {
let mut piece = pieces.pop().ok_or(Error::InvalidParameter)?; let mut piece = pieces.pop().ok_or_else(|| {
error!("piece not found");
Error::InvalidParameter
})?;
piece.length = piece_length + content_length - offset; piece.length = piece_length + content_length - offset;
pieces.push(piece); pieces.push(piece);
break; break;
@ -254,10 +260,10 @@ impl Piece {
self.storage.download_piece_started(task_id, number).await?; self.storage.download_piece_started(task_id, number).await?;
// Create a dfdaemon client. // Create a dfdaemon client.
let host = parent let host = parent.host.clone().ok_or_else(|| {
.host error!("peer host is empty");
.clone() Error::InvalidPeer(parent.id.clone())
.ok_or(Error::InvalidPeer(parent.id.clone()))?; })?;
let dfdaemon_upload_client = let dfdaemon_upload_client =
DfdaemonUploadClient::new(format!("http://{}:{}", host.ip, host.port)) DfdaemonUploadClient::new(format!("http://{}:{}", host.ip, host.port))
.await .await
@ -325,9 +331,10 @@ impl Piece {
err err
})?; })?;
self.storage self.storage.get_piece(task_id, number)?.ok_or_else(|| {
.get_piece(task_id, number)? error!("piece not found");
.ok_or(Error::PieceNotFound(number.to_string())) Error::PieceNotFound(number.to_string())
})
} }
// download_from_source downloads a single piece from the source. // download_from_source downloads a single piece from the source.
@ -378,7 +385,10 @@ impl Piece {
// HTTP status code is not OK, handle the error. // HTTP status code is not OK, handle the error.
if let Some(http_status_code) = response.http_status_code { if let Some(http_status_code) = response.http_status_code {
let http_header = response.http_header.ok_or(Error::InvalidParameter)?; let http_header = response.http_header.ok_or_else(|| {
error!("http header is empty");
Error::InvalidParameter
})?;
if !http_status_code.is_success() { if !http_status_code.is_success() {
// Record the failure of downloading piece, // Record the failure of downloading piece,
// if the status code is not OK. // if the status code is not OK.
@ -413,10 +423,10 @@ impl Piece {
err err
})?; })?;
return self return self.storage.get_piece(task_id, number)?.ok_or_else(|| {
.storage error!("piece not found");
.get_piece(task_id, number)? Error::PieceNotFound(number.to_string())
.ok_or(Error::PieceNotFound(number.to_string())); });
} }
error!("backend returns invalid response"); error!("backend returns invalid response");