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

Better error on input type missmatch (fix #459) #484

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/public-api.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -50,6 +50,10 @@ export function parseAllDocuments<
: Document<Contents, Strict>
>
| 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)
Expand Down Expand Up @@ -82,6 +86,10 @@ export function parseDocument<
): Contents extends ParsedNode
? Document.Parsed<Contents, Strict>
: Document<Contents, Strict> {
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)
Expand Down
18 changes: 18 additions & 0 deletions tests/doc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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', () => {
Expand Down Expand Up @@ -819,3 +820,20 @@
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)

Check failure on line 828 in tests/doc/parse.ts

View workflow job for this annotation

GitHub Actions / typescript

Argument of type 'Buffer' is not assignable to parameter of type 'string'.
}
expect(scenario).toThrow(YAMLInputError)
})
test('rise in parseAllDocuments', () => {
const scenario = () => {
const input = Buffer.alloc(8)
YAML.parseAllDocuments(input)

Check failure on line 835 in tests/doc/parse.ts

View workflow job for this annotation

GitHub Actions / typescript

Argument of type 'Buffer' is not assignable to parameter of type 'string'.
}
expect(scenario).toThrow(YAMLInputError)
})
})
Loading