yaml icon indicating copy to clipboard operation
yaml copied to clipboard

Improve error for "TypeError: this.buffer.substring is not a function"

Open ptrumpis opened this issue 2 years ago • 5 comments

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.

ptrumpis avatar Apr 04 '23 16:04 ptrumpis

Without a second encoding argument readFile() returns a Buffer, not a string.

eemeli avatar Apr 04 '23 17:04 eemeli

@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.

bambuchaAdm avatar Jun 30 '23 08:06 bambuchaAdm

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.

eemeli avatar Jun 30 '23 11:06 eemeli

image

chenll7 avatar Apr 27 '24 13:04 chenll7

image

2.4.1

chenll7 avatar Apr 27 '24 13:04 chenll7