diff --git a/nitric/faas.py b/nitric/faas.py index 9e2255a..1e04dee 100644 --- a/nitric/faas.py +++ b/nitric/faas.py @@ -144,10 +144,12 @@ def __init__(self, data: bytes, method: str, path: str, params: Dict[str, str], @property def json(self) -> Optional[Any]: - """Get the body of the request as JSON, returns None if request body is not JSON""" + """Get the body of the request as JSON, returns None if request body is not JSON.""" try: return json.loads(self.body) - except: + except json.JSONDecodeError: + return None + except TypeError: return None @property @@ -178,8 +180,7 @@ def body(self, value: Union[str, bytes, Any]): self._body = value else: self._body = json.dumps(value).encode("utf-8") - self.headers['Content-Type'] = ['application/json'] - + self.headers["Content-Type"] = ["application/json"] class HttpContext(TriggerContext): @@ -509,6 +510,9 @@ async def _run(self): continue if request_channel.done(): break + except asyncio.CancelledError: + # Membrane has closed stream after init + print("stream from Membrane closed, closing client stream") except ConnectionRefusedError as cre: traceback.print_exc() raise ConnectionRefusedError("Failed to register function with Membrane") from cre @@ -516,7 +520,6 @@ async def _run(self): traceback.print_exc() raise Exception("An unexpected error occurred.") from e finally: - print("stream from Membrane closed, closing client stream") # The channel must be closed to complete the gRPC connection request_channel.close() channel.close()