Skip to content

Commit f0990a8

Browse files
committed
tidy error handling
1 parent 68a7928 commit f0990a8

File tree

2 files changed

+17
-39
lines changed

2 files changed

+17
-39
lines changed

graphql_client_codegen/src/lib.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod tests;
2828

2929
pub use crate::codegen_options::{CodegenMode, GraphQLClientCodegenOptions};
3030

31-
use std::{collections::BTreeMap, fmt::Display, io};
31+
use std::{collections::BTreeMap, io};
3232

3333
#[derive(Debug, thiserror::Error)]
3434
#[error("{0}")]
@@ -162,32 +162,20 @@ fn generate_module_token_stream_inner(
162162
Ok(modules)
163163
}
164164

165-
#[derive(Debug)]
165+
#[derive(Debug, thiserror::Error)]
166166
enum ReadFileError {
167-
FileNotFound { path: String, io_error: io::Error },
168-
ReadError { path: String, io_error: io::Error },
169-
}
170-
171-
impl Display for ReadFileError {
172-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173-
match self {
174-
ReadFileError::FileNotFound { path, .. } => {
175-
write!(f, "Could not find file with path: {path}\nHint: file paths in the GraphQLQuery attribute are relative to the project root (location of the Cargo.toml). Example: query_path = \"src/my_query.graphql\".")
176-
}
177-
ReadFileError::ReadError { path, .. } => {
178-
write!(f, "Error reading file at: {}", path)
179-
}
180-
}
181-
}
182-
}
183-
184-
impl std::error::Error for ReadFileError {
185-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
186-
match self {
187-
ReadFileError::FileNotFound { io_error, .. }
188-
| ReadFileError::ReadError { io_error, .. } => Some(io_error),
189-
}
190-
}
167+
#[error("Could not find file with path: {path}\nHint: file paths in the GraphQLQuery attribute are relative to the project root (location of the Cargo.toml). Example: query_path = \"src/my_query.graphql\".")]
168+
FileNotFound {
169+
path: String,
170+
#[source]
171+
io_error: io::Error,
172+
},
173+
#[error("Error reading file at: {path}")]
174+
ReadError {
175+
path: String,
176+
#[source]
177+
io_error: io::Error,
178+
},
191179
}
192180

193181
fn read_file(path: &std::path::Path) -> Result<String, ReadFileError> {

graphql_client_codegen/src/query.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,14 @@ use crate::{
1818
StoredInputType, StoredScalar, TypeId, UnionId,
1919
},
2020
};
21-
use std::{
22-
collections::{BTreeMap, BTreeSet},
23-
fmt::Display,
24-
};
21+
use std::collections::{BTreeMap, BTreeSet};
2522

26-
#[derive(Debug)]
23+
#[derive(Debug, thiserror::Error)]
24+
#[error("{message}")]
2725
pub(crate) struct QueryValidationError {
2826
message: String,
2927
}
3028

31-
impl Display for QueryValidationError {
32-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33-
f.write_str(&self.message)
34-
}
35-
}
36-
37-
impl std::error::Error for QueryValidationError {}
38-
3929
impl QueryValidationError {
4030
pub(crate) fn new(message: String) -> Self {
4131
QueryValidationError { message }

0 commit comments

Comments
 (0)