YamlSyntaxError

import { YamlSyntaxError } from "jsr:@ende/yaml";

class

class YamlSyntaxError extends SyntaxError

Error thrown when YAML parsing fails.

Subclass of SyntaxError, so existing instanceof SyntaxError checks keep working. Carries structured position information so consumers do not have to parse the message.

Example: Usage

import { parse, YamlSyntaxError } from "@ende/yaml";
import { assertInstanceOf } from "@ende/assert";

try {
  parse(`"`);
} catch (error) {
  assertInstanceOf(error, YamlSyntaxError);
  assertInstanceOf(error, SyntaxError);
}

Constructors

constructor

new YamlSyntaxError(message: string, position: YamlPosition, snippet?: string)

Constructs a new YamlSyntaxError.

The constructed message is ${message} at line ${line}, column ${column}, with :\n${snippet} appended when a snippet is provided.

message: string

The error message describing the syntax issue.

position: YamlPosition

The position in the YAML source where the error occurred.

snippet?: string

Optional formatted snippet of the source around the error.

Properties

line

readonly line: number

The line number where the error occurred (1-indexed).

Example: Usage

import { YamlSyntaxError } from "@ende/yaml";
import { assertEquals } from "@ende/assert";

const error = new YamlSyntaxError("Test", { line: 5, column: 10, offset: 50 });
assertEquals(error.line, 5);

column

readonly column: number

The column number where the error occurred (1-indexed).

Example: Usage

import { YamlSyntaxError } from "@ende/yaml";
import { assertEquals } from "@ende/assert";

const error = new YamlSyntaxError("Test", { line: 5, column: 10, offset: 50 });
assertEquals(error.column, 10);

offset

readonly offset: number

The character offset where the error occurred (0-indexed).

Example: Usage

import { YamlSyntaxError } from "@ende/yaml";
import { assertEquals } from "@ende/assert";

const error = new YamlSyntaxError("Test", { line: 5, column: 10, offset: 50 });
assertEquals(error.offset, 50);

snippet

readonly snippet?: string

A formatted snippet of the source around the error, including a caret line indicating the column. Undefined when the source is empty.

Example: Usage

import { YamlSyntaxError } from "@ende/yaml";
import { assertEquals } from "@ende/assert";

const snippet = "    foo: bar\n         ^";
const error = new YamlSyntaxError(
  "Test",
  { line: 1, column: 6, offset: 5 },
  snippet,
);
assertEquals(error.snippet, snippet);

Source