Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ptrumpis opened this issue Apr 4, 2023 · 5 comments
Open
Labels
bug Something isn't working

Comments

@ptrumpis
Copy link

ptrumpis commented Apr 4, 2023

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 ptrumpis added the bug Something isn't working label Apr 4, 2023
@eemeli
Copy link
Owner

eemeli commented Apr 4, 2023

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

@eemeli eemeli closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2023
@bambuchaAdm
Copy link

bambuchaAdm commented Jun 30, 2023

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

@eemeli
Copy link
Owner

eemeli commented Jun 30, 2023

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 eemeli reopened this Jun 30, 2023
@eemeli eemeli changed the title TypeError: this.buffer.substring is not a function Improve error for "TypeError: this.buffer.substring is not a function" Jun 30, 2023
bambuchaAdm added a commit to bambuchaAdm/yaml that referenced this issue Jul 1, 2023
bambuchaAdm added a commit to bambuchaAdm/yaml that referenced this issue Jul 1, 2023
@chenll7
Copy link

chenll7 commented Apr 27, 2024

image

@chenll7
Copy link

chenll7 commented Apr 27, 2024

image

2.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants