Skip to content

Commit

Permalink
Added simple exception handling for server.py
Browse files Browse the repository at this point in the history
  • Loading branch information
itisallgood committed Jan 17, 2025
1 parent e8f9bb2 commit bf22e8d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import traceback
from holmes.utils.cert_utils import add_custom_certificate

ADDITIONAL_CERTIFICATE: str = os.environ.get("CERTIFICATE", "")
Expand Down Expand Up @@ -84,6 +85,14 @@ async def lifespan(app: FastAPI):
yield


def exception_handler(error, endpoint_name):
logging.exception(f"Error during request to /api/{endpoint_name} endpoint:")
error_message = str(error)
stack_trace = traceback.format_exc()
raise HTTPException(status_code=500, detail={"message": error_message,
"traceback": stack_trace})


app = FastAPI(lifespan=lifespan)
console = Console()

Expand All @@ -101,6 +110,8 @@ def investigate_issues(investigate_request: InvestigateRequest):

except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
except Exception as error:
exception_handler(error, "investigate")


@app.post("/api/workload_health_check")
Expand Down Expand Up @@ -146,6 +157,8 @@ def workload_health_check(request: WorkloadHealthRequest):
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
except Exception as error:
exception_handler(error, "workload_health_check")


# older api that does not support conversation history
Expand Down Expand Up @@ -184,6 +197,8 @@ def issue_conversation(issue_chat_request: IssueChatRequest):
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
except Exception as error:
exception_handler(error, "issue_chat")


@app.post("/api/chat")
Expand All @@ -206,7 +221,9 @@ def chat(chat_request: ChatRequest):
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)

except Exception as error:
exception_handler(error, "chat")


@app.get("/api/model")
def get_model():
Expand Down

0 comments on commit bf22e8d

Please sign in to comment.