forked from cucumber/gherkin-javascript
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrors.ts
60 lines (51 loc) · 1.79 KB
/
Errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { messages } from '@cucumber/messages'
import createLocation from './cli/createLocation'
export class GherkinException extends Error {
public errors: Error[]
public location: messages.ILocation
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-ignore
this.__proto__ = actualProto
}
}
protected static _create(message: string, location?: messages.ILocation) {
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 this(`(${line}:${column}): ${message}`)
err.location = createLocation({ 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 this(message)
err.errors = errors
return err
}
}
export class AstBuilderException extends GherkinException {
public static create(message: string, location: messages.ILocation) {
return this._create(message, location)
}
}
export class NoSuchLanguageException extends GherkinException {
public static create(language: string, location?: messages.ILocation) {
const message = 'Language not supported: ' + language
return this._create(message, location)
}
}