Skip to content

Commit 0094f5e

Browse files
committed
Gracefully handle case where empty field is slugified
1 parent 2ceafcb commit 0094f5e

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

index.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ const { getSetupForData, getSetupForPage } = require("./lib/setup");
55

66
module.exports.name = pkg.name;
77

8-
module.exports.transform = ({ data, log, options }) => {
8+
module.exports.transform = ({ data, debug, log, options }) => {
99
if (typeof options.writeFile !== "function") {
1010
return data;
1111
}
1212

1313
const utils = {
14-
slugify
14+
slugify: input => {
15+
if (typeof input !== "string" || input.trim().length === 0) {
16+
throw new Error("ERROR_FAILED_SLUGIFY");
17+
}
18+
19+
return slugify(input);
20+
}
1521
};
1622
const files = data.objects.reduce((result, object) => {
1723
let processedObject = object;
@@ -33,11 +39,33 @@ module.exports.transform = ({ data, log, options }) => {
3339
}, {});
3440
}
3541

36-
const writer = options.writeFile(processedObject, utils);
42+
try {
43+
const writer = options.writeFile(processedObject, utils);
44+
45+
if (!writer) return result;
46+
47+
return result.concat(writer);
48+
} catch (error) {
49+
const objectDetails =
50+
object.__metadata && object.__metadata.id
51+
? ` (Object ID: ${object.__metadata.id})`
52+
: "";
3753

38-
if (!writer) return result;
54+
if (error.message === "ERROR_FAILED_SLUGIFY") {
55+
log(
56+
`Could not write object to disk because \`slugify()\` was used on an empty field.${objectDetails}`,
57+
"fail"
58+
);
59+
60+
debug(error);
61+
} else {
62+
log(`Could not write object to disk.${objectDetails} `, "fail");
3963

40-
return result.concat(writer);
64+
debug(error);
65+
}
66+
67+
return result;
68+
}
4169
}, []);
4270

4371
return {

0 commit comments

Comments
 (0)