Skip to content

Commit 37a5ec0

Browse files
committed
Leverage derive_more::Error
1 parent d87e499 commit 37a5ec0

File tree

15 files changed

+43
-91
lines changed

15 files changed

+43
-91
lines changed

juniper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bson = { version = "2.4", optional = true }
5151
chrono = { version = "0.4.30", features = ["alloc"], default-features = false, optional = true }
5252
chrono-tz = { version = "0.10", default-features = false, optional = true }
5353
compact_str = "0.9"
54-
derive_more = { version = "2.0", features = ["debug", "display"] }
54+
derive_more = { version = "2.0", features = ["debug", "display", "error"] }
5555
fnv = "1.0.5"
5656
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
5757
graphql-parser = { version = "0.4", optional = true }

juniper/src/integrations/jiff.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
//! [s7]: https://graphql-scalars.dev/docs/scalars/duration
5151
//! [1]: https://docs.rs/jiff/latest/jiff/index.html#time-zone-features
5252
53-
use std::{error::Error, str};
53+
use std::str;
5454

55-
use derive_more::with_trait::{Debug, Display};
55+
use derive_more::with_trait::{Debug, Display, Error};
5656

5757
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
5858

@@ -411,23 +411,18 @@ mod time_zone_or_utc_offset {
411411
}
412412

413413
/// Error parsing a [`TimeZone`] value.
414-
#[derive(Clone, Debug, Display)]
414+
#[derive(Clone, Debug, Display, Error)]
415415
pub enum TimeZoneParsingError {
416416
/// Identifier cannot not be parsed by the [`jiff::tz::TimeZone::get()`] method.
417417
InvalidTimeZone(jiff::Error),
418418

419419
/// GraphQL scalar [`TimeZone`] requires `tz::TimeZone` with IANA name.
420420
#[display("missing IANA name")]
421-
MissingIanaName(#[debug(ignore)] jiff::tz::TimeZone),
422-
}
423-
424-
impl Error for TimeZoneParsingError {
425-
fn source(&self) -> Option<&(dyn Error + 'static)> {
426-
match self {
427-
Self::InvalidTimeZone(e) => Some(e),
428-
Self::MissingIanaName(..) => None,
429-
}
430-
}
421+
MissingIanaName(
422+
#[debug(ignore)]
423+
#[error(not(source))]
424+
jiff::tz::TimeZone,
425+
),
431426
}
432427

433428
/// Representation of a time zone from the [IANA Time Zone Database][0].

juniper/src/parser/lexer.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{char, iter::Peekable, str::CharIndices};
22

3-
use derive_more::with_trait::Display;
3+
use derive_more::with_trait::{Display, Error};
44

55
use crate::parser::{SourcePosition, Spanning};
66

@@ -63,21 +63,21 @@ pub enum Token<'a> {
6363
}
6464

