hasura/graphql-engine · error · Error

invalid string literal found: {0:?}

Error message

invalid string literal found: {0:?}

What it means

Wrapper error: the lexer's string sub-scanner (string::Error — unterminated string, invalid character, bad escape, bad unicode escape) failed while scanning a string literal, and the detail is included via {0:?}.

Source

Thrown at v3/crates/graphql/lang-graphql/src/lexer.rs:151

/// Error when tokenizing the input source
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum Error {
    /// An unexpected character was found
    ///
    /// Unexpected characters are characters that _do_ exist in the GraphQL
    /// language, but is not expected at the current position in the document.
    #[error("unexpected character in the document: {0:?}")]
    UnexpectedCharacter(char),

    /// The input source was unexpectedly terminated
    ///
    /// Emitted when the current token requires a succeeding character, but
    /// the source has reached EOF. Emitted when scanning e.g. `"1."`.
    #[error("end of file reached when expecting further input")]
    UnexpectedEndOfFile,

    /// An invalid string literal was found
    #[error("invalid string literal found: {0:?}")]
    InvalidString(string::Error),

    /// An invalid number literal was found
    #[error("invalid number literal found: {0:?}")]
    InvalidNumber(number::Error),

    // An invalid graphql name
    #[error("invalid graphql name: {0}")]
    InvalidGraphQlName(String),
}

impl From<ast::common::InvalidGraphQlName> for Error {
    fn from(error: ast::common::InvalidGraphQlName) -> Self {
        Error::InvalidGraphQlName(error.0)
    }
}

pub type Result = core::result::Result<Spanning<Token>, Positioned<Error>>;

View on GitHub (pinned to 724551b9ae)

Solutions

  1. Look at the inner string::Error variant for the specific cause
  2. Apply the corresponding fix (escape, terminate the string, fix \u)
  3. Consider using variables instead of inlining user text into string literals

Example fix

// before
format!("{{ search(term: \"{}\") }}", user_input)  // breaks if input has quotes/backslashes
// after
use variables: query($term: String!) { search(term: $term) } with parameter binding
Defensive patterns

Strategy: try-catch

Type guard

fn is_string_lex_error(e: &lexer::Error) -> bool { matches!(e, lexer::Error::InvalidString(_)) }

Try / catch

match result { Err(lexer::Error::InvalidString(inner)) => match inner { string::Error::Unterminated => ..., string::Error::UnknownEscapeSequence(seq) => ..., _ => ... }, _ => {} }

Prevention

When it happens

Trigger: Any string-literal scanning failure: unterminated quote, raw control character (UnknownCharacterInString), unknown escape (UnknownEscapeSequence), or malformed \u (InvalidUnicodeEscapeSequence).

Common situations: Umbrella error seen at the parser level when any of the string scanning rules are violated, typically from unescaped user-supplied text interpolated into queries.

Related errors


AI-assisted analysis of hasura/graphql-engine@724551b9ae (2026-08-28). Data as JSON: /api/errors/6cb43ea02267aa83. Report an issue: GitHub.