Skip to content

Commit b977ac6

Browse files
authored
Improve GraphQL client errors. (#3640)
## Motivation I introduced an `.unwrap()` in #3632. ## Proposal Return an error instead. ## Test Plan CI ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links - Related to #3632. - [reviewer checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
1 parent bb684d0 commit b977ac6

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

linera-indexer/lib/src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ pub enum IndexerError {
3838
UnknownPlugin(String),
3939
#[error("Plugin not loaded: {0}")]
4040
UnloadedPlugin(String),
41-
#[error("Unknown certificate status: {0:?}")]
42-
UnknownCertificateStatus(String),
41+
#[error(transparent)]
42+
ConversionError(linera_service_graphql_client::ConversionError),
4343
#[error("Different plugins in command line and memory")]
4444
WrongPlugins,
4545
#[error("Plugin is already registered")]

linera-indexer/lib/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Service {
9797
.block
9898
.ok_or_else(|| IndexerError::NotFound(hash))?
9999
.try_into()
100-
.map_err(IndexerError::UnknownCertificateStatus)
100+
.map_err(IndexerError::ConversionError)
101101
}
102102

103103
/// Gets chains

linera-service-graphql-client/src/service.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use linera_base::{
1010
GenericApplicationId, StreamName,
1111
},
1212
};
13+
use thiserror::Error;
1314

1415
pub type JSONObject = serde_json::Value;
1516

@@ -131,6 +132,14 @@ pub struct Notifications;
131132
)]
132133
pub struct Transfer;
133134

135+
#[derive(Error, Debug)]
136+
pub enum ConversionError {
137+
#[error(transparent)]
138+
Serde(#[from] serde_json::Error),
139+
#[error("Unexpected certificate type: {0}")]
140+
UnexpectedCertificateType(String),
141+
}
142+
134143
#[cfg(not(target_arch = "wasm32"))]
135144
mod from {
136145
use linera_base::{data_types::Event, hashed::Hashed, identifiers::StreamId};
@@ -220,8 +229,10 @@ mod from {
220229
}
221230
}
222231

223-
impl From<block::BlockBlockValueBlock> for ExecutedBlock {
224-
fn from(val: block::BlockBlockValueBlock) -> Self {
232+
impl TryFrom<block::BlockBlockValueBlock> for ExecutedBlock {
233+
type Error = serde_json::Error;
234+
235+
fn try_from(val: block::BlockBlockValueBlock) -> Result<Self, Self::Error> {
225236
let block::BlockBlockValueBlock { header, body } = val;
226237
let block::BlockBlockValueBlockHeader {
227238
chain_id,
@@ -277,7 +288,7 @@ mod from {
277288
.into_iter()
278289
.map(|messages| messages.into_iter().map(Into::into).collect())
279290
.collect::<Vec<Vec<_>>>(),
280-
previous_message_blocks: serde_json::from_value(previous_message_blocks).unwrap(),
291+
previous_message_blocks: serde_json::from_value(previous_message_blocks)?,
281292
operations,
282293
oracle_responses: oracle_responses.into_iter().map(Into::into).collect(),
283294
events: events
@@ -291,11 +302,11 @@ mod from {
291302
operation_results,
292303
};
293304

294-
Block {
305+
Ok(Block {
295306
header: block_header,
296307
body: block_body,
297308
}
298-
.into()
309+
.into())
299310
}
300311
}
301312

@@ -319,11 +330,12 @@ mod from {
319330
}
320331

321332
impl TryFrom<block::BlockBlock> for Hashed<ConfirmedBlock> {
322-
type Error = String;
333+
type Error = ConversionError;
334+
323335
fn try_from(val: block::BlockBlock) -> Result<Self, Self::Error> {
324336
match (val.value.status.as_str(), val.value.block) {
325-
("confirmed", block) => Ok(Hashed::new(ConfirmedBlock::new(block.into()))),
326-
_ => Err(val.value.status),
337+
("confirmed", block) => Ok(Hashed::new(ConfirmedBlock::new(block.try_into()?))),
338+
_ => Err(ConversionError::UnexpectedCertificateType(val.value.status)),
327339
}
328340
}
329341
}

0 commit comments

Comments
 (0)