99
99
ToolUpdate ,
100
100
)
101
101
from mcpgateway .services .completion_service import CompletionService
102
- from mcpgateway .services .gateway_service import GatewayConnectionError , GatewayNameConflictError , GatewayService
102
+ from mcpgateway .services .gateway_service import GatewayConnectionError , GatewayNameConflictError , GatewayNotFoundError , GatewayService
103
103
from mcpgateway .services .logging_service import LoggingService
104
104
from mcpgateway .services .prompt_service import (
105
105
PromptError ,
124
124
from mcpgateway .services .tool_service import (
125
125
ToolError ,
126
126
ToolNameConflictError ,
127
+ ToolNotFoundError ,
127
128
ToolService ,
128
129
)
129
130
from mcpgateway .transports .sse_transport import SSETransport
@@ -1247,15 +1248,25 @@ async def create_tool(tool: ToolCreate, db: Session = Depends(get_db), user: str
1247
1248
try :
1248
1249
logger .debug (f"User { user } is creating a new tool" )
1249
1250
return await tool_service .register_tool (db , tool )
1250
- except ToolNameConflictError as e :
1251
- if not e .enabled and e .tool_id :
1252
- raise HTTPException (
1253
- status_code = status .HTTP_409_CONFLICT ,
1254
- detail = f"Tool name already exists but is inactive. Consider activating it with ID: { e .tool_id } " ,
1255
- )
1256
- raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = str (e ))
1257
- except ToolError as e :
1258
- raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (e ))
1251
+ except Exception as ex :
1252
+ logger .error (f"Error while creating tool: { ex } " )
1253
+ if isinstance (ex , ToolNameConflictError ):
1254
+ if not ex .enabled and ex .tool_id :
1255
+ raise HTTPException (
1256
+ status_code = status .HTTP_409_CONFLICT ,
1257
+ detail = f"Tool name already exists but is inactive. Consider activating it with ID: { ex .tool_id } " ,
1258
+ )
1259
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = str (ex ))
1260
+ if isinstance (ex , (ValidationError , ValueError )):
1261
+ logger .error (f"Validation error while creating tool: { ex } " )
1262
+ raise HTTPException (status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = ErrorFormatter .format_validation_error (ex ))
1263
+ if isinstance (ex , IntegrityError ):
1264
+ logger .error (f"Integrity error while creating tool: { ex } " )
1265
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = ErrorFormatter .format_database_error (ex ))
1266
+ if isinstance (ex , ToolError ):
1267
+ raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (ex ))
1268
+ logger .error (f"Unexpected error while creating tool: { ex } " )
1269
+ raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "An unexpected error occurred while creating the tool" )
1259
1270
1260
1271
1261
1272
@tool_router .get ("/{tool_id}" , response_model = Union [ToolRead , Dict ])
@@ -1319,8 +1330,19 @@ async def update_tool(
1319
1330
try :
1320
1331
logger .debug (f"User { user } is updating tool with ID { tool_id } " )
1321
1332
return await tool_service .update_tool (db , tool_id , tool )
1322
- except Exception as e :
1323
- raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (e ))
1333
+ except Exception as ex :
1334
+ if isinstance (ex , ToolNotFoundError ):
1335
+ raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = str (ex ))
1336
+ if isinstance (ex , ValidationError ):
1337
+ logger .error (f"Validation error while creating tool: { ex } " )
1338
+ raise HTTPException (status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = ErrorFormatter .format_validation_error (ex ))
1339
+ if isinstance (ex , IntegrityError ):
1340
+ logger .error (f"Integrity error while creating tool: { ex } " )
1341
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = ErrorFormatter .format_database_error (ex ))
1342
+ if isinstance (ex , ToolError ):
1343
+ raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (ex ))
1344
+ logger .error (f"Unexpected error while creating tool: { ex } " )
1345
+ raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "An unexpected error occurred while creating the tool" )
1324
1346
1325
1347
1326
1348
@tool_router .delete ("/{tool_id}" )
@@ -1716,10 +1738,24 @@ async def create_prompt(
1716
1738
logger .debug (f"User: { user } requested to create prompt: { prompt } " )
1717
1739
try :
1718
1740
return await prompt_service .register_prompt (db , prompt )
1719
- except PromptNameConflictError as e :
1720
- raise HTTPException (status_code = 409 , detail = str (e ))
1721
- except PromptError as e :
1722
- raise HTTPException (status_code = 400 , detail = str (e ))
1741
+ except Exception as e :
1742
+ if isinstance (e , PromptNameConflictError ):
1743
+ # If the prompt name already exists, return a 409 Conflict error
1744
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = str (e ))
1745
+ if isinstance (e , PromptError ):
1746
+ # If there is a general prompt error, return a 400 Bad Request error
1747
+ raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (e ))
1748
+ if isinstance (e , ValidationError ):
1749
+ # If there is a validation error, return a 422 Unprocessable Entity error
1750
+ logger .error (f"Validation error while creating prompt: { e } " )
1751
+ raise HTTPException (status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = ErrorFormatter .format_validation_error (e ))
1752
+ if isinstance (e , IntegrityError ):
1753
+ # If there is an integrity error, return a 409 Conflict error
1754
+ logger .error (f"Integrity error while creating prompt: { e } " )
1755
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = ErrorFormatter .format_database_error (e ))
1756
+ # For any other unexpected errors, return a 500 Internal Server Error
1757
+ logger .error (f"Unexpected error while creating prompt: { e } " )
1758
+ raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "An unexpected error occurred while creating the prompt" )
1723
1759
1724
1760
1725
1761
@prompt_router .post ("/{name}" )
@@ -1801,13 +1837,28 @@ async def update_prompt(
1801
1837
HTTPException: * **409 Conflict** - a different prompt with the same *name* already exists and is still active.
1802
1838
* **400 Bad Request** - validation or persistence error raised by :pyclass:`~mcpgateway.services.prompt_service.PromptService`.
1803
1839
"""
1840
+ logger .info (f"User: { user } requested to update prompt: { name } with data={ prompt } " )
1804
1841
logger .debug (f"User: { user } requested to update prompt: { name } with data={ prompt } " )
1805
1842
try :
1806
1843
return await prompt_service .update_prompt (db , name , prompt )
1807
- except PromptNameConflictError as e :
1808
- raise HTTPException (status_code = 409 , detail = str (e ))
1809
- except PromptError as e :
1810
- raise HTTPException (status_code = 400 , detail = str (e ))
1844
+ except Exception as e :
1845
+ if isinstance (e , PromptNotFoundError ):
1846
+ raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = str (e ))
1847
+ if isinstance (e , ValidationError ):
1848
+ logger .error (f"Validation error while updating prompt: { e } " )
1849
+ raise HTTPException (status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = ErrorFormatter .format_validation_error (e ))
1850
+ if isinstance (e , IntegrityError ):
1851
+ logger .error (f"Integrity error while updating prompt: { e } " )
1852
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = ErrorFormatter .format_database_error (e ))
1853
+ if isinstance (e , PromptNameConflictError ):
1854
+ # If the prompt name already exists, return a 409 Conflict error
1855
+ raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = str (e ))
1856
+ if isinstance (e , PromptError ):
1857
+ # If there is a general prompt error, return a 400 Bad Request error
1858
+ raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (e ))
1859
+ # For any other unexpected errors, return a 500 Internal Server Error
1860
+ logger .error (f"Unexpected error while updating prompt: { e } " )
1861
+ raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "An unexpected error occurred while updating the prompt" )
1811
1862
1812
1863
1813
1864
@prompt_router .delete ("/{name}" )
@@ -1822,15 +1873,26 @@ async def delete_prompt(name: str, db: Session = Depends(get_db), user: str = De
1822
1873
1823
1874
Returns:
1824
1875
Status message.
1876
+
1877
+ Raises:
1878
+ HTTPException: If the prompt is not found, a prompt error occurs, or an unexpected error occurs during deletion.
1825
1879
"""
1826
1880
logger .debug (f"User: { user } requested deletion of prompt { name } " )
1827
1881
try :
1828
1882
await prompt_service .delete_prompt (db , name )
1829
1883
return {"status" : "success" , "message" : f"Prompt { name } deleted" }
1830
- except PromptNotFoundError as e :
1831
- return {"status" : "error" , "message" : str (e )}
1832
- except PromptError as e :
1833
- return {"status" : "error" , "message" : str (e )}
1884
+ except Exception as e :
1885
+ if isinstance (e , PromptNotFoundError ):
1886
+ raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = str (e ))
1887
+ if isinstance (e , PromptError ):
1888
+ raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = str (e ))
1889
+ logger .error (f"Unexpected error while deleting prompt { name } : { e } " )
1890
+ raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "An unexpected error occurred while deleting the prompt" )
1891
+
1892
+ # except PromptNotFoundError as e:
1893
+ # return {"status": "error", "message": str(e)}
1894
+ # except PromptError as e:
1895
+ # return {"status": "error", "message": str(e)}
1834
1896
1835
1897
1836
1898
################
@@ -1919,18 +1981,18 @@ async def register_gateway(
1919
1981
return await gateway_service .register_gateway (db , gateway )
1920
1982
except Exception as ex :
1921
1983
if isinstance (ex , GatewayConnectionError ):
1922
- return JSONResponse (content = {"message" : "Unable to connect to gateway" }, status_code = 502 )
1984
+ return JSONResponse (content = {"message" : "Unable to connect to gateway" }, status_code = status . HTTP_503_SERVICE_UNAVAILABLE )
1923
1985
if isinstance (ex , ValueError ):
1924
- return JSONResponse (content = {"message" : "Unable to process input" }, status_code = 400 )
1986
+ return JSONResponse (content = {"message" : "Unable to process input" }, status_code = status . HTTP_400_BAD_REQUEST )
1925
1987
if isinstance (ex , GatewayNameConflictError ):
1926
- return JSONResponse (content = {"message" : "Gateway name already exists" }, status_code = 400 )
1988
+ return JSONResponse (content = {"message" : "Gateway name already exists" }, status_code = status . HTTP_409_CONFLICT )
1927
1989
if isinstance (ex , RuntimeError ):
1928
- return JSONResponse (content = {"message" : "Error during execution" }, status_code = 500 )
1990
+ return JSONResponse (content = {"message" : "Error during execution" }, status_code = status . HTTP_500_INTERNAL_SERVER_ERROR )
1929
1991
if isinstance (ex , ValidationError ):
1930
- return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = 422 )
1992
+ return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = status . HTTP_422_UNPROCESSABLE_ENTITY )
1931
1993
if isinstance (ex , IntegrityError ):
1932
- return JSONResponse (status_code = 409 , content = ErrorFormatter .format_database_error (ex ))
1933
- return JSONResponse (content = {"message" : "Unexpected error" }, status_code = 500 )
1994
+ return JSONResponse (status_code = status . HTTP_409_CONFLICT , content = ErrorFormatter .format_database_error (ex ))
1995
+ return JSONResponse (content = {"message" : "Unexpected error" }, status_code = status . HTTP_500_INTERNAL_SERVER_ERROR )
1934
1996
1935
1997
1936
1998
@gateway_router .get ("/{gateway_id}" , response_model = GatewayRead )
@@ -1970,7 +2032,24 @@ async def update_gateway(
1970
2032
Updated gateway.
1971
2033
"""
1972
2034
logger .debug (f"User '{ user } ' requested update on gateway { gateway_id } with data={ gateway } " )
1973
- return await gateway_service .update_gateway (db , gateway_id , gateway )
2035
+ try :
2036
+ return await gateway_service .update_gateway (db , gateway_id , gateway )
2037
+ except Exception as ex :
2038
+ if isinstance (ex , GatewayNotFoundError ):
2039
+ return JSONResponse (content = {"message" : "Gateway not found" }, status_code = status .HTTP_404_NOT_FOUND )
2040
+ if isinstance (ex , GatewayConnectionError ):
2041
+ return JSONResponse (content = {"message" : "Unable to connect to gateway" }, status_code = status .HTTP_503_SERVICE_UNAVAILABLE )
2042
+ if isinstance (ex , ValueError ):
2043
+ return JSONResponse (content = {"message" : "Unable to process input" }, status_code = status .HTTP_400_BAD_REQUEST )
2044
+ if isinstance (ex , GatewayNameConflictError ):
2045
+ return JSONResponse (content = {"message" : "Gateway name already exists" }, status_code = status .HTTP_409_CONFLICT )
2046
+ if isinstance (ex , RuntimeError ):
2047
+ return JSONResponse (content = {"message" : "Error during execution" }, status_code = status .HTTP_500_INTERNAL_SERVER_ERROR )
2048
+ if isinstance (ex , ValidationError ):
2049
+ return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = status .HTTP_422_UNPROCESSABLE_ENTITY )
2050
+ if isinstance (ex , IntegrityError ):
2051
+ return JSONResponse (status_code = status .HTTP_409_CONFLICT , content = ErrorFormatter .format_database_error (ex ))
2052
+ return JSONResponse (content = {"message" : "Unexpected error" }, status_code = status .HTTP_500_INTERNAL_SERVER_ERROR )
1974
2053
1975
2054
1976
2055
@gateway_router .delete ("/{gateway_id}" )
0 commit comments