Skip to content

fix: Handle Exceptions and Prevent application from crashing when using Logger #538

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
merged 2 commits into from
Feb 16, 2024

Conversation

hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Jan 5, 2024

Please provide the issue number

Issue number: #514

Summary

On exceptions within the Logging framework code, currently, it throws an exception and brings down the whole application.
For example below is a code that has a mismatch count of parameters passed to the log message - Missing Count property value being passed in.

Logger.LogInformation(extraKeys, "Retrieved data for city {cityName} with count {Count}", new[] { cityName });

It throws an exception and causes the Lambda Handler endpoint to crash and return a 500 Internal Server Error

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Microsoft.Extensions.Logging.LogValuesFormatter.GetValue(Object[] values, Int32 index)
   at Microsoft.Extensions.Logging.FormattedLogValues.get_Item(Int32 index)
   at Microsoft.Extensions.Logging.FormattedLogValues.GetEnumerator()+MoveNext(

Changes

Add exception handling in Log extension method.

try
{
    Log(logger, logLevel, extraKeys, 0, null, message, args);
}
catch (Exception e)
{
    logger.Log(LogLevel.Error, 0, e, "Powertools internal error");
}

Add Tests

User experience

The client should not see any exception in case Logger fails internally.
It will be logged as error to get visibility

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@auto-assign auto-assign bot requested a review from amirkaws January 5, 2024 11:27
@boring-cyborg boring-cyborg bot added the area/logging Core logging utility label Jan 5, 2024
@auto-assign auto-assign bot requested a review from sliedig January 5, 2024 11:27
@boring-cyborg boring-cyborg bot added the tests label Jan 5, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 5, 2024
@github-actions github-actions bot added the bug Unexpected, reproducible and unintended software behaviour label Jan 5, 2024
Copy link

sonarqubecloud bot commented Jan 5, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (7308cc6) 72.23% compared to head (5159cd8) 73.32%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #538      +/-   ##
===========================================
+ Coverage    72.23%   73.32%   +1.08%     
===========================================
  Files          101      101              
  Lines         4106     4112       +6     
  Branches       418      418              
===========================================
+ Hits          2966     3015      +49     
+ Misses        1026      977      -49     
- Partials       114      120       +6     
Flag Coverage Δ
unittests 73.32% <100.00%> (+1.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@amirkaws amirkaws left a comment

Choose a reason for hiding this comment

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

LGTM

@hjgraca hjgraca merged commit efec6fb into aws-powertools:develop Feb 16, 2024
@hjgraca hjgraca deleted the hendle-exception-logger branch February 16, 2024 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/logging Core logging utility bug Unexpected, reproducible and unintended software behaviour size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Handle Exceptions and Prevent application from crashing when using Logger
3 participants