dgraph-io/dgraph · error

item.Val

Error message

item.Val

What it means

Lexer.ValidateResult walks every lexed item and returns errors.New(item.Val) when it encounters an ItemError token. The message is the raw lexer error text stored in the item, so any lexing failure (illegal character, unterminated string, bad escape) surfaces here verbatim. It is used by all query/mutation entry points (ParseRDF, ParseDQL, ParseWithNeedVars, ParseMutation) to fail fast before parsing.

Source

Thrown at lex/lexer.go:189

func (l *Lexer) Reset(input string) {
	// Pick the slices so we can reuse it.
	item := l.items
	widthStack := l.widthStack

	*l = Lexer{}
	l.Input = input
	l.items = item[:0]
	l.widthStack = widthStack[:0]
	l.Line = 1
}

// ValidateResult verifies whether the entire input can be lexed without errors.
func (l *Lexer) ValidateResult() error {
	it := l.NewIterator()
	for it.Next() {
		item := it.Item()
		if item.Typ == ItemError {
			return errors.New(item.Val)
		}
	}
	return nil
}

// Run  executes the given StateFn on the lexer and returns the lexer.
func (l *Lexer) Run(f StateFn) *Lexer {
	for state := f; state != nil; {
		// The following statement is useful for debugging.
		// fmt.Printf("Func: %v\n", runtime.FuncForPC(reflect.ValueOf(state).Pointer()).Name())
		state = state(l)
	}
	return l
}

// Errorf returns the error state function.
func (l *Lexer) Errorf(format string, args ...interface{}) StateFn {
	l.items = append(l.items, Item{

View on GitHub (pinned to 759e242be6)

Solutions

  1. Read item.Val in the error — it states the exact lexical problem — and fix that character/construct in the input
  2. Always call ValidateResult (or the Parse entry point) and check the error before continuing with parsed output
  3. Escape quotes and backslashes inside string literals properly
  4. If parsing RDF, verify each N-Quad line terminates with ' .' and literals are quoted

Example fix

// before
err := l.ValidateResult() // "unterminated quoted string"
// after: fix input then guard
if err := l.ValidateResult(); err != nil {
    return fmt.Errorf("invalid query: %v", err)
}
Defensive patterns

Strategy: validation

Validate before calling

l := lex.NewLexer(input)
if err := l.ValidateResult(); err != nil {
    return fmt.Errorf("lexical error in input: %v", err)
}
// proceed to parse only if valid

Try / catch

if err := l.ValidateResult(); err != nil {
    return fmt.Errorf("invalid input: %v", err) // error text is the lexer's item.Val
}

Prevention

When it happens

Trigger: Calling ValidateResult (directly or via ParseDQL/ParseRDF/ParseMutation) on input containing a character or construct the lexer cannot tokenize, e.g. an unterminated quoted string, invalid escape, or stray symbol; the error message is whatever text was recorded in the ItemError item.

Common situations: Sending RDF N-Quads mutations with a bad literal, or DQL queries with unescaped quotes/braces; also seen when RDFN-Quad lines use characters outside what the lexer accepts.

Related errors


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