Skip to content

Commit f2e47f5

Browse files
committed
Return protobuf serialized ErrorResponse from APIs.
1 parent 5e8fb35 commit f2e47f5

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

ldk-server/src/service.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::api::bolt12_receive::{handle_bolt12_receive_request, BOLT12_RECEIVE_P
1313
use crate::api::bolt12_send::{handle_bolt12_send_request, BOLT12_SEND_PATH};
1414
use crate::api::close_channel::{handle_close_channel_request, CLOSE_CHANNEL_PATH};
1515
use crate::api::error::LdkServerError;
16+
use crate::api::error::LdkServerErrorCode::InvalidRequestError;
1617
use crate::api::get_balances::{handle_get_balances_request, GET_BALANCES};
1718
use crate::api::get_node_info::{handle_get_node_info_request, GET_NODE_INFO};
1819
use crate::api::get_payment_details::{
@@ -30,6 +31,7 @@ use crate::api::update_channel_config::{
3031
handle_update_channel_config_request, UPDATE_CHANNEL_CONFIG_PATH,
3132
};
3233
use crate::io::paginated_kv_store::PaginatedKVStore;
34+
use crate::util::proto_adapter::to_error_response;
3335
use std::future::Future;
3436
use std::pin::Pin;
3537
use std::sync::Arc;
@@ -129,16 +131,23 @@ async fn handle_request<
129131
.body(Full::new(Bytes::from(response.encode_to_vec())))
130132
// unwrap safety: body only errors when previous chained calls failed.
131133
.unwrap()),
132-
Err(e) => Ok(Response::builder()
133-
.status(StatusCode::INTERNAL_SERVER_ERROR)
134-
.body(Full::new(Bytes::from(e.to_string().into_bytes())))
134+
Err(e) => {
135+
let (error_response, status_code) = to_error_response(e);
136+
Ok(Response::builder()
137+
.status(status_code)
138+
.body(Full::new(Bytes::from(error_response.encode_to_vec())))
139+
// unwrap safety: body only errors when previous chained calls failed.
140+
.unwrap())
141+
},
142+
},
143+
Err(_) => {
144+
let (error_response, status_code) =
145+
to_error_response(LdkServerError::new(InvalidRequestError, "Malformed request."));
146+
Ok(Response::builder()
147+
.status(status_code)
148+
.body(Full::new(Bytes::from(error_response.encode_to_vec())))
135149
// unwrap safety: body only errors when previous chained calls failed.
136-
.unwrap()),
150+
.unwrap())
137151
},
138-
Err(_) => Ok(Response::builder()
139-
.status(StatusCode::BAD_REQUEST)
140-
.body(Full::new(Bytes::from(b"Error parsing request".to_vec())))
141-
// unwrap safety: body only errors when previous chained calls failed.
142-
.unwrap()),
143152
}
144153
}

ldk-server/src/util/proto_adapter.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::api::error::LdkServerError;
2-
use crate::api::error::LdkServerErrorCode::InvalidRequestError;
2+
use crate::api::error::LdkServerErrorCode::{
3+
AuthError, InternalServerError, InvalidRequestError, LightningError,
4+
};
35
use bytes::Bytes;
46
use hex::prelude::*;
7+
use hyper::StatusCode;
58
use ldk_node::bitcoin::hashes::sha256;
69
use ldk_node::bitcoin::secp256k1::PublicKey;
710
use ldk_node::config::{ChannelConfig, MaxDustHTLCExposure};
@@ -11,6 +14,7 @@ use ldk_node::payment::{
1114
ConfirmationStatus, PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus,
1215
};
1316
use ldk_node::{ChannelDetails, LightningBalance, PendingSweepBalance, UserChannelId};
17+
use ldk_server_protos::error::{ErrorCode, ErrorResponse};
1418
use ldk_server_protos::types::confirmation_status::Status::{Confirmed, Unconfirmed};
1519
use ldk_server_protos::types::lightning_balance::BalanceType::{
1620
ClaimableAwaitingConfirmations, ClaimableOnChannelClose, ContentiousClaimable,
@@ -409,3 +413,23 @@ pub(crate) fn proto_to_bolt11_description(
409413
},
410414
})
411415
}
416+
417+
pub(crate) fn to_error_response(ldk_error: LdkServerError) -> (ErrorResponse, StatusCode) {
418+
let error_code = match ldk_error.error_code {
419+
InvalidRequestError => ErrorCode::InvalidRequestError,
420+
AuthError => ErrorCode::AuthError,
421+
LightningError => ErrorCode::LightningError,
422+
InternalServerError => ErrorCode::InternalServerError,
423+
} as i32;
424+
425+
let status = match ldk_error.error_code {
426+
InvalidRequestError => StatusCode::BAD_REQUEST,
427+
AuthError => StatusCode::UNAUTHORIZED,
428+
LightningError => StatusCode::INTERNAL_SERVER_ERROR,
429+
InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
430+
};
431+
432+
let error_response = ErrorResponse { message: ldk_error.message, error_code };
433+
434+
(error_response, status)
435+
}

0 commit comments

Comments
 (0)