From a0f0b78b91aca5fdf09f312981c1108eff81ad3b Mon Sep 17 00:00:00 2001 From: Gabriel Nieves-Ponce <39567323+nievespg1@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:05:12 -0400 Subject: [PATCH] Gnievesponce/remove verbose exceptions (#32) Co-authored-by: Gabriel Nieves-Ponce --- backend/src/api/common.py | 4 +-- backend/src/api/data.py | 12 +++---- backend/src/api/experimental.py | 6 ++-- backend/src/api/graph.py | 8 ++--- backend/src/api/index.py | 35 +++++++++--------- backend/src/api/index_configuration.py | 20 +++++------ backend/src/api/query.py | 6 ++-- backend/src/api/source.py | 20 +++++------ backend/src/main.py | 1 - frontend/app.py | 49 ++++++++++++-------------- notebooks/get-wiki-articles.py | 4 +-- 11 files changed, 78 insertions(+), 87 deletions(-) diff --git a/backend/src/api/common.py b/backend/src/api/common.py index 00f097d..5a27c95 100644 --- a/backend/src/api/common.py +++ b/backend/src/api/common.py @@ -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) diff --git a/backend/src/api/data.py b/backend/src/api/data.py index ed45cd4..f622bb2 100644 --- a/backend/src/api/data.py +++ b/backend/src/api/data.py @@ -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." ) @@ -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, @@ -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}'." diff --git a/backend/src/api/experimental.py b/backend/src/api/experimental.py index c6f6417..503cbee 100644 --- a/backend/src/api/experimental.py +++ b/backend/src/api/experimental.py @@ -189,8 +189,6 @@ async def global_search_streaming(request: GraphRequest): 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) diff --git a/backend/src/api/graph.py b/backend/src/api/graph.py index e975cea..5cfd8df 100644 --- a/backend/src/api/graph.py +++ b/backend/src/api/graph.py @@ -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}'.", @@ -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}'.", diff --git a/backend/src/api/index.py b/backend/src/api/index.py index eb55900..d19086b 100644 --- a/backend/src/api/index.py +++ b/backend/src/api/index.py @@ -198,13 +198,13 @@ 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, @@ -212,7 +212,7 @@ async def setup_indexing_pipeline( } 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, @@ -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( @@ -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) @@ -437,10 +436,10 @@ 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: @@ -448,10 +447,10 @@ def _delete_k8s_job(job_name: str, namespace: str) -> None: 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 @@ -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}'." diff --git a/backend/src/api/index_configuration.py b/backend/src/api/index_configuration.py index 5c8a634..df620d0 100644 --- a/backend/src/api/index_configuration.py +++ b/backend/src/api/index_configuration.py @@ -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) @@ -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." ) @@ -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.") @@ -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." ) @@ -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." ) diff --git a/backend/src/api/query.py b/backend/src/api/query.py index 087a696..23af4b4 100644 --- a/backend/src/api/query.py +++ b/backend/src/api/query.py @@ -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( diff --git a/backend/src/api/source.py b/backend/src/api/source.py index 2c5d164..22d1a4a 100644 --- a/backend/src/api/source.py +++ b/backend/src/api/source.py @@ -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}'.", @@ -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}'.", @@ -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}'.", @@ -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}'.", @@ -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}'.", diff --git a/backend/src/main.py b/backend/src/main.py index 8a76ce7..8166cd1 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -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) diff --git a/frontend/app.py b/frontend/app.py index bcc60a3..03e4c5c 100644 --- a/frontend/app.py +++ b/frontend/app.py @@ -44,8 +44,7 @@ def get_storage_data(api_url: str) -> dict | None: else: st.error(f"Error: {response.status_code}") return None - except Exception as e: - st.error(f"Error: {str(e)}") + except Exception: return None @@ -58,8 +57,7 @@ def get_entity_data(api_url: str) -> dict | None: else: st.error(f"Error: {response.status_code}") return None - except Exception as e: - st.error(f"Error: {str(e)}") + except Exception: return None @@ -72,8 +70,7 @@ def get_indexes_data(api_url: str) -> dict | None: else: st.error(f"Error: {response.status_code}") return None - except Exception as e: - st.error(f"Error: {str(e)}") + except Exception: return None @@ -105,8 +102,7 @@ async def query_index(index_name: list[str], query_type: str, query: str): else: st.error(f"Error: {response.status_code} {response.json()}") return None - except Exception as e: - st.error(f"Error: {str(e)}") + except Exception: return None @@ -120,8 +116,7 @@ def get_source_entity(index_name: str, entity_id: str) -> dict | None: else: st.error(f"Error: {response.status_code} {response.json()}") return None - except Exception as e: - st.error(f"Error: {str(e)}") + except Exception: return None @@ -144,8 +139,8 @@ async def app(): data_containers = [""] try: data_containers = data_containers + data["storage_name"] - except Exception as e: - print(f"No data containers found, continuing...\nException: {str(e)}") + except Exception: + print("No data containers found, continuing...") disable_other_input = False @@ -206,9 +201,9 @@ async def app(): st.session_state["entity_config"] = ["DEFAULT"] + get_entity_data(api_url)[ "entity_configuration_name" ] - except Exception as e: + except Exception: st.session_state.entity_config = [""] - print(f"No entity configurations found, continuing...\nException: {str(e)}") + print("No entity configurations found, continuing...") disable_entity_input = False st.session_state.update(st.session_state) @@ -304,8 +299,8 @@ async def app(): options_indexes = [""] try: options_indexes = options_indexes + indexes["index_name"] - except Exception as e: - print(f"No indexes found, continuing...\nException: {str(e)}") + except Exception: + print("No indexes found, continuing...") index_name_select = st.sidebar.selectbox( "Select an index to check its status.", options_indexes @@ -324,18 +319,18 @@ async def app(): try: percent_complete = status_response_text["percent_complete"] st.success(f"Status: {status_response_text['status']}") - except Exception as e: - print(f"Error: {str(e)}") + except Exception: + pass try: progress_bar.progress(float(percent_complete) / 100) st.success(f"Percent Complete: {percent_complete}% ") - except Exception as e: - print(f"Error: {str(e)}") + except Exception: + pass try: progress_status = status_response_text["progress"] st.success(f"Progress: {progress_status } ") - except Exception as e: - print(f"Error: {str(e)}") + except Exception: + pass else: st.error(f"Status: No workflow associated with {index_name}") @@ -446,11 +441,11 @@ async def app(): ): try: payload = json.loads(chunk) - except json.JSONDecodeError as e: + except json.JSONDecodeError: # In the event that a chunk is not a complete JSON object, # document it for further analysis. - print(chunk) - raise e + print("Error decoding JSON chunk.") + pass token = payload["token"] context = payload["context"] @@ -577,8 +572,8 @@ async def app(): st.dataframe( df_textinfo_rel, use_container_width=True ) - except requests.exceptions.RequestException as e: - st.error(f"Error with query: {str(e)}") + except requests.exceptions.RequestException: + st.error("Error with query") if search_button: await search_button_clicked() diff --git a/notebooks/get-wiki-articles.py b/notebooks/get-wiki-articles.py index e221cd7..3ae8630 100755 --- a/notebooks/get-wiki-articles.py +++ b/notebooks/get-wiki-articles.py @@ -53,8 +53,8 @@ def main(): with open(filename, "w", encoding="utf-8") as f: f.write(content) print(f"Saving wiki article '{title}' to {filename}") - except Exception as e: - print(f"Error fetching wiki article {title}: {e}") + except Exception: + print(f"Error fetching wiki article {title}") if __name__ == "__main__":