-3

I am trying to raise the custom exception using the starlette framework in python. I have the API call which checks some condtions depends on the result, it should raise exception. I have two files app.py and error.py

#app.py
        from starlette.applications import Starlette
        from starlette.responses import JSONResponse
        from starlette.routing import Route

        from error import EmptyParamError

        async def homepage(request):

          a=1
          b=0

          if a == 1:
             raise EmptyParamError(400, "status_code")

         return JSONResponse({'hello': 'world'})


        routes = [
           Route("/", endpoint=homepage)
        ]

       app = Starlette(routes=routes,debug=True)`

#error.py ```
from starlette.responses import JSONResponse

class BaseError(Exception):
    def __init__(self, status_code: int, detail: str = None) -> None:
        if detail is None:
          detail = "http.HTTPStatus(status_code).phrase"
       self.status_code = status_code
       self.detail = detail

    async def not_found(self):
       return JSONResponse(content=self.title, status_code=self.status_code)



 class EmptyParamError(BaseError):
         """ Error is raised when group name is not provided. """
        status_code = 400
        title = "Missing Group Name"

When the condition is true, i want to raise the exception but its not returning the jsonrespnse but its returning the stacktrace on the console. Please let me know if anything is wrong here

lokesh
  • 27
  • 2

1 Answers1

1

adding try block resolved the issue

   try:
        if a==1:
            raise InvalidUsage(100,"invalid this one")

        if b == 0:
            raise EmptyParamError("this is empty paramuvi")
    except InvalidUsage as e:
        return JSONResponse({'hello': str(e.message)})
    except EmptyParamError as e:
        return JSONResponse({'hello': str(e.message)})
lokesh
  • 27
  • 2