Skip to content

Commit

Permalink
refactor: raise httpexception to return invalid pid error message
Browse files Browse the repository at this point in the history
  • Loading branch information
bdromard committed Oct 16, 2024
1 parent 4fd728b commit 73ff017
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions boagent/api/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import time
from typing import Dict, Any, List, Union
from fastapi import FastAPI, Response, Body
from fastapi import FastAPI, Response, Body, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from boaviztapi_sdk.api.server_api import ServerApi
Expand Down Expand Up @@ -206,7 +206,7 @@ async def process_embedded_impacts(
try:
queried_process = Process(metrics_data, process_id)
except InvalidPIDException as invalid_pid:
return Response(status_code=400, content=invalid_pid.message)
raise HTTPException(status_code=400, detail=invalid_pid.message)
else:
process_embedded_impact_values = queried_process.embedded_impact_values
json_content = json.dumps(process_embedded_impact_values)
Expand Down
2 changes: 1 addition & 1 deletion boagent/api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class InvalidPIDException(Exception):
def __init__(self, pid):
self.pid = pid
self.message = f"Process_id {self.pid} has not been found in metrics data. Check the queried PID"
self.message = f"Process_id {self.pid} has not been found in metrics data. Check the queried PID."
super().__init__(self.message)
8 changes: 4 additions & 4 deletions tests/api/test_api_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ def test_get_process_embedded_impacts_with_error_if_pid_not_found_in_metrics_dat
}

response = client.get("/process_embedded_impacts", params=params)
assert response.status_code == 400
assert (
response.text
== "Process_id 1234 has not been found in metrics data. Check the queried PID"
error_message = (
"Process_id 1234 has not been found in metrics data. Check the queried PID."
)
self.assertEqual(response.status_code, 400)
self.assertIs(error_message in response.text, True)
2 changes: 1 addition & 1 deletion tests/api/test_api_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_get_process_exe(self):
def test_validate_pid_with_error_if_process_id_not_in_metrics(self):

expected_error_message = (
"Process_id 1234 has not been found in metrics data. Check the queried PID"
"Process_id 1234 has not been found in metrics data. Check the queried PID."
)

with self.assertRaises(InvalidPIDException) as context_manager:
Expand Down

0 comments on commit 73ff017

Please sign in to comment.