Skip to content

Conversation

@yCodeTech
Copy link
Owner

Fixes #14

The extension crashes on startup with the Cannot convert undefined or null to object error. This is caused by the Object.keys in Configuration::setLanguageConfigDefinitions method because the config JSON file failed to parse in the readJsonFile util function which made the config null.

When the "jsonc-parser" package encounters a problem parsing the JSON via its parse function, it just returns null instead of erroring.

The fix is to provide proper error handling, logging and showing a user error dialog when parse errors occur, directly from readJsonFile function. If errors occur, the error will be thrown from readJsonFile which will fail extension startup and crash, this is to provide easier debugging, instead of waiting for subsequent code that relies on the function from crashing the extension with an unrelated error.

If no errors occur but the JSON file still returns as null, then the readJsonFile function will return an empty object as a fallback.

Changes:

Logger Refactor:

  • Replaces the Logger class export with a singleton logger export in logger.ts, and updates all imports and usages to use this singleton instead of passing a Logger instance around. This simplifies the codebase, centralizes logging, and enables multiple files to use the logger on the same instance.

Configuration Initialization:

  • Updates the Configuration class and its instantiation to no longer require a logger parameter, reflecting the new singleton logger usage.

Improved JSON File Error Handling:

  • Enhances the readJsonFile utility in utils.ts to provide detailed error messages — including file, line, and column — when JSON parsing fails. It also notifies the user via a VS Code error message and offers to open the output channel for more details, then throws an error to halt execution.

Utility Function Enhancement:

  • Adds the constructJsonParseErrorMsg helper function to format JSON parse errors for improved debugging and user feedback.

To be able to log errors or debug info from files other than the configuration and extension, we need to ensure the `Logger` class has only one instance throughout, and be able to use globally (Singleton design pattern).

Added:

- Added a line to instantiate `Logger` from within the logger file and export that single instance.

Removed:

- Removed `export` keyword from `Logger` class.
- Removed the `Logger` instantiation from the extension file.
- Removed the private `Configuration::logger` property.
- Removed the `Configuration::constructor` method's `logger` param.

Changed:

- Changed the imports to the new lowercased `logger`, ie the exported instance of `Logger` instead of `Logger` itself.

- Changed all references to `this.logger` in `Configuration` class to just reference the new single `logger` instance by removing `this`.
Fixes #14

- Split up the reading of the file contents and the parsing of the JSON into 2 separate variables `fileContent` and `jsonContents` for ease in the `readJsonFile` util function.

- Added a null coalescing operator to the `jsonContents` variable to ensure the variable is always an empty object if the parsing function from `jsonc-parser` package returns `null`.

- Added a `jsonErrors` variable with a default of an empty array, and passed this variable as the optional param in the json `parse` method to collect the parse errors it encounters. We can then use this array to check if there's errors inside.

- Added new `constructJsonParseErrorMsg` util function (and added the function call to `readJsonFile`) to loop through the JSON parse error array and construct the error messages since the `jsonc-parser` package doesn't provide a proper error message itself, and only provides error codes and offsets.

For each of the parse errors we construct a message that includes:

    - A formatted error name obtained using the numeric error code
    - A line number calculated from the error's offset and length
    - A column number calculated from the error's offset

The messages are formatted like so:

`Error [error index] - [Error Name] at [filepath]:[line]:[column]`

The filepath format of `filepath:line:column` makes it easy to navigate to the specific place in the file for easier debugging.

- Added error handling so that it creates a new internal Error with a specific "Failed to parse..." error message. Then log the error stack to the Output Channel, which consists of the specific message, the parse error messages, and the stack trace of where in the extension it occurred.
Because `readJsonFile` is a make or break kinda function, we should notify the user by showing an error popup dialog.

- Added vscode's `showErrorMessage` method to tell the user that errors occurred when parsing JSON files, and the extension won't work correctly. It also opens the Output Channel when the button is clicked.

- Enabled the error to be thrown. By `throw`ing the error, we allow the extension to fail startup and crash at the point of parse errors.

This is to prevent further code that requires the parsed json from the `readJsonFile` function from throwing an unrelated error such as the "Cannot convert undefined or null to object" error, which makes it harder to debug. So if we fail the extension here, then we can easily debug the root cause.
Fixes the ts error from the workflow because I just forgot to commit it.

https://github.com/yCodeTech/auto-comment-blocks/actions/runs/19952456507/job/57215155157?pr=15

- Added new `showChannel` method to logger to show the Output Channel.
@yCodeTech yCodeTech merged commit 08de8ee into master Dec 5, 2025
1 check passed
@yCodeTech yCodeTech deleted the fix/issue-14/add-null-checks-and-error-handling branch December 5, 2025 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Extension startup crash - error: "Cannot convert undefined or null to object"

2 participants