Skip to content

Error handling that has access to cbv #69

@hstoebel

Description

@hstoebel

I would like to make a class based view that can define its own error handling. I know that I can define global error handlers using @app.exception_handler(MyException). But I would love to be able to write an error handler that some how has access to the cbv instance so I can access state that was built up while processing the request. I think this would lead to cleaner code than wrapping everything endpoint in a try/except

I'm imagining something like this:

@cbv(router)
Class MyView:
    @router.get('/hello')
    def hello(self):
        return {'msg': 'hello world'}

    def handle_errors(self):
        """errors happening in this class are routed to here"""

If I was using Flask, there would be a way to do this with their class based view system

I might do something like this

from flask.views import View

class BaseRoute(View):
    def __init__(self, *args, **kwargs):
        """
        do setup here
        """

    def dispatch_request(self, *args, **kwargs):
        try:
            # self.work is implemented by the sun class
            return self.work(*args, **kwargs)
        except MyError as e:
            """
            handle my error
            """
        finally:
            """
            clean up
            """

class GetUsers(BaseRoute):
    def work(self, *args, **kwargs):
        """
        do the actual work of the endpoint
        """

But of course since FastAPI aims to be smarter about types, this is less trivial (after a few hours of looking around I didn't see an obvious way).

Any ideas on how to go about something like this? I'm flexible with the approach to solving it, at the end of the day I want access to self when handling errors without having to do a try/except in every endpoint

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions