Skip to content

Commit

Permalink
Gnievesponce/remove verbose exceptions (Azure-Samples#32)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Nieves-Ponce <gnievesponce@microsoft.com>
  • Loading branch information
nievespg1 and Gabriel Nieves-Ponce authored Jun 27, 2024
1 parent f6cf4b6 commit a0f0b78
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 87 deletions.
4 changes: 2 additions & 2 deletions backend/src/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def validate_index_file_exist(index_name: str, file_name: str):
)
)
container_store_client.read_item(index_name, index_name)
except Exception as e:
except Exception:
raise ValueError(
f"Container {index_name} is not a valid index.\nDetails: {str(e)}"
f"Container {index_name} is not a valid index."
)
# check for file existence
index_container_client = blob_service_client.get_container_client(index_name)
Expand Down
12 changes: 6 additions & 6 deletions backend/src/api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ async def get_all_data_storage_containers():
for item in container_store_client.read_all_items():
if item["type"] == "data":
items.append(item["human_readable_name"])
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error("Error getting list of blob containers.\nDetails: " + str(e))
reporter.on_error("Error getting list of blob containers.")
raise HTTPException(
status_code=500, detail="Error getting list of blob containers."
)
Expand Down Expand Up @@ -185,9 +185,9 @@ async def upload_files(
}
)
return BaseResponse(status="File upload successful.")
except Exception as e:
except Exception:
reporter.on_error(
"Error uploading files.", details={"ErrorDetails": str(e), "files": files}
"Error uploading files.", details={"files": files}
)
raise HTTPException(
status_code=500,
Expand Down Expand Up @@ -219,11 +219,11 @@ async def delete_files(storage_name: str):
item=sanitized_storage_name,
partition_key=sanitized_storage_name,
)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(
f"Error deleting container {storage_name}.",
details={"ErrorDetails": str(e), "Container": storage_name},
details={"Container": storage_name},
)
raise HTTPException(
status_code=500, detail=f"Error deleting container '{storage_name}'."
Expand Down
6 changes: 2 additions & 4 deletions backend/src/api/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ def task():
stream_response(report_df=report_df, query=request.query),
media_type="application/json",
)
except Exception as e:
except Exception:
# temporary logging of errors until reporters are in place
print(e)
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=str(e))
raise HTTPException(status_code=500, detail=None)
8 changes: 4 additions & 4 deletions backend/src/api/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ async def retrieve_graphml_file(index_name: str):
media_type="application/octet-stream",
headers={"Content-Disposition": f"attachment; filename={graphml_filename}"},
)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not retrieve graphml file: {str(e)}")
reporter.on_error("Could not retrieve graphml file")
raise HTTPException(
status_code=500,
detail=f"Could not retrieve graphml file for index '{index_name}'.",
Expand All @@ -80,9 +80,9 @@ async def retrieve_graph_stats(index_name: str):
bytes_io = BytesIO(blob_data)
g = nx.read_graphml(bytes_io)
return GraphDataResponse(nodes=len(g.nodes), edges=len(g.edges))
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not retrieve graph data file: {str(e)}")
reporter.on_error("Could not retrieve graph data file")
raise HTTPException(
status_code=500,
detail=f"Could not retrieve graph statistics for index '{index_name}'.",
Expand Down
35 changes: 17 additions & 18 deletions backend/src/api/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,21 @@ async def setup_indexing_pipeline(
batch_v1.create_namespaced_job(
body=job_manifest, namespace=os.environ["AKS_NAMESPACE"]
)
except ApiException as e:
except ApiException:
raise HTTPException(
status_code=500,
detail=f"exception when calling BatchV1Api->create_namespaced_job: {str(e)}",
detail="exception when calling BatchV1Api->create_namespaced_job",
)
return BaseResponse(status="indexing operation has been scheduled.")
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
job_details = {
"storage_name": storage_name,
"index_name": index_name,
}
reporter.on_error(
"Error creating a new index",
details={"error_details": str(e), "job_details": job_details},
details={"job_details": job_details},
)
raise HTTPException(
status_code=500,
Expand Down Expand Up @@ -336,26 +336,25 @@ async def _start_indexing_pipeline(index_name: str):
if pipeline_job.status == PipelineJobState.FAILED:
exit(1) # signal to AKS that indexing job failed

except Exception as e:
except Exception:
pipeline_job.status = PipelineJobState.FAILED

# update failed state in cosmos db
error_details = {
"error_details": str(e),
"error_message": "Indexing pipeline failed.",
}
# log error in local index directory logs
workflow_callbacks.on_error(
message=f"Index Name: {index_name}, Container Name: {storage_name}\n",
cause=e,
stack=traceback.format_exc(),
cause=None,
stack=None,
details=error_details,
)
# log error in global index directory logs
reporter.on_error(
f"Index Name: {index_name}, Container Name: {storage_name}\n {str(e)} \n",
cause=str(e),
stack=traceback.format_exc(),
stack=None,
details=error_details,
)
raise HTTPException(
Expand Down Expand Up @@ -405,9 +404,9 @@ async def get_all_indexes():
for item in container_store_client.read_all_items():
if item["type"] == "index":
items.append(item["human_readable_name"])
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Error retrieving index names: {str(e)}")
reporter.on_error("Error retrieving index names")
return IndexNameList(index_name=items)


Expand Down Expand Up @@ -437,21 +436,21 @@ def _delete_k8s_job(job_name: str, namespace: str) -> None:
try:
batch_v1 = client.BatchV1Api()
batch_v1.delete_namespaced_job(name=job_name, namespace=namespace)
except Exception as e:
except Exception:
reporter.on_error(
f"Error deleting k8s job {job_name}.",
details={"error_details": str(e), "Container": job_name},
details={"Container": job_name},
)
pass
try:
core_v1 = client.CoreV1Api()
job_pod = _get_pod_name(job_name, os.environ["AKS_NAMESPACE"])
if job_pod:
core_v1.delete_namespaced_pod(job_pod, namespace=namespace)
except Exception as e:
except Exception:
reporter.on_error(
f"Error deleting k8s pod for job {job_name}.",
details={"error_details": str(e), "Container": job_name},
details={"Container": job_name},
)
pass

Expand Down Expand Up @@ -510,11 +509,11 @@ async def delete_index(index_name: str):
if ai_search_index_name in index_client.list_index_names():
index_client.delete_index(ai_search_index_name)

except Exception as e:
except Exception:
reporter.on_error(
message=f"Error encountered while deleting all data for index {index_name}.",
stack=traceback.format_exc(),
details={"error_details": str(e), "container": index_name},
stack=None,
details={"container": index_name},
)
raise HTTPException(
status_code=500, detail=f"Error deleting index '{index_name}'."
Expand Down
20 changes: 10 additions & 10 deletions backend/src/api/index_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ async def get_all_entitys():
)
for item in entity_container.read_all_items():
items.append(item["human_readable_name"])
except Exception as e:
except Exception:
reporter = ReporterSingleton.get_instance()
reporter.on_error(f"Error getting all entity configurations: {str(e)}")
reporter.on_error("Error getting all entity configurations")
return EntityNameList(entity_configuration_name=items)


Expand Down Expand Up @@ -147,8 +147,8 @@ async def update_entity(request: EntityConfiguration):
item=sanitized_config_name,
partition_key=sanitized_config_name,
)
except Exception as e:
reporter.on_error(f"Error getting entity type: {str(e)}")
except Exception:
reporter.on_error("Error getting entity type")
reporter.on_error(
f"Item with entity configuration name '{request.entity_configuration_name}' not found."
)
Expand Down Expand Up @@ -181,8 +181,8 @@ async def update_entity(request: EntityConfiguration):
for i in request.entity_examples
]
entity_container.replace_item(sanitized_config_name, existing_item)
except Exception as e:
reporter.on_error(f"Error updating entity type: {str(e)}")
except Exception:
reporter.on_error("Error updating entity type")
return BaseResponse(status="Success.")


Expand Down Expand Up @@ -210,8 +210,8 @@ async def get_entity(entity_configuration_name: str):
entity_types=existing_item["entity_types"],
entity_examples=existing_item["entity_examples"],
)
except Exception as e:
reporter.on_error(f"Error getting entity type: {str(e)}")
except Exception:
reporter.on_error("Error getting entity type")
reporter.on_error(
f"Item with entity configuration name '{entity_configuration_name}' not found."
)
Expand Down Expand Up @@ -240,8 +240,8 @@ async def delete_entity(entity_configuration_name: str):
partition_key=sanitized_entity_config_name,
)
return BaseResponse(status="Success")
except Exception as e:
reporter.on_error(f"Error deleting entity: {str(e)}")
except Exception:
reporter.on_error("Error deleting entity")
reporter.on_error(
f"Item with entity configuration name '{entity_configuration_name}' not found."
)
Expand Down
6 changes: 3 additions & 3 deletions backend/src/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ async def global_query(request: GraphRequest):
]

