Repository navigation
refactor: Improve error log message when manifest.json is empty #1667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+268
−20
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
24d4272
refactor: Handle empty JSON (manifests) in builder
d3xter666 7f08715
refactor: Handle empty JSON (manifests) in project
d3xter666 5c4d283
refactor: Simplify parseJSON util
d3xter666 85ac842
refactor: Improve parseJSON util to also handle malformed JSON
d3xter666 fca52ec
refactor: Displayed messages
d3xter666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Parses JSON content, attaching the file path to any parse failure so the | ||
| * root cause (empty file vs. syntax error) is always identifiable. | ||
| * | ||
| * An empty JSON object (<code>{}</code>) is valid and parsed normally; only content-less | ||
| * files (empty or whitespace-only) throw <code>ERR_EMPTY_JSON</code>. Any other parse | ||
| * failure is re-thrown as <code>ERR_INVALID_JSON</code> with the file path prefixed. | ||
| * | ||
| * @param {string} content Raw file content | ||
| * @param {string} filePath Path of the file the content was read from, used in the error message | ||
| * @returns {object} Parsed JSON | ||
| * @throws {SyntaxError} with code "ERR_EMPTY_JSON" if content is empty/whitespace-only | ||
| * @throws {SyntaxError} with code "ERR_INVALID_JSON" if content is malformed JSON | ||
| */ | ||
| export default function parseJSON(content, filePath) { | ||
| try { | ||
| return JSON.parse(content); | ||
| } catch (err) { | ||
| if (typeof content === "string" && content.trim() === "") { | ||
| const error = new SyntaxError(`The file "${filePath}" is empty.`); | ||
| error.code = "ERR_EMPTY_JSON"; | ||
| throw error; | ||
| } | ||
| const error = new SyntaxError(`Failed to parse "${filePath}" as JSON: ${err.message}`); | ||
| error.code = "ERR_INVALID_JSON"; | ||
| throw error; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import test from "ava"; | ||
| import parseJSON from "../../../lib/utils/json.js"; | ||
|
|
||
| test("parseJSON: Parses valid JSON content", (t) => { | ||
| const result = parseJSON(`{"foo": "bar"}`, "/some/file.json"); | ||
| t.deepEqual(result, {foo: "bar"}, "Returned the parsed object"); | ||
| }); | ||
|
|
||
| test("parseJSON: Parses an empty JSON object", (t) => { | ||
| const result = parseJSON("{}", "/some/file.json"); | ||
| t.deepEqual(result, {}, "An empty JSON object is valid and parsed correctly"); | ||
| }); | ||
|
|
||
| test("parseJSON: Throws for empty content", (t) => { | ||
| const error = t.throws(() => parseJSON("", "webapp/manifest.json")); | ||
| t.true(error instanceof SyntaxError, "Threw a SyntaxError"); | ||
| t.is(error.message, `The file "webapp/manifest.json" is empty.`, | ||
| "Threw with the expected error message"); | ||
| t.is(error.code, "ERR_EMPTY_JSON", "Threw with the expected error code"); | ||
| }); | ||
|
|
||
| test("parseJSON: Throws for whitespace-only content", (t) => { | ||
| const error = t.throws(() => parseJSON(" \n\t ", "webapp/manifest.json")); | ||
| t.true(error instanceof SyntaxError, "Threw a SyntaxError"); | ||
| t.is(error.message, `The file "webapp/manifest.json" is empty.`, | ||
| "Threw with the expected error message"); | ||
| t.is(error.code, "ERR_EMPTY_JSON", "Threw with the expected error code"); | ||
| }); | ||
|
|
||
| test("parseJSON: Throws ERR_INVALID_JSON for malformed JSON", (t) => { | ||
| const error = t.throws(() => parseJSON("{bad json", "/some/file.json")); | ||
| t.true(error instanceof SyntaxError, "Threw a SyntaxError"); | ||
| t.is(error.code, "ERR_INVALID_JSON", "Threw with the expected error code"); | ||
| t.true(error.message.startsWith(`Failed to parse "/some/file.json" as JSON:`), | ||
| "Error message includes the file path and root cause"); | ||
| }); | ||
|
|
||
| test("parseJSON: null content is treated as valid JSON returning null", (t) => { | ||
| // JSON.parse(null) succeeds — null is valid JSON — so no error is thrown | ||
| const result = parseJSON(null, "/some/file.json"); | ||
| t.is(result, null, "JSON.parse(null) returns null without throwing"); | ||
| }); | ||
|
|
||
| test("parseJSON: Throws ERR_INVALID_JSON for undefined content", (t) => { | ||
| const error = t.throws(() => parseJSON(undefined, "/some/file.json")); | ||
| t.is(error.code, "ERR_INVALID_JSON", "Threw with the expected error code"); | ||
| }); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Parses JSON content, attaching the file path to any parse failure so the | ||
| * root cause (empty file vs. syntax error) is always identifiable. | ||
| * | ||
| * An empty JSON object (<code>{}</code>) is valid and parsed normally; only content-less | ||
| * files (empty or whitespace-only) throw <code>ERR_EMPTY_JSON</code>. Any other parse | ||
| * failure is re-thrown as <code>ERR_INVALID_JSON</code> with the file path prefixed. | ||
| * | ||
| * @param {string} content Raw file content | ||
| * @param {string} filePath Path of the file the content was read from, used in the error message | ||
| * @returns {object} Parsed JSON | ||
| * @throws {SyntaxError} with code "ERR_EMPTY_JSON" if content is empty/whitespace-only | ||
| * @throws {SyntaxError} with code "ERR_INVALID_JSON" if content is malformed JSON | ||
| */ | ||
| export default function parseJSON(content, filePath) { | ||
| try { | ||
| return JSON.parse(content); | ||
| } catch (err) { | ||
| if (typeof content === "string" && content.trim() === "") { | ||
| const error = new SyntaxError(`The file "${filePath}" is empty.`); | ||
| error.code = "ERR_EMPTY_JSON"; | ||
| throw error; | ||
| } | ||
| const error = new SyntaxError(`Failed to parse "${filePath}" as JSON: ${err.message}`); | ||
| error.code = "ERR_INVALID_JSON"; | ||
| throw error; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.