UNPKG

@cucumber/gherkin

Version:
61 lines (51 loc) 1.89 kB
import type { Location } from '@cucumber/messages' export class GherkinException extends Error { public errors: Error[] public location: Location constructor(message: string) { super(message) const actualProto = new.target.prototype // https://stackoverflow.com/questions/41102060/typescript-extending-error-class if (Object.setPrototypeOf) { Object.setPrototypeOf(this, actualProto) } else { // @ts-expect-error // biome-ignore lint/suspicious/noProto: Object.setPrototypeOf is used if available this.__proto__ = actualProto } } protected static _create(message: string, location?: Location) { const column = location != null ? location.column || 0 : -1 const line = location != null ? location.line || 0 : -1 const m = `(${line}:${column}): ${message}` const err = new this(m) err.location = location return err } } export class ParserException extends GherkinException { public static create(message: string, line: number, column: number) { const err = new ParserException(`(${line}:${column}): ${message}`) err.location = { line, column } return err } } export class CompositeParserException extends GherkinException { public static create(errors: Error[]) { const message = `Parser errors:\n${errors.map((e) => e.message).join('\n')}` const err = new CompositeParserException(message) err.errors = errors return err } } export class AstBuilderException extends GherkinException { public static create(message: string, location: Location) { return AstBuilderException._create(message, location) } } export class NoSuchLanguageException extends GherkinException { public static create(language: string, location?: Location) { const message = `Language not supported: ${language}` return NoSuchLanguageException._create(message, location) } }