Skip to content

Commit 5b22e6e

Browse files
committed
Add ScopeError stub
1 parent 4307eb5 commit 5b22e6e

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

resolver/src/bundle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import {Scope} from "./scope";
33
import {Value, NoneValue} from "./value";
44
import {Message} from "./message";
55
import {hello, exclamation, select} from "./fixtures";
6+
import {ScopeError} from "./error";
67

78
export interface Formatted {
89
readonly value: string | null;
9-
readonly errors: Array<string>;
10+
readonly errors: Array<ScopeError>;
1011
}
1112

1213
export class Bundle {

resolver/src/error.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export enum ErrorKind {
2+
UnknownVariable,
3+
UnknownMessage,
4+
MissingValue,
5+
}
6+
7+
export class ScopeError extends Error {
8+
public kind: ErrorKind;
9+
public arg: string;
10+
11+
constructor(kind: ErrorKind, arg: string) {
12+
super();
13+
this.kind = kind;
14+
this.arg = arg;
15+
}
16+
}

resolver/src/message.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ast from "./ast";
22
import {Scope} from "./scope";
33
import {Failure, Result} from "./result";
44
import {Value, StringValue} from "./value";
5+
import {ScopeError, ErrorKind} from "./error";
56

67
export class Message {
78
private readonly id: string;
@@ -16,7 +17,7 @@ export class Message {
1617
if (this.value !== null) {
1718
return scope.resolvePattern(this.value);
1819
} else {
19-
scope.errors.push(`Message ${this.id} has a null value.`);
20+
scope.errors.push(new ScopeError(ErrorKind.MissingValue, this.id));
2021
return new Failure(new StringValue(this.id));
2122
}
2223
}

resolver/src/scope.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import * as ast from "./ast";
22
import {Message} from "./message";
33
import {Value, StringValue} from "./value";
44
import {Result, Success, Failure} from "./result";
5+
import {ScopeError, ErrorKind} from "./error";
56

67
export class Scope {
78
private readonly messages: Map<string, Message>;
89
private readonly variables: Map<string, Value>;
9-
public errors: Array<string>;
10+
public errors: Array<ScopeError>;
1011

1112
constructor(messages: Map<string, Message>, variables: Map<string, Value>) {
1213
this.messages = messages;
@@ -32,7 +33,7 @@ export class Scope {
3233
if (value !== undefined) {
3334
return new Success(value);
3435
} else {
35-
this.errors.push(`Unknown variable: $${node.id.name}.`);
36+
this.errors.push(new ScopeError(ErrorKind.UnknownVariable, node.id.name));
3637
return new Failure(new StringValue(`$${node.id.name}`));
3738
}
3839
}
@@ -42,7 +43,7 @@ export class Scope {
4243
if (message !== undefined) {
4344
return message.resolveValue(this);
4445
} else {
45-
this.errors.push(`Unknown message: ${node.id.name}.`);
46+
this.errors.push(new ScopeError(ErrorKind.UnknownMessage, node.id.name));
4647
return new Failure(new StringValue(`${node.id.name}`));
4748
}
4849
}

0 commit comments

Comments
 (0)