Skip to content

Commit 30ac298

Browse files
committed
error: Add SdkApiError and SdkErrorKind
1 parent 9743149 commit 30ac298

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

lexe-api-core/src/error.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ api_error!(LspApiError, LspErrorKind);
511511
api_error!(MegaApiError, MegaErrorKind);
512512
api_error!(NodeApiError, NodeErrorKind);
513513
api_error!(RunnerApiError, RunnerErrorKind);
514+
api_error!(SdkApiError, SdkErrorKind);
514515

515516
// --- Error variants --- //
516517

@@ -967,6 +968,67 @@ impl ToHttpStatus for RunnerErrorKind {
967968
}
968969
}
969970

971+
api_error_kind! {
972+
/// All variants of errors that the SDK can return.
973+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
974+
pub enum SdkErrorKind {
975+
/// Unknown error
976+
Unknown(ErrorCode),
977+
978+
// --- Common --- //
979+
980+
/// Unknown Reqwest client error
981+
UnknownReqwest = 1,
982+
/// Error building the HTTP request
983+
Building = 2,
984+
/// Error connecting to a remote HTTP service
985+
Connect = 3,
986+
/// Request timed out
987+
Timeout = 4,
988+
/// Error decoding/deserializing the HTTP response body
989+
Decode = 5,
990+
/// General server error
991+
Server = 6,
992+
/// Client provided a bad request that the server rejected
993+
Rejection = 7,
994+
/// Server is at capacity
995+
AtCapacity = 8,
996+
997+
// --- SDK --- //
998+
999+
/// Error
1000+
// NOTE: Intentionally NOT descriptive.
1001+
// These get displayed to users frequently and should be concise.
1002+
Command = 100,
1003+
/// Authentication error
1004+
BadAuth = 101,
1005+
/// Resource not found
1006+
NotFound = 102,
1007+
}
1008+
}
1009+
1010+
impl ToHttpStatus for SdkErrorKind {
1011+
fn to_http_status(&self) -> StatusCode {
1012+
use SdkErrorKind::*;
1013+
match self {
1014+
Unknown(_) => SERVER_500_INTERNAL_SERVER_ERROR,
1015+
1016+
UnknownReqwest => CLIENT_400_BAD_REQUEST,
1017+
Building => CLIENT_400_BAD_REQUEST,
1018+
Connect => SERVER_503_SERVICE_UNAVAILABLE,
1019+
Timeout => SERVER_504_GATEWAY_TIMEOUT,
1020+
Decode => SERVER_502_BAD_GATEWAY,
1021+
Server => SERVER_500_INTERNAL_SERVER_ERROR,
1022+
Rejection => CLIENT_400_BAD_REQUEST,
1023+
AtCapacity => SERVER_503_SERVICE_UNAVAILABLE,
1024+
1025+
Command => SERVER_500_INTERNAL_SERVER_ERROR,
1026+
BadAuth => CLIENT_401_UNAUTHORIZED,
1027+
NotFound => CLIENT_404_NOT_FOUND,
1028+
}
1029+
}
1030+
}
1031+
9701032
// --- CommonApiError / CommonErrorKind impls --- //
9711033

9721034
impl CommonApiError {
@@ -1439,6 +1501,38 @@ impl RunnerApiError {
14391501
}
14401502
}
14411503

1504+
impl SdkApiError {
1505+
pub fn command(error: impl fmt::Display) -> Self {
1506+
let msg = format!("{error:#}");
1507+
let kind = SdkErrorKind::Command;
1508+
Self {
1509+
kind,
1510+
msg,
1511+
..Default::default()
1512+
}
1513+
}
1514+
1515+
pub fn bad_auth(error: impl fmt::Display) -> Self {
1516+
let msg = format!("{error:#}");
1517+
let kind = SdkErrorKind::BadAuth;
1518+
Self {
1519+
kind,
1520+
msg,
1521+
..Default::default()
1522+
}
1523+
}
1524+
1525+
pub fn not_found(error: impl fmt::Display) -> Self {
1526+
let msg = format!("{error:#}");
1527+
let kind = SdkErrorKind::NotFound;
1528+
Self {
1529+
kind,
1530+
msg,
1531+
..Default::default()
1532+
}
1533+
}
1534+
}
1535+
14421536
// --- Build JSON response --- //
14431537

14441538
pub mod error_response {}
@@ -1612,6 +1706,7 @@ mod test {
16121706
invariants::assert_error_kind_invariants::<MegaErrorKind>();
16131707
invariants::assert_error_kind_invariants::<NodeErrorKind>();
16141708
invariants::assert_error_kind_invariants::<RunnerErrorKind>();
1709+
invariants::assert_error_kind_invariants::<SdkErrorKind>();
16151710
}
16161711

16171712
#[test]
@@ -1623,6 +1718,7 @@ mod test {
16231718
assert_api_error_invariants::<MegaApiError, MegaErrorKind>();
16241719
assert_api_error_invariants::<NodeApiError, NodeErrorKind>();
16251720
assert_api_error_invariants::<RunnerApiError, RunnerErrorKind>();
1721+
assert_api_error_invariants::<SdkApiError, SdkErrorKind>();
16261722
}
16271723

16281724
#[test]

0 commit comments

Comments
 (0)