Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix[AI_field_Description]: error handling when credit limit reached #42

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/app/api/v1/extract.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List
from app.exceptions import CreditLimitExceededException
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -99,6 +100,11 @@ async def get_field_descriptions(
"data": data,
}

except CreditLimitExceededException:
raise HTTPException(
status_code=402, detail="Credit limit Reached, Wait next month or upgrade your Plan!"
)

except Exception as e:
logger.error(e)
logger.log("Retrying AI field description generation.")
Expand Down
30 changes: 18 additions & 12 deletions backend/app/requests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
from app.exceptions import CreditLimitExceededException
from fastapi import HTTPException
from .schemas import ExtractFieldsResponse, TextExtractionResponse
import requests
from app.config import settings
Expand Down Expand Up @@ -124,14 +125,19 @@ def extract_field_descriptions(api_token, fields):
headers=headers,
timeout=360,
)
# Check the response status code
if response.status_code == 201 or response.status_code == 200:
return response.json()
else:
if response.status_code not in [200, 201]:
logger.error(
f"Unable to process file during field description generation. It returned {response.status_code} code: {response.text}"
f"Failed to field description. It returned {response.status_code} code: {response.text}"
)
raise Exception("Unable to process file!")
if response.status_code == 402:
raise CreditLimitExceededException(
response.json().get("detail", "Credit limit exceeded!")
)

raise HTTPException(response.json().get("detail", "Failed to fetch field description"))

# Check the response status code
return response.json()


def highlight_sentences_in_pdf(api_token, sentences, file_path, output_path):
Expand Down Expand Up @@ -203,13 +209,13 @@ def get_user_usage_data(api_token: str):

response = requests.post(url, headers=headers)

if response.status_code not in [200, 201]:
logger.error(
f"Failed to fetch usage data. It returned {response.status_code} code: {response.text}"
)
raise Exception("Failed to fetch usage data")

try:
if response.status_code not in [200, 201]:
logger.error(
f"Failed to fetch usage data. It returned {response.status_code} code: {response.text}"
)
raise Exception(response.text)

return response.json()
except requests.exceptions.JSONDecodeError:
logger.error(f"Invalid JSON response from API server: {response.text}")
Expand Down
14 changes: 11 additions & 3 deletions frontend/src/services/extract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ export const GetAIFieldDescriptions = async (
);
return response;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.data?.error) {
throw new Error(error.response.data.error);
if (axios.isAxiosError(error)) {
if (error.response?.data) {
throw new Error(error.response.data.detail);
} else {
throw new Error(
"Failed to generate AI field descriptions. Please try again."
);
}
} else {
throw new Error("Failed to extract data. Please try again.");
throw new Error(
"Failed to generate AI field descriptions. Please try again."
);
}
}
};
Loading