Skip to content

Commit

Permalink
refactor: handle errors (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 22, 2020
1 parent b971374 commit f98be10
Showing 1 changed file with 78 additions and 30 deletions.
108 changes: 78 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,19 @@ class CopyPlugin {
`begin globbing "${pattern.glob}" with a context of "${pattern.context}"`
);

const paths = await globby(pattern.glob, pattern.globOptions);
let paths;

try {
paths = await globby(pattern.glob, pattern.globOptions);
} catch (error) {
compilation.errors.push(error);

return;
}

if (paths.length === 0) {
if (pattern.noErrorOnMissing) {
return Promise.resolve();
return;
}

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

compilation.errors.push(missingError);

return Promise.resolve();
return;
}

const filteredPaths = (
Expand All @@ -269,7 +277,15 @@ class CopyPlugin {
}

if (pattern.filter) {
const isFiltered = await pattern.filter(item.path);
let isFiltered;

try {
isFiltered = await pattern.filter(item.path);
} catch (error) {
compilation.errors.push(error);

return false;
}

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

if (filteredPaths.length === 0) {
return Promise.resolve();
return;
}

const files = filteredPaths.map((item) => {
Expand Down Expand Up @@ -311,6 +327,7 @@ class CopyPlugin {
return { absoluteFilename, sourceFilename, filename };
});

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

if (cache) {
const cacheEntry = await cache.getPromise(file.sourceFilename, null);
let cacheEntry;

try {
cacheEntry = await cache.getPromise(file.sourceFilename, null);
} catch (error) {
compilation.errors.push(error);

return;
}

if (cacheEntry) {
const isValidSnapshot = await CopyPlugin.checkSnapshotValid(
compilation,
cacheEntry.snapshot
);
let isValidSnapshot;

try {
isValidSnapshot = await CopyPlugin.checkSnapshotValid(
compilation,
cacheEntry.snapshot
);
} catch (error) {
compilation.errors.push(error);

return;
}

if (isValidSnapshot) {
({ source } = cacheEntry);
Expand Down Expand Up @@ -374,17 +407,16 @@ class CopyPlugin {
.digest('hex'),
};

if (typeof pattern.cacheTransform.keys === 'function') {
defaultCacheKeys = await pattern.cacheTransform.keys(
defaultCacheKeys,
file.absoluteFilename
);
} else {
defaultCacheKeys = {
...defaultCacheKeys,
...pattern.cacheTransform.keys,
};
}
defaultCacheKeys =
typeof pattern.cacheTransform.keys === 'function'
? await pattern.cacheTransform.keys(
defaultCacheKeys,
file.absoluteFilename
)
: {
...defaultCacheKeys,
...pattern.cacheTransform.keys,
};

const cacheKeys = serialize(defaultCacheKeys);

Expand Down Expand Up @@ -413,16 +445,32 @@ class CopyPlugin {
source = new RawSource(data);

if (cache) {
const snapshot = await CopyPlugin.createSnapshot(
compilation,
startTime,
file.sourceFilename
);
let snapshot;

await cache.storePromise(file.sourceFilename, null, {
source,
snapshot,
});
try {
snapshot = await CopyPlugin.createSnapshot(
compilation,
startTime,
file.sourceFilename
);
} catch (error) {
compilation.errors.push(error);

return;
}

if (snapshot) {
try {
await cache.storePromise(file.sourceFilename, null, {
source,
snapshot,
});
} catch (error) {
compilation.errors.push(error);

return;
}
}
}
}

Expand Down

0 comments on commit f98be10

Please sign in to comment.