Skip to content

Commit f98be10

Browse files
refactor: handle errors (#544)
1 parent b971374 commit f98be10

File tree

1 file changed

+78
-30
lines changed

1 file changed

+78
-30
lines changed

src/index.js

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,19 @@ class CopyPlugin {
242242
`begin globbing "${pattern.glob}" with a context of "${pattern.context}"`
243243
);
244244

245-
const paths = await globby(pattern.glob, pattern.globOptions);
245+
let paths;
246+
247+
try {
248+
paths = await globby(pattern.glob, pattern.globOptions);
249+
} catch (error) {
250+
compilation.errors.push(error);
251+
252+
return;
253+
}
246254

247255
if (paths.length === 0) {
248256
if (pattern.noErrorOnMissing) {
249-
return Promise.resolve();
257+
return;
250258
}
251259

252260
const missingError = new Error(
@@ -257,7 +265,7 @@ class CopyPlugin {
257265

258266
compilation.errors.push(missingError);
259267

260-
return Promise.resolve();
268+
return;
261269
}
262270

263271
const filteredPaths = (
@@ -269,7 +277,15 @@ class CopyPlugin {
269277
}
270278

271279
if (pattern.filter) {
272-
const isFiltered = await pattern.filter(item.path);
280+
let isFiltered;
281+
282+
try {
283+
isFiltered = await pattern.filter(item.path);
284+
} catch (error) {
285+
compilation.errors.push(error);
286+
287+
return false;
288+
}
273289

274290
return isFiltered ? item : false;
275291
}
@@ -280,7 +296,7 @@ class CopyPlugin {
280296
).filter((item) => item);
281297

282298
if (filteredPaths.length === 0) {
283-
return Promise.resolve();
299+
return;
284300
}
285301

286302
const files = filteredPaths.map((item) => {
@@ -311,6 +327,7 @@ class CopyPlugin {
311327
return { absoluteFilename, sourceFilename, filename };
312328
});
313329

330+
// eslint-disable-next-line consistent-return
314331
return Promise.all(
315332
files.map(async (file) => {
316333
// If this came from a glob, add it to the file watchlist
@@ -323,13 +340,29 @@ class CopyPlugin {
323340
let source;
324341

325342
if (cache) {
326-
const cacheEntry = await cache.getPromise(file.sourceFilename, null);
343+
let cacheEntry;
344+
345+
try {
346+
cacheEntry = await cache.getPromise(file.sourceFilename, null);
347+
} catch (error) {
348+
compilation.errors.push(error);
349+
350+
return;
351+
}
327352

328353
if (cacheEntry) {
329-
const isValidSnapshot = await CopyPlugin.checkSnapshotValid(
330-
compilation,
331-
cacheEntry.snapshot
332-
);
354+
let isValidSnapshot;
355+
356+
try {
357+
isValidSnapshot = await CopyPlugin.checkSnapshotValid(
358+
compilation,
359+
cacheEntry.snapshot
360+
);
361+
} catch (error) {
362+
compilation.errors.push(error);
363+
364+
return;
365+
}
333366

334367
if (isValidSnapshot) {
335368
({ source } = cacheEntry);
@@ -374,17 +407,16 @@ class CopyPlugin {
374407
.digest('hex'),
375408
};
376409

377-
if (typeof pattern.cacheTransform.keys === 'function') {
378-
defaultCacheKeys = await pattern.cacheTransform.keys(
379-
defaultCacheKeys,
380-
file.absoluteFilename
381-
);
382-
} else {
383-
defaultCacheKeys = {
384-
...defaultCacheKeys,
385-
...pattern.cacheTransform.keys,
386-
};
387-
}
410+
defaultCacheKeys =
411+
typeof pattern.cacheTransform.keys === 'function'
412+
? await pattern.cacheTransform.keys(
413+
defaultCacheKeys,
414+
file.absoluteFilename
415+
)
416+
: {
417+
...defaultCacheKeys,
418+
...pattern.cacheTransform.keys,
419+
};
388420

389421
const cacheKeys = serialize(defaultCacheKeys);
390422

@@ -413,16 +445,32 @@ class CopyPlugin {
413445
source = new RawSource(data);
414446

415447
if (cache) {
416-
const snapshot = await CopyPlugin.createSnapshot(
417-
compilation,
418-
startTime,
419-
file.sourceFilename
420-
);
448+
let snapshot;
421449

422-
await cache.storePromise(file.sourceFilename, null, {
423-
source,
424-
snapshot,
425-
});
450+
try {
451+
snapshot = await CopyPlugin.createSnapshot(
452+
compilation,
453+
startTime,
454+
file.sourceFilename
455+
);
456+
} catch (error) {
457+
compilation.errors.push(error);
458+
459+
return;
460+
}
461+
462+
if (snapshot) {
463+
try {
464+
await cache.storePromise(file.sourceFilename, null, {
465+
source,
466+
snapshot,
467+
});
468+
} catch (error) {
469+
compilation.errors.push(error);
470+
471+
return;
472+
}
473+
}
426474
}
427475
}
428476

0 commit comments

Comments
 (0)