Skip to content

Commit 14c1d31

Browse files
ramithachriseppstein
authored andcommitted
feat: Adding a new class of errors - MultipleCssBlockErrors.
1 parent a5f96f6 commit 14c1d31

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/@css-blocks/core/src/BlockTree/Block.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { isAttributeNode, isClassNode } from "../BlockParser";
1414
import { isRootNode, toAttrToken } from "../BlockParser";
1515
import { BlockPath, CLASS_NAME_IDENT, DEFAULT_EXPORT, ROOT_CLASS } from "../BlockSyntax";
1616
import { ResolvedConfiguration } from "../configuration";
17-
import { CssBlockError, InvalidBlockSyntax } from "../errors";
17+
import { CssBlockError, InvalidBlockSyntax, MultipleCssBlockErrors } from "../errors";
1818
import { FileIdentifier } from "../importing";
1919
import { SourceFile, SourceRange } from "../SourceLocation";
2020

@@ -51,6 +51,7 @@ export class Block
5151
private _blockExportReverseLookup: Map<Block, string> = new Map();
5252
private _identifier: FileIdentifier;
5353
private _implements: Block[] = [];
54+
private _blockErrors: CssBlockError[] = [];
5455
private hasHadNameReset = false;
5556

5657
public readonly guid: string;
@@ -185,6 +186,25 @@ export class Block
185186
return attr || klass || undefined;
186187
}
187188

189+
/**
190+
* Stores a block error along with the block
191+
* @param error CssBlockError that is added to the block
192+
*/
193+
addError(error: CssBlockError) {
194+
this._blockErrors.push(error);
195+
}
196+
197+
/**
198+
* Checks for errors on the block
199+
* @returns true if the block is valid else throws the errors on the block
200+
*/
201+
assertValid(): Boolean {
202+
if (this._blockErrors.length) {
203+
throw new MultipleCssBlockErrors(this._blockErrors);
204+
}
205+
return true;
206+
}
207+
188208
/**
189209
* Add an absolute, normalized path as a compilation dependency. This is used
190210
* to invalidate caches and trigger watchers when those files change.

packages/@css-blocks/core/src/errors.ts

+18
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,21 @@ export class BlockPathError extends CssBlockError {
127127
if (details) { this.message += `\n${details}`; }
128128
}
129129
}
130+
131+
/**
132+
* Acts as a collection of CssBlockErrors along with utility methods to add and
133+
* clear errors
134+
*/
135+
export class MultipleCssBlockErrors extends Error {
136+
private _errors: CssBlockError[] = [];
137+
constructor(errors: CssBlockError[]) {
138+
super();
139+
this._errors = errors;
140+
}
141+
add(error:CssBlockError) {
142+
this._errors.push(error);
143+
}
144+
clear() {
145+
this._errors = [];
146+
}
147+
}

0 commit comments

Comments
 (0)