Skip to content

Commit 35c3991

Browse files
committed
fix: Capture block parsing errors in the promise.
Errors were being thrown after block parsing but in some cases, weren't getting captured as a rejected promise within the block factory. To fix this, I refactored a complex promise chain into an async function which cleaned up the code and also ensured that errors thrown within the async function would get converted into a rejected promise.
1 parent 2bcc249 commit 35c3991

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

packages/@css-blocks/core/src/BlockParser/BlockFactory.ts

+33-32
Original file line numberDiff line numberDiff line change
@@ -135,38 +135,39 @@ export class BlockFactory {
135135
return this._getBlockPromise(identifier);
136136
}
137137

138-
async _getBlockPromise(identifier: FileIdentifier): Promise<Block> {
139-
140-
return this.promises[identifier] = this.importer.import(identifier, this.configuration)
141-
.then(file => this._importAndPreprocessBlock(file))
142-
.then(block => {
143-
debug(`Finalizing Block object for "${block.identifier}"`);
144-
145-
// last check to make sure we don't return a new instance
146-
if (this.blocks[block.identifier]) {
147-
return this.blocks[block.identifier];
148-
}
149-
150-
// Ensure this block name is unique.
151-
block.setName(this.getUniqueBlockName(block.name));
152-
153-
// if the block has any errors, surface them here
154-
this._surfaceBlockErrors(block);
155-
156-
return this.blocks[block.identifier] = block;
157-
})
158-
.catch((error) => {
159-
if (this.preprocessQueue.activeJobCount > 0) {
160-
debug(`Block error. Currently there are ${this.preprocessQueue.activeJobCount} preprocessing jobs. waiting.`);
161-
return this.preprocessQueue.drain().then(() => {
162-
debug(`Drain complete. Raising error.`);
163-
throw error;
164-
});
165-
} else {
166-
debug(`Block error. There are no preprocessing jobs. raising.`);
167-
throw error;
168-
}
169-
});
138+
_getBlockPromise(identifier: FileIdentifier): Promise<Block> {
139+
return this.promises[identifier] = this._getBlockPromiseAsync(identifier);
140+
}
141+
142+
async _getBlockPromiseAsync(identifier: FileIdentifier): Promise<Block> {
143+
try {
144+
let file = await this.importer.import(identifier, this.configuration);
145+
let block = await this._importAndPreprocessBlock(file);
146+
debug(`Finalizing Block object for "${block.identifier}"`);
147+
148+
// last check to make sure we don't return a new instance
149+
if (this.blocks[block.identifier]) {
150+
return this.blocks[block.identifier];
151+
}
152+
153+
// Ensure this block name is unique.
154+
block.setName(this.getUniqueBlockName(block.name));
155+
156+
// if the block has any errors, surface them here unless we're in fault tolerant mode.
157+
this._surfaceBlockErrors(block);
158+
this.blocks[block.identifier] = block;
159+
return block;
160+
} catch (error) {
161+
if (this.preprocessQueue.activeJobCount > 0) {
162+
debug(`Block error. Currently there are ${this.preprocessQueue.activeJobCount} preprocessing jobs. waiting.`);
163+
await this.preprocessQueue.drain();
164+
debug(`Drain complete. Raising error.`);
165+
throw error;
166+
} else {
167+
debug(`Block error. There are no preprocessing jobs. raising.`);
168+
throw error;
169+
}
170+
}
170171
}
171172

172173
/**

0 commit comments

Comments
 (0)