Skip to content
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

fix: Exception detail attribute #2231

Merged
merged 6 commits into from
Aug 26, 2023
Merged

fix: Exception detail attribute #2231

merged 6 commits into from
Aug 26, 2023

Conversation

gsakkis
Copy link
Contributor

@gsakkis gsakkis commented Aug 26, 2023

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR

Description

  1. Set correctly the detail attribute on LitestarException and HTTPException regardless of whether it's passed positionally or by name.

    Main branch:

    In [1]: from litestar.exceptions import LitestarException, HTTPException
    
    In [2]: LitestarException(detail="foo").detail
    Out[2]: 'foo'
    
    In [3]: LitestarException("foo").detail
    Out[3]: ''
    
    In [4]: HTTPException(detail="").detail
    Out[4]: 'Internal Server Error'
    
    In [5]: HTTPException("").detail
    Out[5]: ''

    This branch:

    In [1]: from litestar.exceptions import LitestarException, HTTPException
    
    In [2]: LitestarException(detail="foo").detail
    Out[2]: 'foo'
    
    In [3]: LitestarException("foo").detail
    Out[3]: 'foo'
    
    In [4]: HTTPException(detail="").detail
    Out[4]: 'Internal Server Error'
    
    In [5]: HTTPException("").detail
    Out[5]: 'Internal Server Error'
  2. Honor the detail class attribute for custom exception classes.

    Main branch:

    In [1]: from litestar.exceptions import LitestarException, HTTPException
    
    In [2]: class CustomLitestarException(LitestarException):
       ...:     detail = "xxx"
       ...:
    
    In [3]: class CustomHTTPException(HTTPException):
       ...:     detail = "yyy"
       ...:
    
    In [4]: CustomLitestarException().detail
    Out[4]: ''
    
    In [5]: CustomHTTPException().detail
    Out[5]: 'Internal Server Error'

    This branch:

    In [1]: from litestar.exceptions import LitestarException, HTTPException
    
    In [2]: class CustomLitestarException(LitestarException):
       ...:     detail = "xxx"
       ...:
    
    In [3]: class CustomHTTPException(HTTPException):
       ...:     detail = "yyy"
       ...:
    
    In [4]: CustomLitestarException().detail
    Out[4]: 'xxx'
    
    In [5]: CustomHTTPException().detail
    Out[5]: 'yyy'

- Fix LitestarException(detail) to be equivalent to LitestarException(detail=detail)
- Honor the detail class attribute (if any)
- Fix HTTPException(detail) to be equivalent to HTTPException(detail=detail)
- Honor the detail class attribute (if any)
@gsakkis gsakkis requested review from a team as code owners August 26, 2023 12:36
Copy link
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

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

Looks good! After merging, can you cherry-pick this onto the release-2.0 branch?

@gsakkis gsakkis changed the title Exception detail fixes fix: Exception detail attribute Aug 26, 2023
@sonarcloud
Copy link

sonarcloud bot commented Aug 26, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/2231

@gsakkis gsakkis merged commit 88de686 into main Aug 26, 2023
19 checks passed
@gsakkis gsakkis deleted the fix_exception_detail branch August 26, 2023 13:03
gsakkis added a commit that referenced this pull request Aug 26, 2023
* Update exception tests with custom subclasses of LitestarException and HTTPException

* LitestarException fixes:
- Fix LitestarException(detail) to be equivalent to LitestarException(detail=detail)
- Honor the detail class attribute (if any)

* HTTPException fixes:
- Fix HTTPException(detail) to be equivalent to HTTPException(detail=detail)
- Honor the detail class attribute (if any)

* Refactor HTTPException.__init__ to call super().__init__

* Fix annotation for python 3.8

* Mypy fixes
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.

None yet

2 participants