6565
/// Error when tokenizing the input source
66-
#[derive(Clone, Debug, Display, Eq, PartialEq)]
66+
#[derive(Clone, Debug, Display, Eq, Error, PartialEq)]
6767
pub enum LexerError {
6868
/// An unknown character was found
6969
///
7070
/// Unknown characters are characters that do not occur anywhere in the
7171
/// GraphQL language, such as `?` or `%`.
7272
#[display("Unknown character \"{_0}\"")]
73-
UnknownCharacter(char),
73+
UnknownCharacter(#[error(not(source))] char),
7474

7575
/// An unexpected character was found
7676
///
7777
/// Unexpected characters are characters that _do_ exist in the GraphQL
7878
/// language, but is not expected at the current position in the document.
7979
#[display("Unexpected character \"{_0}\"")]
80-
UnexpectedCharacter(char),
80+
UnexpectedCharacter(#[error(not(source))] char),
8181

8282
/// An unterminated string literal was found
8383
///
@@ -92,14 +92,14 @@ pub enum LexerError {
9292
/// This occurs when an invalid source character is found in a string
9393
/// literal, such as ASCII control characters.
9494
#[display("Unknown character \"{_0}\" in string literal")]
95-
UnknownCharacterInString(char),
95+
UnknownCharacterInString(#[error(not(source))] char),
9696

9797
/// An unknown escape sequence in a string literal was found
9898
///
9999
/// Only a limited set of escape sequences are supported, this is emitted
100100
/// when e.g. `"\l"` is parsed.
101101
#[display("Unknown escape sequence \"{_0}\" in string")]
102-
UnknownEscapeSequence(String),
102+
UnknownEscapeSequence(#[error(not(source))] String),
103103

104104
/// The input source was unexpectedly terminated
105105
///
@@ -516,5 +516,3 @@ fn is_name_cont(c: char) -> bool {
516516
fn is_number_start(c: char) -> bool {
517517
c == '-' || c.is_ascii_digit()
518518
}
519-
520-
impl std::error::Error for LexerError {}

juniper/src/parser/parser.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
use std::{error::Error, fmt};
1+
use std::fmt;
22

33
use compact_str::{CompactString, format_compact};
4-
use derive_more::with_trait::Display;
4+
use derive_more::with_trait::{Display, Error};
55

66
use crate::parser::{Lexer, LexerError, Spanning, Token};
77

88
/// Error while parsing a GraphQL query
9-
#[derive(Clone, Debug, Display, Eq, PartialEq)]
9+
#[derive(Clone, Debug, Display, Eq, Error, PartialEq)]
1010
pub enum ParseError {
1111
/// An unexpected token occurred in the source
1212
// TODO: Previously was `Token<'a>`.
1313
// Revisit on `graphql-parser` integration.
1414
#[display("Unexpected \"{_0}\"")]
15-
UnexpectedToken(CompactString),
15+
UnexpectedToken(#[error(not(source))] CompactString),
1616

1717
/// The input source abruptly ended
1818
#[display("Unexpected end of input")]
@@ -22,18 +22,7 @@ pub enum ParseError {
2222
LexerError(LexerError),
2323

2424
/// A scalar of unexpected type occurred in the source
25-
ExpectedScalarError(&'static str),
26-
}
27-
28-
impl Error for ParseError {
29-
fn source(&self) -> Option<&(dyn Error + 'static)> {
30-
match self {
31-
Self::LexerError(e) => Some(e),
32-
Self::ExpectedScalarError(_) | Self::UnexpectedToken(_) | Self::UnexpectedEndOfFile => {
33-
None
34-
}
35-
}
36-
}
25+
ExpectedScalarError(#[error(not(source))] &'static str),
3726
}
3827

3928
impl ParseError {

juniper/src/parser/utils.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use derive_more::with_trait::Display;
3+
use derive_more::with_trait::{Display, Error};
44

55
/// A reference to a line and column in an input source file
66
#[derive(Clone, Copy, Debug, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
@@ -54,9 +54,10 @@ impl Span {
5454
}
5555

5656
/// Data structure used to wrap items into a [`Span`].
57-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
57+
#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
5858
pub struct Spanning<T, Sp = Span> {
5959
/// Wrapped item.
60+
#[error(source)]
6061
pub item: T,
6162

6263
/// [`Span`] of the wrapped item.
@@ -145,8 +146,6 @@ impl<T: Display> Display for Spanning<T> {
145146
}
146147
}
147148

148-
impl<T: std::error::Error> std::error::Error for Spanning<T> {}
149-
150149
impl SourcePosition {
151150
#[doc(hidden)]
152151
pub fn new(index: usize, line: usize, col: usize) -> SourcePosition {

juniper/src/types/name.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{borrow::Borrow, error::Error};
1+
use std::borrow::Borrow;
22

33
use arcstr::ArcStr;
4-
use derive_more::with_trait::Display;
4+
use derive_more::with_trait::{Display, Error};
55

66
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
77
pub struct Name(ArcStr);
@@ -49,14 +49,8 @@ impl Borrow<str> for Name {
4949
}
5050
}
5151

52-
#[derive(Clone, Debug, Display, Eq, Ord, PartialEq, PartialOrd)]
53-
pub struct NameParseError(ArcStr);
54-
55-
impl Error for NameParseError {
56-
fn description(&self) -> &str {
57-
&self.0
58-
}
59-
}
52+
#[derive(Clone, Debug, Display, Eq, Error, Ord, PartialEq, PartialOrd)]
53+
pub struct NameParseError(#[error(not(source))] ArcStr);
6054

6155
#[test]
6256
fn test_name_is_valid() {

juniper/src/validation/context.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{collections::HashSet, fmt::Debug};
22

3-
use derive_more::with_trait::Display;
3+
use derive_more::with_trait::{Display, Error};
44
use itertools::Itertools as _;
55

66
use crate::{
@@ -13,7 +13,7 @@ use crate::schema::{meta::MetaType, model::SchemaType};
1313
use crate::parser::SourcePosition;
1414

1515
/// Query validation error
16-
#[derive(Clone, Debug, Display, Eq, Ord, PartialEq, PartialOrd)]
16+
#[derive(Clone, Debug, Display, Eq, Error, Ord, PartialEq, PartialOrd)]
1717
#[display("{message}. At {}", locations.iter().format(", "))]
1818
pub struct RuleError {
1919
locations: Vec<SourcePosition>,
@@ -55,8 +55,6 @@ impl RuleError {
5555
}
5656
}
5757

58-
impl std::error::Error for RuleError {}
59-
6058
impl<'a, S: Debug> ValidatorContext<'a, S> {
6159
#[doc(hidden)]
6260
pub fn new(schema: &'a SchemaType<S>, document: &Document<'a, S>) -> ValidatorContext<'a, S> {

juniper_actix/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ subscriptions = [
3232
[dependencies]
3333
actix-web = "4.4"
3434
actix-ws = { version = "0.3", optional = true }
35-
derive_more = { version = "2.0", features = ["display"], optional = true }
35+
derive_more = { version = "2.0", features = ["display", "error"], optional = true }
3636
futures = { version = "0.3.22", optional = true }
3737
juniper = { version = "0.16", path = "../juniper", default-features = false }
3838
juniper_graphql_ws = { version = "0.4.0", path = "../juniper_graphql_ws", features = ["graphql-transport-ws", "graphql-ws"], optional = true }

juniper_actix/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub mod subscriptions {
174174
http::header::{HeaderName, HeaderValue},
175175
web,
176176
};
177-
use derive_more::with_trait::Display;
177+
use derive_more::with_trait::{Display, Error as StdError};
178178
use futures::{SinkExt as _, StreamExt as _, future};
179179
use juniper::{GraphQLSubscriptionType, GraphQLTypeAsync, RootNode, ScalarValue};
180180
use juniper_graphql_ws::{ArcSchema, Init, graphql_transport_ws, graphql_ws};
@@ -418,7 +418,7 @@ pub mod subscriptions {
418418
}
419419

420420
/// Possible errors of serving an [`actix_ws`] connection.
421-
#[derive(Debug, Display)]
421+
#[derive(Debug, Display, StdError)]
422422
enum Error {
423423
/// Deserializing of a client [`actix_ws::Message`] failed.
424424
#[display("`serde` error: {_0}")]
@@ -428,8 +428,6 @@ pub mod subscriptions {
428428
#[display("unexpected message received from client: {_0:?}")]
429429
UnexpectedClientMessage(actix_ws::Message),
430430
}
431-
432-
impl std::error::Error for Error {}
433431
}
434432

435433
#[cfg(test)]

juniper_axum/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ subscriptions = [
3131

3232
[dependencies]
3333
axum = { version = "0.8", features = ["json", "query"], default-features = false }
34-
derive_more = { version = "2.0", features = ["display"], optional = true }
34+
derive_more = { version = "2.0", features = ["display", "error"], optional = true }
3535
futures = { version = "0.3.22", optional = true }
3636
juniper = { version = "0.16", path = "../juniper", default-features = false }
3737
juniper_graphql_ws = { version = "0.4.0", path = "../juniper_graphql_ws", features = ["graphql-transport-ws"] }

juniper_axum/src/subscriptions.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use axum::{
77
},
88
response::Response,
99
};
10-
use derive_more::with_trait::Display;
10+
use derive_more::with_trait::{Display, Error as StdError};
1111
use futures::{SinkExt as _, StreamExt as _, future};
1212
use juniper::ScalarValue;
1313
use juniper_graphql_ws::{Init, Schema, graphql_transport_ws, graphql_ws};
@@ -670,7 +670,7 @@ impl<S: ScalarValue> TryFrom<Message> for graphql_ws::ClientMessage<S> {
670670
}
671671

672672
/// Possible errors of serving a [`WebSocket`] connection.
673-
#[derive(Debug, Display)]
673+
#[derive(Debug, Display, StdError)]
674674
enum Error {
675675
/// Deserializing of a client [`ws::Message`] failed.
676676
#[display("`serde` error: {_0}")]
@@ -680,5 +680,3 @@ enum Error {
680680
#[display("unexpected message received from client: {_0:?}")]
681681
UnexpectedClientMessage(ws::Message),
682682
}
683-
684-
impl std::error::Error for Error {}

juniper_hyper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ keywords = ["apollo", "graphql", "hyper", "juniper"]
1818
exclude = ["/examples/", "/release.toml"]
1919

2020
[dependencies]
21-
derive_more = { version = "2.0", features = ["debug", "display"] }
21+
derive_more = { version = "2.0", features = ["debug", "display", "error"] }
2222
http-body-util = "0.1"
2323
hyper = { version = "1.0", features = ["server"] }
2424
juniper = { version = "0.16", path = "../juniper", default-features = false }

juniper_hyper/src/lib.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#![cfg_attr(not(any(doc, test)), doc = env!("CARGO_PKG_NAME"))]
33
#![cfg_attr(test, expect(unused_crate_dependencies, reason = "examples"))]
44

5-
use std::{error::Error, string::FromUtf8Error, sync::Arc};
5+
use std::{string::FromUtf8Error, sync::Arc};
66

7-
use derive_more::with_trait::{Debug, Display};
7+
use derive_more::with_trait::{Debug, Display, Error};
88
use http_body_util::BodyExt as _;
99
use hyper::{
1010
Method, Request, Response, StatusCode,
@@ -313,7 +313,7 @@ fn new_html_response(code: StatusCode) -> Response<String> {
313313
}
314314

315315
// TODO: Use `#[debug(forward)]` once `derive_more::Debug` is capable of it.
316-
#[derive(Debug, Display)]
316+
#[derive(Debug, Display, Error)]
317317
enum GraphQLRequestError<B: Body> {
318318
#[debug("{_0:?}")]
319319
BodyHyper(B::Error),
@@ -324,22 +324,7 @@ enum GraphQLRequestError<B: Body> {
324324
#[debug("{_0:?}")]
325325
Variables(SerdeError),
326326
#[debug("{_0:?}")]
327-
Invalid(String),
328-
}
329-
330-
impl<B> Error for GraphQLRequestError<B>
331-
where
332-
B: Body<Error: Error + 'static>,
333-
{
334-
fn source(&self) -> Option<&(dyn Error + 'static)> {
335-
match self {
336-
Self::BodyHyper(e) => Some(e),
337-
Self::BodyUtf8(e) => Some(e),
338-
Self::BodyJSONError(e) => Some(e),
339-
Self::Variables(e) => Some(e),
340-
Self::Invalid(_) => None,
341-
}
342-
}
327+
Invalid(#[error(not(source))] String),
343328
}
344329

345330
#[cfg(test)]

juniper_warp/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ subscriptions = [
3030
]
3131

3232
[dependencies]
33-
derive_more = { version = "2.0", features = ["display"] }
33+
derive_more = { version = "2.0", features = ["display", "error"] }
3434
futures = { version = "0.3.22", optional = true }
3535
juniper = { version = "0.16", path = "../juniper", default-features = false }
3636
juniper_graphql_ws = { version = "0.4.0", path = "../juniper_graphql_ws", features = ["graphql-transport-ws", "graphql-ws"], optional = true }

juniper_warp/src/subscriptions.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{convert::Infallible, sync::Arc};
44

5-
use derive_more::with_trait::Display;
5+
use derive_more::with_trait::{Display, Error as StdError};
66
use futures::{
77
future::{self, Either},
88
sink::SinkExt as _,
@@ -39,7 +39,7 @@ impl<S: ScalarValue> TryFrom<Message> for graphql_transport_ws::Input<S> {
3939
}
4040

4141
/// Errors that can happen while serving a connection.
42-
#[derive(Debug, Display)]
42+
#[derive(Debug, Display, StdError)]
4343
pub enum Error {
4444
/// Errors that can happen in Warp while serving a connection.
4545
#[display("`warp` error: {_0}")]
@@ -51,8 +51,6 @@ pub enum Error {
5151
Serde(serde_json::Error),
5252
}
5353

54-
impl std::error::Error for Error {}
55-
5654
impl From<warp::Error> for Error {
5755
fn from(err: warp::Error) -> Self {
5856
Self::Warp(err)

0 commit comments

Comments
 (0)