Skip to content

Conversation

@shrivarshapoojari
Copy link
Contributor

@shrivarshapoojari shrivarshapoojari commented Sep 8, 2025

Issue number: closes #7279

Summary

This PR enhances event handler exception classes to accept both string and dictionary messages, providing developers with the flexibility to pass structured error information while maintaining full backward compatibility. All exception classes in aws_lambda_powertools/event_handler/exceptions.py now support str | dict type annotations and include comprehensive docstrings.

Changes

1. Update Parameter types

  • Updated parameter types: Changed from msg: str to msg: str | dict across all exception classes in aws_lambda_powertools/event_handler/exceptions.py

2. Documentation Improvements

  • Added comprehensive docstrings to all exception class constructors
  • Updated parameter documentation to clearly specify that msg can be either a string or dictionary

3. Affected Exception Classes

  • ServiceError
  • BadRequestError
  • UnauthorizedError
  • ForbiddenError
  • NotFoundError
  • RequestTimeoutError
  • RequestEntityTooLargeError
  • InternalServerError
  • ServiceUnavailableError

User experience

Before Changes

Users were limited to simple string error messages:

# Only string messages supported
raise BadRequestError("Invalid email format")
raise UnauthorizedError("Access denied")
raise NotFoundError("User not found")

After Changes

Users can now choose between simple strings or rich structured error objects:
Option 1: String messages (backward compatible)

raise BadRequestError("Invalid email format")

Option 2: Structured dictionary messages (new capability)

raise BadRequestError({
    "error_code": "VALIDATION_FAILED",
    "message": "Invalid email format",
    "field": "email",
    "provided_value": "invalid-email",
    "expected_format": "[email protected]"
})

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

@shrivarshapoojari shrivarshapoojari requested a review from a team as a code owner September 8, 2025 12:27
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 8, 2025
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 8, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@shrivarshapoojari shrivarshapoojari changed the title improv/ Static typing: allow to pass dict as argument to exception classes chore(event handler): Allow to pass dict as argument to exception classes Sep 8, 2025
@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 8, 2025

@shrivarshapoojari shrivarshapoojari changed the title chore(event handler): Allow to pass dict as argument to exception classes chore(event_ handler): Allow to pass dict as argument to exception classes Sep 8, 2025
@shrivarshapoojari shrivarshapoojari changed the title chore(event_ handler): Allow to pass dict as argument to exception classes chore(event_handler): Allow to pass dict as argument to exception classes Sep 8, 2025
@leandrodamascena leandrodamascena changed the title chore(event_handler): Allow to pass dict as argument to exception classes refactor(event_handler): allow to pass dict as argument to exception classes Sep 8, 2025
@github-actions github-actions bot added internal Maintenance changes enhancement labels Sep 8, 2025
@codecov
Copy link

codecov bot commented Sep 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.33%. Comparing base (4fcf71f) to head (18e9d98).
⚠️ Report is 4 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #7341      +/-   ##
===========================================
- Coverage    96.36%   96.33%   -0.04%     
===========================================
  Files          275      275              
  Lines        13002    13003       +1     
  Branches       966      966              
===========================================
- Hits         12529    12526       -3     
- Misses         366      368       +2     
- Partials       107      109       +2     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Thanks for working on this @shrivarshapoojari! APPROVED.

@github-actions github-actions bot removed the internal Maintenance changes label Sep 8, 2025
@leandrodamascena
Copy link
Contributor

Codecov is reporting indirect changes that are not part of this PR! It's safe to merge.

@leandrodamascena leandrodamascena merged commit a186759 into aws-powertools:develop Sep 8, 2025
17 of 18 checks passed
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 8, 2025

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement event_handlers size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Static typing: allow to pass dict as argument to exception classes

2 participants