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();
// 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 =

View File

@ -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 =

View File

@ -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");