Skip to content

Commit 7d82f6a

Browse files
author
David Linko
committed
Merge remote-tracking branch 'origin/305-rest-api-error-handling' into 281-user-friendly-decommutation-of-received-reports
2 parents 9a30211 + 2679935 commit 7d82f6a

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

anms-core/anms/routes/network_manager.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# the prime contract 80NM0018D0004 between the Caltech and NASA under
2222
# subcontract 1658085.
2323
#
24-
from fastapi import APIRouter, status
24+
from fastapi import APIRouter, status, HTTPException
2525
import requests
2626
from pydantic import BaseModel
2727

@@ -51,7 +51,7 @@ async def nm_get_version():
5151
try:
5252
request = requests.get(url=url)
5353
except Exception:
54-
return {}
54+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="ConnectTimeout")
5555
return request.json()
5656

5757

@@ -63,7 +63,7 @@ def nm_get_agents():
6363
try:
6464
request = requests.get(url=url)
6565
except Exception:
66-
return -1
66+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="ConnectTimeout")
6767
return request.json()
6868

6969

@@ -90,20 +90,20 @@ def do_nm_put_hex_eid(eid: str, ari: str):
9090
url = nm_url + "/agents/eid/{}/send?form=hex".format(_prepare_url(eid))
9191
logger.info('post to nm manager %s with eid %s and data %s' % (url, eid, ari))
9292

93-
try:
93+
try:
9494
request = requests.post(url=url,
9595
data=ari,
9696
headers={'Content-Type': 'text/plain'},
9797
timeout=(2.0, 8.0) # 2s for manager to connect, 8s for it to respond
9898
)
9999
except requests.exceptions.ConnectTimeout:
100-
return status.HTTP_504_GATEWAY_TIMEOUT
100+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="ConnectTimeout")
101101
except requests.exceptions.ReadTimeout:
102-
return status.HTTP_504_GATEWAY_TIMEOUT
102+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="ReadTimeout")
103103
except requests.exceptions.Timeout:
104-
return status.HTTP_504_GATEWAY_TIMEOUT
104+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="Timeout")
105105
except Exception:
106-
return status.HTTP_500_INTERNAL_SERVER_ERROR
106+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
107107
return request.status_code
108108

109109

anms-core/anms/routes/transcoder.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import time
2323
import asyncio
2424

25-
from fastapi import APIRouter, Depends
26-
from fastapi import status
25+
from fastapi import APIRouter, Depends, status, HTTPException
2726
from fastapi_pagination import Page, Params
2827
from fastapi_pagination.ext.async_sqlalchemy import paginate
2928

@@ -144,7 +143,7 @@ async def transcoder_put_await_str(input_ari: str):
144143
def transcoder_incoming_str(input_ari: str):
145144
return _transcoder_put_str(input_ari)
146145

147-
def _transcoder_put_str(input_ari: str):
146+
def transcoder_put_str(input_ari: str):
148147
input_ari = input_ari.strip()
149148
transcoder_log_id = None
150149
send_to_transcode = False
@@ -171,7 +170,11 @@ def _transcoder_put_str(input_ari: str):
171170

172171

173172
# PUT /ui/incoming_send/str Body is str ARI to send to transcoder
174-
@router.put("/ui/incoming_send/str", status_code=status.HTTP_200_OK)
173+
@router.put("/ui/incoming_send/str", status_code=status.HTTP_200_OK,
174+
responses={
175+
status.HTTP_500_INTERNAL_SERVER_ERROR: {"description" : "Error response from NM"},
176+
status.HTTP_504_GATEWAY_TIMEOUT: {"description" : "Manager response timed out"}
177+
})
175178
async def transcoder_send_ari_str(eid: str, ari: str):
176179
try:
177180
# Perform translation (API wrapper)
@@ -188,18 +191,24 @@ async def transcoder_send_ari_str(eid: str, ari: str):
188191
if info.parsed_as != "pending":
189192
break
190193
if retries <= 0:
191-
return { "idinfo" : idinfo, "info" : info, "status" : 504 }
194+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT,
195+
detail={ "idinfo" : idinfo, "info" : info, "status" : "transcoder timeout" })
196+
192197
retries -= 1
193198

194199
if info.parsed_as == "ERROR":
195-
return { "idinfo" : idinfo, "info" : info, "status" : 500 }
200+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
201+
detail={ "idinfo" : idinfo, "info" : info, "status" : 500 })
196202

197203
# Publish
198204
state = do_nm_put_hex_eid( eid, info.cbor )
199-
200205
return { "idinfo" : idinfo, "info" : info, "status" : state }
206+
except HTTPException as e:
207+
e.detail = { "idinfo" : idinfo, "info" : info, "status" : e.status_code }
208+
raise e
201209
except Exception as e:
202210
logger.exception(e)
203-
return status.HTTP_500_INTERNAL_SERVER_ERROR
211+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
212+
204213

205214

deps/dtnma-tools

Submodule dtnma-tools updated 303 files

testenv.Containerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ RUN systemctl enable ion bpecho@4 refda-ion && \
176176
mkdir -p /var/run/ion
177177

178178
# Runtime config for this container
179+
COPY deps/dtnma-tools/integration-test-ion/startup.uri /etc/refda/startup.uri
179180
COPY deps/test-ion-configs/agent-2.rc /etc/ion/node-2.rc
180181
COPY deps/test-ion-configs/agent-3.rc /etc/ion/node-3.rc
181182

0 commit comments

Comments
 (0)