Skip to content

Commit

Permalink
feat: handle cancelled error from membrane gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed Nov 3, 2022
1 parent 664a16f commit 0d8ecde
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions nitric/faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -509,14 +510,16 @@ 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
except Exception as e:
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()
Expand Down

0 comments on commit 0d8ecde

Please sign in to comment.