-
Notifications
You must be signed in to change notification settings - Fork 72
feat(flagd): log error on invalid env var parsing #1773
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -85,6 +85,11 @@ static int fallBackToEnvOrDefault(String key, int defaultValue) { | |||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| return System.getenv(key) != null ? Integer.parseInt(System.getenv(key)) : defaultValue; | ||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||
| log.error( | ||||||||||||||||||||||||
| "Failed to parse env variable '{}' with value '{}' as int, using default: {}", | ||||||||||||||||||||||||
| key, | ||||||||||||||||||||||||
| System.getenv(key), | ||||||||||||||||||||||||
| defaultValue); | ||||||||||||||||||||||||
| return defaultValue; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -93,6 +98,11 @@ static long fallBackToEnvOrDefault(String key, long defaultValue) { | |||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| return System.getenv(key) != null ? Long.parseLong(System.getenv(key)) : defaultValue; | ||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||
| log.error( | ||||||||||||||||||||||||
| "Failed to parse env variable '{}' with value '{}' as long, using default: {}", | ||||||||||||||||||||||||
| key, | ||||||||||||||||||||||||
| System.getenv(key), | ||||||||||||||||||||||||
| defaultValue); | ||||||||||||||||||||||||
|
Comment on lines
+101
to
+105
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including the exception
Suggested change
|
||||||||||||||||||||||||
| return defaultValue; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -105,6 +115,11 @@ static List<String> fallBackToEnvOrDefaultList(String key, List<String> defaultV | |||||||||||||||||||||||
| .collect(Collectors.toList()) | ||||||||||||||||||||||||
| : defaultValue; | ||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||
| log.error( | ||||||||||||||||||||||||
| "Failed to parse env variable '{}' with value '{}' as list, using default: {}", | ||||||||||||||||||||||||
| key, | ||||||||||||||||||||||||
| System.getenv(key), | ||||||||||||||||||||||||
| defaultValue); | ||||||||||||||||||||||||
|
Comment on lines
+118
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding the exception
Suggested change
|
||||||||||||||||||||||||
| return defaultValue; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to include the exception
ein thelog.errorcall. This allows the logger to capture the stack trace and the specific error message (e.g., why the integer parsing failed), which is invaluable for debugging. Additionally, note thatSystem.getenv(key)is called multiple times in this method; while not a performance bottleneck in a configuration loader, it is generally better practice to retrieve the value once to ensure consistency and avoid redundant system lookups.