dgraph-io/dgraph · error
Not a valid escape char: '%c'
Error message
Not a valid escape char: '%c'
What it means
Inside LexQuotedString, a backslash starts an escape sequence; the next rune must be one of the lexer's valid escape characters (checked via IsEscChar). If it is not, the lexer rejects the string with this error naming the offending character. Dgraph's lexer supports a limited escape set, so arbitrary \x sequences or stray backslashes fail.
Source
Thrown at lex/lexer.go:410
return r == '\u000A' || r == '\u000D'
}
// LexQuotedString properly processes a quoted string (by taking care of escaped characters).
func (l *Lexer) LexQuotedString() error {
l.Backup()
r := l.Next()
if r != quote {
return errors.Errorf("String should start with quote.")
}
for {
r := l.Next()
if r == EOF {
return errors.Errorf("Unexpected end of input.")
}
if r == '\\' {
r := l.Next()
if !l.IsEscChar(r) {
return errors.Errorf("Not a valid escape char: '%c'", r)
}
continue // eat the next char
}
if r == quote {
break
}
}
return nil
}
View on GitHub (pinned to 759e242be6)
Solutions
- Double the backslash (write \\\\ in source to mean a literal backslash) or remove the escape
- Use only supported escape characters (\\", \\\\, \\n, \\t, etc. — see IsEscChar) inside string literals
- When embedding JSON, use Dgraph's JSON mutation format instead of hand-escaped N-Quads
- Build queries with parameterized/builder libraries instead of string concatenation
Example fix
// before <0x01> <path> "C:\Users\bob" . // after <0x01> <path> "C:\\Users\\bob" .
Defensive patterns
Strategy: validation
Validate before calling
// Validate escapes in string literals: allowed set per lexer IsEscChar
var validEsc = map[rune]bool{'n': true, 't': true, 'r': true, '"': true, '\\': true, '\'': true}
func checkEscapes(s string) error {
for i, r := range s {
if r == '\\' && i+1 < len([]rune(s)) {
nxt := []rune(s)[i+1]
if !validEsc[nxt] {
return fmt.Errorf("invalid escape \\%c at %d", nxt, i)
}
}
}
return nil
} Try / catch
if err := mutate(q); err != nil && strings.Contains(err.Error(), "Not a valid escape char") {
return fmt.Errorf("bad escape sequence in literal: %w", err)
} Prevention
- Double every backslash in paths and regexes inside string literals
- Build queries with JSON APIs or builder libraries to avoid manual escaping
- Never hand-concatenate user input into N-Quads; sanitize backslashes first
When it happens
Trigger: A string literal in a DQL query or RDF mutation contains '\\' followed by a character not in the escape table — e.g. \x41, \u0041 (unsupported form), a trailing backslash before the closing quote, or a Windows path like 'C:\Users\bob' inserted unescaped.
Common situations: Inserting file paths, regexes, or JSON blobs into mutations without escaping; generating queries by string concatenation; copying N-Quads with non-standard escapes.
Related errors
- String should start with quote.
- Unexpected end of input.
- Unexpected end of IRI
- Unexpected character %q while parsing IRI
- line %d column %d:
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/89e24413fa9433cc.
Report an issue: GitHub.