-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: Handling temperature sensor not available error
- Loading branch information
Showing
7 changed files
with
87 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class ErrorDetailModel(BaseModel): | ||
msg: str | ||
|
||
|
||
class ServiceNotAvailableModel(BaseModel): | ||
detail: ErrorDetailModel |
13 changes: 10 additions & 3 deletions
13
apps/rasptherm-backend/rasptherm_backend/routers/sensor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Protocol | ||
|
||
from rasptherm_sensor.types import SensorReadout | ||
|
||
|
||
class Sensor(Protocol): | ||
async def read_sensor(self) -> SensorReadout: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from collections.abc import Awaitable, Callable | ||
from functools import wraps | ||
from typing import TypeVar | ||
|
||
from fastapi import HTTPException, status | ||
from grpc import StatusCode # type: ignore | ||
from grpc.aio import AioRpcError # type: ignore | ||
|
||
from rasptherm_backend.types.sensor import Sensor | ||
|
||
_SensorOutput = TypeVar("_SensorOutput") | ||
_SensorFunc = Callable[[Sensor], Awaitable[_SensorOutput]] | ||
|
||
|
||
def sensor_not_available_handling( | ||
sensor_func: _SensorFunc[_SensorOutput], | ||
) -> _SensorFunc[_SensorOutput]: | ||
@wraps(sensor_func) | ||
async def handle_sensor_not_awailable(sensor: Sensor) -> _SensorOutput: | ||
try: | ||
return await sensor_func(sensor) | ||
except AioRpcError as rpc_exception: | ||
if rpc_exception.code() != StatusCode.UNAVAILABLE: | ||
raise rpc_exception | ||
|
||
raise HTTPException( | ||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | ||
detail={"msg": "The requested sensor is currently not available"}, | ||
) | ||
|
||
return handle_sensor_not_awailable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters