diff --git a/cynic-parser/CHANGELOG.md b/cynic-parser/CHANGELOG.md index f453ec69..05c0f362 100644 --- a/cynic-parser/CHANGELOG.md +++ b/cynic-parser/CHANGELOG.md @@ -9,6 +9,10 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html ## Unreleased - xxxx-xx-xx +### Breaking Changes + +- `Error::span` now returns an `Option` instead of a `Span` + ## v0.8.7 - 2024-12-03 ### Changes @@ -23,7 +27,6 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html - Fixed the `VariableValue` debug impl which was misleading ([#1104](https://github.com/obmarg/cynic/pull/1104)) - ## v0.8.5 - 2024-11-27 ### New Features diff --git a/cynic-parser/src/errors.rs b/cynic-parser/src/errors.rs index 66bd0fa4..9b57ea4c 100644 --- a/cynic-parser/src/errors.rs +++ b/cynic-parser/src/errors.rs @@ -55,25 +55,32 @@ pub enum Error { /// Variable found in const position VariableInConstPosition(usize, String, usize), + + /// The GraphQl document was empty + EmptyTypeSystemDocument, + + /// The GraphQl document was empty + EmptyExecutableDocument, } impl Error { - pub fn span(&self) -> Span { + pub fn span(&self) -> Option { match self { - Error::InvalidToken { location } => Span::new(*location, *location), - Error::UnrecognizedEof { location, .. } => Span::new(*location, *location), + Error::InvalidToken { location } => Span::new(*location, *location).into(), + Error::UnrecognizedEof { location, .. } => Span::new(*location, *location).into(), Error::UnrecognizedToken { token: (start, _, end), .. - } => Span::new(*start, *end), + } => Span::new(*start, *end).into(), Error::ExtraToken { token: (start, _, end), .. - } => Span::new(*start, *end), - Error::Lexical(error) => error.span(), - Error::MalformedStringLiteral(error) => error.span(), - Error::MalformedDirectiveLocation(lhs, _, rhs) => Span::new(*lhs, *rhs), - Error::VariableInConstPosition(lhs, _, rhs) => Span::new(*lhs, *rhs), + } => Span::new(*start, *end).into(), + Error::Lexical(error) => error.span().into(), + Error::MalformedStringLiteral(error) => error.span().into(), + Error::MalformedDirectiveLocation(lhs, _, rhs) => Span::new(*lhs, *rhs).into(), + Error::VariableInConstPosition(lhs, _, rhs) => Span::new(*lhs, *rhs).into(), + Error::EmptyExecutableDocument | Error::EmptyTypeSystemDocument => None, } } } @@ -87,7 +94,9 @@ impl std::error::Error for Error { | Error::ExtraToken { .. } | Error::MalformedStringLiteral(..) | Error::MalformedDirectiveLocation(..) - | Error::VariableInConstPosition(..) => None, + | Error::VariableInConstPosition(..) + | Error::EmptyTypeSystemDocument + | Error::EmptyExecutableDocument => None, Error::Lexical(error) => Some(error), } } @@ -157,6 +166,18 @@ impl fmt::Display for Error { )?; Ok(()) } + Error::EmptyExecutableDocument => { + write!( + f, + "the graphql document was empty, please provide an operation" + ) + } + Error::EmptyTypeSystemDocument => { + write!( + f, + "the graphql document was empty, please provide at least one definition" + ) + } } } } diff --git a/cynic-parser/src/errors/report.rs b/cynic-parser/src/errors/report.rs index dc68abb2..46413fb2 100644 --- a/cynic-parser/src/errors/report.rs +++ b/cynic-parser/src/errors/report.rs @@ -15,9 +15,12 @@ impl Error { let mut builder = ariadne::Report::build(ReportKind::Error, (), 0) .with_message(message) - .with_label(label) .with_config(Config::default().with_color(false)); + if let Some(label) = label { + builder.add_label(label); + } + if let Some(note) = note { builder.set_note(note) } @@ -27,16 +30,20 @@ impl Error { Report { inner, document } } - fn components(&self) -> (String, Label, Option) { + fn components(&self) -> (String, Option