diff --git a/dragonfly-client/src/grpc/dfdaemon_download.rs b/dragonfly-client/src/grpc/dfdaemon_download.rs index 74d2e5ae..5c2dda98 100644 --- a/dragonfly-client/src/grpc/dfdaemon_download.rs +++ b/dragonfly-client/src/grpc/dfdaemon_download.rs @@ -164,9 +164,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler { let request = request.into_inner(); // Check whether the download is empty. - let mut download = request - .download - .ok_or(Status::invalid_argument("missing download"))?; + let mut download = request.download.ok_or_else(|| { + error!("missing download"); + Status::invalid_argument("missing download") + })?; // Generate the task id. 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 not http, use the range of the download. if download.range.is_none() { - let content_length = task - .content_length() - .ok_or(Status::internal("missing content length in the response"))?; + let content_length = task.content_length().ok_or_else(|| { + error!("missing content length in the response"); + Status::internal("missing content length in the response") + })?; // Convert the header. let request_header = diff --git a/dragonfly-client/src/grpc/dfdaemon_upload.rs b/dragonfly-client/src/grpc/dfdaemon_upload.rs index 69b112bb..0c002be7 100644 --- a/dragonfly-client/src/grpc/dfdaemon_upload.rs +++ b/dragonfly-client/src/grpc/dfdaemon_upload.rs @@ -337,9 +337,10 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler { let request = request.into_inner(); // Check whether the download is empty. - let mut download = request - .download - .ok_or(Status::invalid_argument("missing download"))?; + let mut download = request.download.ok_or_else(|| { + error!("missing download"); + Status::invalid_argument("missing download") + })?; // Generate the task id. 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 not http, use the range of the download. if download.range.is_none() { - let content_length = task - .content_length() - .ok_or(Status::internal("missing content length in the response"))?; + let content_length = task.content_length().ok_or_else(|| { + error!("missing content length in the response"); + Status::internal("missing content length in the response") + })?; // Convert the header. let request_header = diff --git a/dragonfly-client/src/task/piece.rs b/dragonfly-client/src/task/piece.rs index c0b7fee1..c6a50f70 100644 --- a/dragonfly-client/src/task/piece.rs +++ b/dragonfly-client/src/task/piece.rs @@ -112,7 +112,10 @@ impl Piece { loop { // If offset is greater than content_length, break the loop. 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; pieces.push(piece); break; @@ -156,7 +159,10 @@ impl Piece { loop { // If offset is greater than content_length, break the loop. 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; pieces.push(piece); break; @@ -254,10 +260,10 @@ impl Piece { self.storage.download_piece_started(task_id, number).await?; // Create a dfdaemon client. - let host = parent - .host - .clone() - .ok_or(Error::InvalidPeer(parent.id.clone()))?; + let host = parent.host.clone().ok_or_else(|| { + error!("peer host is empty"); + Error::InvalidPeer(parent.id.clone()) + })?; let dfdaemon_upload_client = DfdaemonUploadClient::new(format!("http://{}:{}", host.ip, host.port)) .await @@ -325,9 +331,10 @@ impl Piece { err })?; - self.storage - .get_piece(task_id, number)? - .ok_or(Error::PieceNotFound(number.to_string())) + self.storage.get_piece(task_id, number)?.ok_or_else(|| { + error!("piece not found"); + Error::PieceNotFound(number.to_string()) + }) } // 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. 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() { // Record the failure of downloading piece, // if the status code is not OK. @@ -413,10 +423,10 @@ impl Piece { err })?; - return self - .storage - .get_piece(task_id, number)? - .ok_or(Error::PieceNotFound(number.to_string())); + return self.storage.get_piece(task_id, number)?.ok_or_else(|| { + error!("piece not found"); + Error::PieceNotFound(number.to_string()) + }); } error!("backend returns invalid response");