AOSSIE-Org / AOSSIE-Org/Perspective

Centralized Error Handling + Strong Input Validation

オープン
#143 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る
good first issue
主要言語
TypeScript
スター
50
フォーク
78
PR マージ指標
30日以内にマージされた PR はありません

説明

### Context

The backend FastAPI endpoints (`/bias`, `/process`, `/chat`) currently process user requests without full validation, and errors thrown by the NLP pipelines or vector database modules are not consistently handled. While Pydantic models exist (`URlRequest` and `ChatQuery`), they allow any string, which can lead to invalid URLs or empty chat messages. Additionally, exceptions from functions like `run_scraper_pipeline`, `check_bias`, `run_langgraph_workflow`, `search_pinecone`, or `ask_llm` can propagate unhandled, returning inconsistent 500 errors to the frontend.

**Problem:**
- Input data is not fully validated (invalid URLs, empty messages).
- Endpoint errors are inconsistent and sometimes unhandled.
- Frontend and API consumers cannot reliably parse error responses, making debugging harder.

**Proposed Solution:**
1. **Strong Input Validation:**
- Use `pydantic.HttpUrl` for URL fields in `URlRequest`.
- Ensure non-empty messages for `/chat` using Pydantic validators.
- Return structured 400 errors for invalid inputs.

2. **Centralized Error Handling Middleware:**
- Implement a global FastAPI exception handler using `@app.exception_handler`.
- Catch both `HTTPException` and unhandled exceptions.
- Return a consistent JSON error format, e.g.:
```json
{
"error": "",
"code": ""
}
```
- Optionally log exceptions with trace IDs for easier debugging.

**Impact:**
- Improves backend reliability and maintainability.
- Provides consistent error responses to the frontend.
- Prevents invalid input from breaking NLP and LangGraph pipelines.
- Enhances developer and contributor experience by reducing unexpected server errors.

### What Needs to Be Done

1. Strong Input Validation
2. Centralized Error Handling Middleware

### Resources

- [Contribution Guide - Start Here!](https://github.com/AOSSIE-Org/TODO/blob/main/CONTRIBUTING.md)
- [Discord Channel](https://discord.gg/hjUhu33uAn)

### Getting Started

- [x] I have joined the [Discord server](https://discord.gg/hjUhu33uAn) and will post updates there
- [x] I have read the [Contribution Guide](https://github.com/AOSSIE-Org/Template-Repo/blob/main/CONTRIBUTING.md)
- [x] I understand this issue is assigned on a first-come, first-served basis

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。