Skip to content

Bug: Unable to set default extra on exception #4006

@umarbutler

Description

@umarbutler

Description

Using the below code to define a custom exception does not let the extra field be returned in responses where the error is raised:

class InvalidApiKeyError(NotAuthorizedException):
    detail = "Invalid API key"
    extra = "The API key you provided is invalid, incorrect, expired, has been revoked, or your account is overdue."

The below code doesn't work either:

class InvalidApiKeyError(NotAuthorizedException):
    detail = "Invalid API key"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.extra = "The API key you provided is invalid, incorrect, expired, has been revoked, or your account is overdue."

URL to code causing the issue

No response

MCVE

# Your MCVE code here

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

Litestar Version

2.14.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expectedGood First IssueThis is good for newcomers to take on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions