Skip to content

Commit 96fdd29

Browse files
committed
fix: Only raise MultipleCssBlockErrors if there's more than one.
1 parent c03c3ca commit 96fdd29

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ export class Block
199199
* @returns true if the block is valid else throws the errors on the block
200200
*/
201201
assertValid(): Block {
202-
if (this._blockErrors.length) {
202+
if (this._blockErrors.length > 1) {
203203
throw new MultipleCssBlockErrors(this._blockErrors);
204+
} else if (this._blockErrors.length === 1) {
205+
throw this._blockErrors[0];
204206
}
205207
return this;
206208
}

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,30 @@ export class BlockPathError extends CssBlockError {
133133
* clear errors
134134
*/
135135
export class MultipleCssBlockErrors extends CssBlockError {
136-
static prefix = "MultipleCssBlocksError";
136+
static prefix = "MultipleCssBlockErrors";
137137
private _errors: CssBlockError[] = [];
138138

139139
constructor(errors: CssBlockError[], location?: ErrorLocation, details?: string) {
140140
super(MultipleCssBlockErrors.prefix, location);
141141
for (let err of errors) {
142142
if (err instanceof MultipleCssBlockErrors) {
143-
// flatten all MultpleCssBlockErrors
144-
// This normally happens if there is a transtive error
143+
// flatten all MultipleCssBlockErrors
144+
// This normally happens if there is a transitive error
145145
for (let e of err.errors) {
146146
this._errors.push(e);
147147
}
148148
} else {
149149
this._errors.push(err);
150150
}
151151
}
152-
if (details) { this.message += `\n${details}`; }
152+
if (!details) {
153+
details = ":";
154+
let i = 0;
155+
for (let err of this._errors) {
156+
details += `\n\t${++i}. ${err}`;
157+
}
158+
}
159+
this.message += details;
153160
}
154161

155162
add(error: CssBlockError) {

packages/@css-blocks/core/test/util/assertError.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert } from "chai";
22
import { postcss } from "opticss";
33

4-
import { MultipleCssBlockErrors } from "../../src";
4+
import { CssBlockError, MultipleCssBlockErrors } from "../../src";
55
import cssBlocks from ".././util/postcss-helper";
66

77
export interface ErrorTypeMessage {
@@ -39,12 +39,16 @@ export function assertMultipleErrors(errors: ErrorTypeMessage[], promise: postcs
3939
(multipleErrorsError: MultipleCssBlockErrors) => assertMultipleErrorsWithoutPromise(multipleErrorsError, errors));
4040
}
4141

42-
export function assertMultipleErrorsWithoutPromise(multipleErrorsError: MultipleCssBlockErrors, errors: ErrorTypeMessage[]) {
43-
if (multipleErrorsError.errors.length !== errors.length) {
44-
assert(false, "The number of errors thrown and expected does not match");
42+
export function assertMultipleErrorsWithoutPromise(mainError: CssBlockError, errors: ErrorTypeMessage[]) {
43+
if (mainError instanceof MultipleCssBlockErrors) {
44+
assert.equal(mainError.errors.length, errors.length, "The number of errors thrown and expected does not match");
45+
errors.forEach((error, idx) => {
46+
assert(mainError.errors[idx] instanceof error.type, "Error raised was not of the type expected.");
47+
assert.deepEqual(mainError.errors[idx].message.split(error.type.prefix + ":")[1].trim(), error.message);
48+
});
49+
} else {
50+
assert.equal(1, errors.length, "The number of errors thrown and expected does not match");
51+
assert(mainError instanceof errors[0].type, "Error raised was not of the type expected.");
52+
assert.deepEqual(mainError.message.split(errors[0].type.prefix + ":")[1].trim(), errors[0].message);
4553
}
46-
return errors.forEach((error, idx) => {
47-
assert(error.type.name, typeof multipleErrorsError.errors[idx]);
48-
assert.deepEqual(multipleErrorsError.errors[idx].message.split(error.type.prefix + ":")[1].trim(), error.message);
49-
});
5054
}

0 commit comments

Comments
 (0)