Skip to content

Commit 84332ff

Browse files
committed
improved client error type
1 parent 4b37d51 commit 84332ff

File tree

5 files changed

+11
-34
lines changed

5 files changed

+11
-34
lines changed

tmc-client/src/error.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ type TokenError = oauth2::RequestTokenError<
1313
/// The main error type for tmc-client.
1414
#[derive(Debug, Error)]
1515
pub enum ClientError {
16-
// Arc
17-
#[error("Tried to mutate client while it was borrowed")]
18-
ArcBorrowed,
19-
20-
// file IO
21-
#[error("Failed to create temporary file")]
22-
TempFile(#[source] std::io::Error),
23-
24-
// network
2516
#[error("HTTP error {status} for {url}: {error}. Obsolete client: {obsolete_client}")]
2617
HttpError {
2718
url: Url,
@@ -33,37 +24,23 @@ pub enum ClientError {
3324
ConnectionError(Method, Url, #[source] reqwest::Error),
3425
#[error("OAuth2 password exchange error")]
3526
Token(#[source] Box<TokenError>),
36-
#[error("OAuth2 unexpected token response: {0}")]
37-
TokenParse(String, #[source] serde_json::error::Error),
3827
#[error("Failed to parse as URL: {0}")]
3928
UrlParse(String, #[source] url::ParseError),
4029
#[error("Failed to write response")]
4130
HttpWriteResponse(#[source] reqwest::Error),
4231
#[error("Failed to deserialize response from {0} as JSON")]
4332
HttpJsonResponse(Url, #[source] reqwest::Error),
4433

45-
#[error("Failed to download some exercises")]
46-
IncompleteDownloadResult {
47-
downloaded: Vec<u32>,
48-
failed: Vec<(u32, Box<ClientError>)>,
49-
},
50-
5134
#[error("Already authenticated")]
5235
AlreadyAuthenticated,
5336
#[error("Authentication required")]
54-
NotLoggedIn,
55-
#[error("Failed to find cache directory")]
56-
CacheDir,
57-
#[error("No values found in exercise details map returned by server")]
58-
MissingDetailsValue,
59-
#[error("List of exercises given was empty")]
60-
NoExercisesGiven,
37+
NotAuthenticated,
6138

6239
#[error(transparent)]
6340
SystemTime(#[from] std::time::SystemTimeError),
6441
#[error(transparent)]
6542
WalkDir(#[from] walkdir::Error),
66-
#[error("File IO error")]
43+
#[error(transparent)]
6744
FileError(#[from] FileError),
6845
#[error(transparent)]
6946
Plugin(#[from] tmc_langs_plugins::PluginError),

tmc-client/src/response.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ pub struct ExerciseDetails {
247247
}
248248

249249
/// get /api/v8/core/exercises/details
250+
#[derive(Debug, Deserialize)]
251+
pub(crate) struct ExercisesDetailsWrapper {
252+
pub exercises: Vec<ExercisesDetails>,
253+
}
254+
250255
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
251256
pub struct ExercisesDetails {
252257
pub id: u32,

tmc-client/src/tmc_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl TmcClient {
5959
if self.0.token.is_some() {
6060
Ok(())
6161
} else {
62-
Err(ClientError::NotLoggedIn)
62+
Err(ClientError::NotAuthenticated)
6363
}
6464
}
6565

tmc-client/src/tmc_client/api_v8.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,8 @@ pub mod core {
819819
.join(","),
820820
);
821821

822-
// returns map with result in key "exercises"
823-
let res: HashMap<String, Vec<ExercisesDetails>> = get_json(client, url, &[exercise_ids])?;
824-
if let Some((_, val)) = res.into_iter().next() {
825-
// just return whatever value is found first
826-
return Ok(val);
827-
}
828-
Err(ClientError::MissingDetailsValue)
822+
let res: ExercisesDetailsWrapper = get_json(client, url, &[exercise_ids])?;
823+
Ok(res.exercises)
829824
}
830825

831826
/// get /api/v8/core/exercises/{exercise_id}/solution/download

tmc-langs-cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn solve_error_kind(e: &anyhow::Error) -> Kind {
142142
return Kind::NotLoggedIn;
143143
}
144144
}
145-
Some(ClientError::NotLoggedIn) => {
145+
Some(ClientError::NotAuthenticated) => {
146146
return Kind::NotLoggedIn;
147147
}
148148
Some(ClientError::ConnectionError(..)) => {

0 commit comments

Comments
 (0)