From b1d1833234ae0c131d89e4c5de8c532191195eec Mon Sep 17 00:00:00 2001 From: 01AtharvR <164509706+01AtharvR@users.noreply.github.com> Date: Mon, 13 Jul 2026 16:36:23 +0530 Subject: [PATCH] Replace bare except with specific exceptions in api layer (#783) (#1039) --- trustgraph-base/trustgraph/api/api.py | 4 ++-- trustgraph-base/trustgraph/api/async_flow.py | 4 ++-- trustgraph-base/trustgraph/api/async_socket_client.py | 2 +- trustgraph-base/trustgraph/api/bulk_client.py | 8 ++++---- trustgraph-base/trustgraph/api/config.py | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/trustgraph-base/trustgraph/api/api.py b/trustgraph-base/trustgraph/api/api.py index 0190d3f5..3a091b3b 100644 --- a/trustgraph-base/trustgraph/api/api.py +++ b/trustgraph-base/trustgraph/api/api.py @@ -25,7 +25,7 @@ def check_error(response): try: msg = response["error"]["message"] tp = response["error"]["type"] - except: + except KeyError: raise ApplicationException(response["error"]) raise ApplicationException(f"{tp}: {msg}") @@ -208,7 +208,7 @@ class Api: try: # Parse the response as JSON object = resp.json() - except: + except ValueError: raise ProtocolException(f"Expected JSON response") check_error(object) diff --git a/trustgraph-base/trustgraph/api/async_flow.py b/trustgraph-base/trustgraph/api/async_flow.py index 97167c81..68d7cd86 100644 --- a/trustgraph-base/trustgraph/api/async_flow.py +++ b/trustgraph-base/trustgraph/api/async_flow.py @@ -25,7 +25,7 @@ def check_error(response): try: msg = response["error"]["message"] tp = response["error"]["type"] - except: + except KeyError: raise ApplicationException(response["error"]) raise ApplicationException(f"{tp}: {msg}") @@ -88,7 +88,7 @@ class AsyncFlow: try: obj = await resp.json() - except: + except (ValueError, aiohttp.ContentTypeError): raise ProtocolException(f"Expected JSON response") check_error(obj) diff --git a/trustgraph-base/trustgraph/api/async_socket_client.py b/trustgraph-base/trustgraph/api/async_socket_client.py index 4ba3795b..3d1030ef 100644 --- a/trustgraph-base/trustgraph/api/async_socket_client.py +++ b/trustgraph-base/trustgraph/api/async_socket_client.py @@ -142,7 +142,7 @@ class AsyncSocketClient: for queue in self._pending.values(): try: await queue.put({"error": str(e)}) - except: + except asyncio.CancelledError : pass finally: self._connected = False diff --git a/trustgraph-base/trustgraph/api/bulk_client.py b/trustgraph-base/trustgraph/api/bulk_client.py index ae185240..452df7ea 100644 --- a/trustgraph-base/trustgraph/api/bulk_client.py +++ b/trustgraph-base/trustgraph/api/bulk_client.py @@ -201,7 +201,7 @@ class BulkClient: finally: try: loop.run_until_complete(async_gen.aclose()) - except: + except Exception: pass async def _export_triples_async(self, flow: str) -> Iterator[Triple]: @@ -299,7 +299,7 @@ class BulkClient: finally: try: loop.run_until_complete(async_gen.aclose()) - except: + except Exception: pass async def _export_graph_embeddings_async(self, flow: str) -> Iterator[Dict[str, Any]]: @@ -393,7 +393,7 @@ class BulkClient: finally: try: loop.run_until_complete(async_gen.aclose()) - except: + except Exception: pass async def _export_document_embeddings_async(self, flow: str) -> Iterator[Dict[str, Any]]: @@ -517,7 +517,7 @@ class BulkClient: finally: try: loop.run_until_complete(async_gen.aclose()) - except: + except Exception: pass async def _export_entity_contexts_async(self, flow: str) -> Iterator[Dict[str, Any]]: diff --git a/trustgraph-base/trustgraph/api/config.py b/trustgraph-base/trustgraph/api/config.py index 5f17672f..10a9f16e 100644 --- a/trustgraph-base/trustgraph/api/config.py +++ b/trustgraph-base/trustgraph/api/config.py @@ -254,7 +254,7 @@ class Config: ) for v in object["values"] ] - except: + except (KeyError, TypeError): raise ProtocolException(f"Response not formatted correctly") def get_values_all_workspaces(self, type): @@ -330,6 +330,6 @@ class Config: try: return object["config"], object["version"] - except: + except KeyError: raise ProtocolException(f"Response not formatted correctly")