return GraphResponse(result=result.response, context_data=result.context_data)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not perform global search. Exception: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
reporter.on_error("Could not perform global search.")
raise HTTPException(status_code=500, detail=None)


@query_route.post(
Expand Down
20 changes: 10 additions & 10 deletions backend/src/api/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ async def get_report_info(index_name: str, report_id: str):
)
row = report_table[report_table.community == report_id]
return ReportResponse(text=row["full_content"].values[0])
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not get report. Exception: {str(e)}")
reporter.on_error("Could not get report.")
raise HTTPException(
status_code=500,
detail=f"Error retrieving report '{report_id}' from index '{index_name}'.",
Expand Down Expand Up @@ -104,9 +104,9 @@ async def get_chunk_info(index_name: str, text_unit_id: str):
return TextUnitResponse(
text=row["chunk"].values[0], source_document=row["source_doc"].values[0]
)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not get text chunk. Exception: {str(e)}")
reporter.on_error("Could not get text chunk.")
raise HTTPException(
status_code=500,
detail=f"Error retrieving text chunk '{text_unit_id}' from index '{index_name}'.",
Expand Down Expand Up @@ -134,9 +134,9 @@ async def get_entity_info(index_name: str, entity_id: int):
description=row["description"].values[0],
text_units=row["text_unit_ids"].values[0].tolist(),
)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not get entity. Exception: {str(e)}")
reporter.on_error("Could not get entity")
raise HTTPException(
status_code=500,
detail=f"Error retrieving entity '{entity_id}' from index '{index_name}'.",
Expand Down Expand Up @@ -179,9 +179,9 @@ async def get_claim_info(index_name: str, claim_id: int):
text_unit_id=row["text_unit_id"].values[0],
document_ids=row["document_ids"].values[0].tolist(),
)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not get claim. Exception: {str(e)}")
reporter.on_error("Could not get claim.")
raise HTTPException(
status_code=500,
detail=f"Error retrieving claim '{claim_id}' from index '{index_name}'.",
Expand Down Expand Up @@ -225,9 +225,9 @@ async def get_relationship_info(index_name: str, relationship_id: int):
x[0] for x in row["text_unit_ids"].to_list()
], # extract text_unit_ids from a list of panda series
)
except Exception as e:
except Exception:
reporter = ReporterSingleton().get_instance()
reporter.on_error(f"Could not get relationship. Exception: {str(e)}")
reporter.on_error("Could not get relationship.")
raise HTTPException(
status_code=500,
detail=f"Error retrieving relationship '{relationship_id}' from index '{index_name}'.",
Expand Down
1 change: 0 additions & 1 deletion backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ async def catch_all_exceptions_middleware(request: Request, call_next):
try:
return await call_next(request)
except Exception:
print(traceback.format_exc())
return Response("Unexpected internal server error", status_code=500)


Expand Down
Loading

0 comments on commit a0f0b78

Please sign in to comment.