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

  1. Double the backslash (write \\\\ in source to mean a literal backslash) or remove the escape
  2. Use only supported escape characters (\\", \\\\, \\n, \\t, etc. — see IsEscChar) inside string literals
  3. When embedding JSON, use Dgraph's JSON mutation format instead of hand-escaped N-Quads
  4. 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

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


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/89e24413fa9433cc. Report an issue: GitHub.