{"record":{"id":"5ecf44d70bb8e939","repo":"dgraph-io/dgraph","slug":"unexpected-character-q-while-parsing-iri","errorCode":null,"errorMessage":"Unexpected character %q while parsing IRI","messagePattern":"Unexpected character %q while parsing IRI","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lex/iri.go","lineNumber":22,"sourceCode":" */\n\npackage lex\n\nimport (\n\t\"github.com/pkg/errors\"\n)\n\n// IRIRef emits an IRIREF or returns an error if the input is invalid.\nfunc IRIRef(l *Lexer, styp ItemType) error {\n\tl.Ignore() // ignore '<'\n\tl.AcceptRunRec(isIRIRefChar)\n\tl.Emit(styp) // will emit without '<' and '>'\n\tr := l.Next()\n\tif r == EOF {\n\t\treturn errors.New(\"Unexpected end of IRI\")\n\t}\n\tif r != '>' {\n\t\treturn errors.Errorf(\"Unexpected character %q while parsing IRI\", r)\n\t}\n\tl.Ignore() // ignore '>'\n\treturn nil\n}\n\n// isIRIRefChar returns whether the rune is a character allowed in an IRIRef.\n// IRIREF ::= '<' ([^#x00-#x20<>\"{}|^`\\] | UCHAR)* '>'\nfunc isIRIRefChar(r rune, l *Lexer) bool {\n\tif r <= 32 { // no chars b/w 0x00 to 0x20 inclusive\n\t\treturn false\n\t}\n\tswitch r {\n\tcase '<', '>', '\"', '{', '}', '|', '^', '`':\n\t\treturn false\n\tcase '\\\\':\n\t\tr2 := l.Next()\n\t\tif r2 != 'u' && r2 != 'U' {\n\t\t\tl.Backup()","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/lex/iri.go#L4-L40","documentation":"After reading an IRI's characters, IRIRef requires the terminator `>`. If the next rune is anything else (not EOF, not `>`), the lexer reports the unexpected character with %q formatting, indicating malformed IRI syntax in the input stream.","triggerScenarios":"Input like `<http://example.com/x http://...` where a space, newline, or other character appears where the closing `>` should be; nested or unescaped `<>` characters inside an IRI.","commonSituations":"Unescaped characters in generated IRIs, copy-pasted data with smart quotes or stray characters, IRIs split across lines without closing, or malformed N-Quads from a producer bug.","solutions":["Close the IRI with `>` before the offending character","Escape or remove illegal characters inside IRIs per the IRIREF grammar","Find and fix the producer emitting malformed IRIs","Pre-validate lines of the RDF document with a regex like <[^<>()\"]*>"],"exampleFix":"// before\n<http://example.com/s> <http://example.com/p>.\n// after\n<http://example.com/s> <http://example.com/p> .","handlingStrategy":"validation","validationCode":"iriRe := regexp.MustCompile(`^<[^<>\"{}|^` + \"`\" + `\\\\\\s]*>$`)\nif !iriRe.MatchString(tok) {\n  return fmt.Errorf(\"invalid IRI syntax: %s\", tok)\n}","typeGuard":null,"tryCatchPattern":"if err := lex.IRIRef(l, item); err != nil {\n  var synErr *SyntaxError\n  if strings.Contains(err.Error(), \"Unexpected character\") {\n    return fmt.Errorf(\"malformed IRI near offset %d: %w\", l.Pos(), err)\n  }\n  return err\n}","preventionTips":["Sanitize generated IRIs to the IRIREF character set","Reject unescaped < > and control characters inside IRIs","Test serialization output of upstream RDF producers"],"tags":["lexer","rdf","parsing","syntax-error"],"backgroundTag":"invalid-iri-syntax","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}