Skip to content

Conversation

@neilhuang007
Copy link

  • Use default values when config options are missing instead of throwing
  • Regenerate config automatically when options are missing or malformed
  • Add TRUNCATE_EXISTING to prevent leftover data from corrupting config
  • Remove System.exit(1) on config parse errors to allow recovery

Fixes #115

- Use default values when config options are missing instead of throwing
- Regenerate config automatically when options are missing or malformed
- Add TRUNCATE_EXISTING to prevent leftover data from corrupting config
- Remove System.exit(1) on config parse errors to allow recovery

Fixes imthosea#115
Copilot AI review requested due to automatic review settings December 25, 2025 00:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves config file handling by gracefully recovering from corrupted or incomplete config files instead of crashing the application. The key changes include removing System.exit(1) calls on config errors, automatically detecting missing options, and regenerating the config file when necessary.

Key Changes:

  • Added hasMissingOptions flag to track when config options are missing
  • Changed exception handling to log warnings and use default values instead of throwing exceptions or exiting
  • Added TRUNCATE_EXISTING flag to prevent partial writes from corrupting config files

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
ConfigLoadContext.java Added hasMissingOptions tracking, removed System.exit(1) on config load failure, changed missing option handling from throwing exceptions to logging warnings
Config.java Updated config regeneration logic to trigger when hasMissingOptions is true, added TRUNCATE_EXISTING flag to file write operation

Note: There is a critical issue with the implementation - while missing config options are now handled gracefully, malformed config values (invalid booleans, invalid numbers, negative numbers) still throw IllegalStateException in the boolOrDefault() and intOrDefault() methods. This means corrupted config files with malformed values will still crash instead of gracefully degrading as intended by the PR.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 73 to 78
String str = properties.getProperty(name);
if(str == null) {
throw new IllegalStateException("Config option " + name + " not found");
LOGGER.warn("Config option {} not found - using default value. Config will be regenerated.", name);
hasMissingOptions = true;
}
return str;
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The str() method now returns null when a config option is not found and sets hasMissingOptions = true. However, the callers of this method (boolOrDefault at line 42 and intOrDefault at line 55) still throw IllegalStateException for malformed values instead of gracefully falling back to defaults. This creates an inconsistency where missing options are handled gracefully, but malformed options still cause exceptions. For complete graceful degradation as described in the PR, those methods should also handle malformed values by logging warnings, setting hasMissingOptions = true, and returning the default value.

Copilot uses AI. Check for mistakes.
Comment on lines +16 to 18
public boolean hasMissingOptions = false;

public ConfigLoadContext() {
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hasMissingOptions field should be initialized with an explicit default value even though false is the default for boolean fields in Java. This makes the intent clearer and follows defensive programming practices, especially since this field is being used to determine whether to regenerate the config file.

Suggested change
public boolean hasMissingOptions = false;
public ConfigLoadContext() {
public boolean hasMissingOptions;
public ConfigLoadContext() {
this.hasMissingOptions = false;

Copilot uses AI. Check for mistakes.
LOGGER.error("Failed to load config from " + FILE + ". " +
"If you need to, you can delete the file to generate a new one.", e);
System.exit(1);
LOGGER.error("Failed to load config from " + FILE + ". Config will be regenerated with default values.", e);
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message states "Config will be regenerated with default values" but this is not entirely accurate. The config regeneration logic is in Config.java and depends on checking hasMissingOptions. However, when loadProp() returns null due to an exception, the properties field is set to null, and str() will return null for all config reads. While the config will eventually be regenerated (due to fromExistingFile() returning false), the message could be more precise by stating "Attempting to use default values and regenerate config" to better reflect the actual behavior.

Suggested change
LOGGER.error("Failed to load config from " + FILE + ". Config will be regenerated with default values.", e);
LOGGER.error("Failed to load config from " + FILE + ". Attempting to use default values and regenerate config.", e);

Copilot uses AI. Check for mistakes.
@imthosea imthosea closed this Dec 28, 2025
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.

2 participants