From 7e6cbe046e77aa3417eab208bc47088d74dc0520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dubiel?= Date: Sat, 1 Jul 2023 15:52:48 +0200 Subject: [PATCH] fix: Better error on input type missmatch (fix #459) --- src/errors.ts | 9 +++++++++ src/public-api.ts | 10 +++++++++- tests/doc/parse.ts | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/errors.ts b/src/errors.ts index 88653643..6e6dfc91 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -25,6 +25,15 @@ export type ErrorCode = export type LinePos = { line: number; col: number } +export class YAMLInputError extends Error { + constructor(message: string) { + super(); + this.name = 'YAMLInputError' + this.message = message + } +} + + export class YAMLError extends Error { name: 'YAMLParseError' | 'YAMLWarning' code: ErrorCode diff --git a/src/public-api.ts b/src/public-api.ts index 86680ea4..cf70fb64 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -1,7 +1,7 @@ import { Composer } from './compose/composer.js' import type { Reviver } from './doc/applyReviver.js' import { Document, Replacer } from './doc/Document.js' -import { prettifyError, YAMLParseError } from './errors.js' +import { prettifyError, YAMLParseError, YAMLInputError } from './errors.js' import { warn } from './log.js' import type { Node, ParsedNode } from './nodes/Node.js' import type { @@ -50,6 +50,10 @@ export function parseAllDocuments< : Document > | EmptyStream { + const inputType = typeof source; + if(inputType !== "string"){ + throw new YAMLInputError(`Expected string, got ${inputType}`) + } const { lineCounter, prettyErrors } = parseOptions(options) const parser = new Parser(lineCounter?.addNewLine) const composer = new Composer(options) @@ -82,6 +86,10 @@ export function parseDocument< ): Contents extends ParsedNode ? Document.Parsed : Document { + const inputType = typeof source; + if(inputType !== "string"){ + throw new YAMLInputError(`Expected string, got ${inputType}`) + } const { lineCounter, prettyErrors } = parseOptions(options) const parser = new Parser(lineCounter?.addNewLine) const composer = new Composer(options) diff --git a/tests/doc/parse.ts b/tests/doc/parse.ts index a40ac414..5cf1c9d8 100644 --- a/tests/doc/parse.ts +++ b/tests/doc/parse.ts @@ -2,6 +2,7 @@ import { readFileSync } from 'fs' import { resolve } from 'path' import * as YAML from 'yaml' import { source } from '../_utils' +import {YAMLInputError} from "../../src/errors"; describe('scalars', () => { test('empty block scalar at end of document', () => { @@ -819,3 +820,20 @@ describe('CRLF line endings', () => { expect(res).toBe('foo bar') }) }) + +describe('Errors on non string inputs', () => { + test('rise in parseDocument', () => { + const scenario = () => { + const input = Buffer.alloc(8) + YAML.parseDocument(input) + } + expect(scenario).toThrow(YAMLInputError) + }) + test('rise in parseAllDocuments', () => { + const scenario = () => { + const input = Buffer.alloc(8) + YAML.parseAllDocuments(input) + } + expect(scenario).toThrow(YAMLInputError) + }) +})