Improve error for "TypeError: this.buffer.substring is not a function"
Describe the bug
TypeError: this.buffer.substring is not a function
at Lexer.getLine (/usr/src/app/node_modules/yaml/dist/parse/lexer.js:208:28)
at Lexer.parseStream (/usr/src/app/node_modules/yaml/dist/parse/lexer.js:244:25)
at parseStream.next (<anonymous>)
at Lexer.parseNext (/usr/src/app/node_modules/yaml/dist/parse/lexer.js:226:36)
at parseNext.next (<anonymous>)
at Lexer.lex (/usr/src/app/node_modules/yaml/dist/parse/lexer.js:160:32)
at lex.next (<anonymous>)
at Parser.parse (/usr/src/app/node_modules/yaml/dist/parse/parser.js:159:20)
at parse.next (<anonymous>)
at Composer.compose (/usr/src/app/node_modules/yaml/dist/compose/composer.js:130:20)
To Reproduce test.js
import YAML from 'yaml';
import * as fs from "fs/promises";
try {
const file = await fs.readFile("config.yml");
let config = YAML.parse(file);
} catch (e) {
console.error(e);
}
config.yml
test:
data:
- foo
- bar
Expected behaviour I expected config.yml to be loaded and parsed to a JS object without error.
Versions:
- Environment: Node.js 16 (Docker Image)
-
yaml: 2.2.1
Additional context Add any other context about the problem here.
Without a second encoding argument readFile() returns a Buffer, not a string.
@eemeli What do you think about adding guard for Buffers with better error message. I made this mistake today and only here found solution in this ticket. Having error like "expected string, got Buffer" immediately narrow down problems.
Sure, that makes sense. Feel free to submit a PR adding a better error to parseDocument() and parseAllDocuments(). Probably something simple that's just checking typeof source === 'string'. It'll probably need a new error code as well.
2.4.1
