Skip to content

Commit d180078

Browse files
authored
Updated error handling for bad requests in test gateway (#435)
* Updated error handling for bad requests & urls Signed-off-by: Reeve Barreto <[email protected]> * lint-web admin.js Signed-off-by: Reeve Barreto <[email protected]> * updated correct latency response at the endpoint Signed-off-by: Reeve Barreto <[email protected]> * Update throw error Signed-off-by: Reeve Barreto <[email protected]> * flake8 Signed-off-by: Reeve Barreto <[email protected]> --------- Signed-off-by: Reeve Barreto <[email protected]>
1 parent a30900a commit d180078

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

mcpgateway/admin.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,24 +1366,23 @@ async def admin_test_gateway(request: GatewayTestRequest, user: str = Depends(re
13661366
13671367
Returns:
13681368
GatewayTestResponse: The response from the gateway, including status code, latency, and body
1369-
1370-
Raises:
1371-
HTTPException: If the gateway request fails (e.g., connection error, timeout).
13721369
"""
13731370
full_url = str(request.base_url).rstrip("/") + "/" + request.path.lstrip("/")
1371+
full_url = full_url.rstrip("/")
13741372
logger.debug(f"User {user} testing server at {request.base_url}.")
13751373
try:
1374+
start_time = time.monotonic()
13761375
async with httpx.AsyncClient(timeout=settings.federation_timeout, verify=not settings.skip_ssl_verify) as client:
1377-
start_time = time.monotonic()
13781376
response = await client.request(method=request.method.upper(), url=full_url, headers=request.headers, json=request.body)
1379-
latency_ms = int((time.monotonic() - start_time) * 1000)
1377+
latency_ms = int((time.monotonic() - start_time) * 1000)
13801378
try:
13811379
response_body: Union[dict, str] = response.json()
13821380
except json.JSONDecodeError:
1383-
response_body = response.text
1381+
response_body = {"details": response.text}
13841382

13851383
return GatewayTestResponse(status_code=response.status_code, latency_ms=latency_ms, body=response_body)
13861384

13871385
except httpx.RequestError as e:
13881386
logger.warning(f"Gateway test failed: {e}")
1389-
raise HTTPException(status_code=502, detail=f"Request failed: {str(e)}")
1387+
latency_ms = int((time.monotonic() - start_time) * 1000)
1388+
return GatewayTestResponse(status_code=502, latency_ms=latency_ms, body={"error": "Request failed", "details": str(e)})

mcpgateway/static/admin.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,7 @@ async function handleGatewayTestSubmit(e) {
33193319
const loading = safeGetElement("gateway-test-loading");
33203320
const responseDiv = safeGetElement("gateway-test-response-json");
33213321
const resultDiv = safeGetElement("gateway-test-result");
3322+
const testButton = safeGetElement("gateway-test-submit");
33223323

33233324
try {
33243325
// Show loading
@@ -3328,6 +3329,10 @@ async function handleGatewayTestSubmit(e) {
33283329
if (resultDiv) {
33293330
resultDiv.classList.add("hidden");
33303331
}
3332+
if (testButton) {
3333+
testButton.disabled = true;
3334+
testButton.textContent = "Testing...";
3335+
}
33313336

33323337
const form = e.target;
33333338
const url = form.action;
@@ -3393,31 +3398,32 @@ async function handleGatewayTestSubmit(e) {
33933398

33943399
const result = await response.json();
33953400

3396-
if (responseDiv) {
3397-
// Display result safely
3398-
responseDiv.innerHTML = `
3399-
<div class="alert alert-success">
3400-
<h4>✅ Connection Successful</h4>
3401-
<p><strong>Status Code:</strong> ${result.statusCode}</p>
3402-
<p><strong>Response Time:</strong> ${result.latencyMs}ms</p>
3403-
${
3404-
result.body
3405-
? `<details>
3406-
<summary class='cursor-pointer'>Response Body</summary>
3407-
<pre class="text-sm px-4 max-h-96 dark:bg-gray-800 dark:text-gray-100 overflow-auto">${JSON.stringify(result.body, null, 2)}</pre>
3408-
</details>`
3409-
: ""
3410-
}
3411-
</div>
3412-
`;
3413-
} else {
3414-
responseDiv.innerHTML = `
3415-
<div class="alert alert-error">
3416-
<h4>❌ Connection Failed</h4>
3417-
<p>${result.error || "Unable to connect to the server"}</p>
3418-
</div>
3401+
const isSuccess =
3402+
result.statusCode &&
3403+
result.statusCode >= 200 &&
3404+
result.statusCode < 300;
3405+
3406+
const alertType = isSuccess ? "success" : "error";
3407+
const icon = isSuccess ? "✅" : "❌";
3408+
const title = isSuccess ? "Connection Successful" : "Connection Failed";
3409+
const statusCode = result.statusCode || "Unknown";
3410+
const latency =
3411+
result.latencyMs != null ? `${result.latencyMs}ms` : "NA";
3412+
const body = result.body
3413+
? `<details open>
3414+
<summary class='cursor-pointer'><strong>Response Body</strong></summary>
3415+
<pre class="text-sm px-4 max-h-96 dark:bg-gray-800 dark:text-gray-100 overflow-auto">${JSON.stringify(result.body, null, 2)}</pre>
3416+
</details>`
3417+
: "";
3418+
3419+
responseDiv.innerHTML = `
3420+
<div class="alert alert-${alertType}">
3421+
<h4><strong>${icon} ${title}</strong></h4>
3422+
<p><strong>Status Code:</strong> ${statusCode}</p>
3423+
<p><strong>Response Time:</strong> ${latency}</p>
3424+
${body}
3425+
</div>
34193426
`;
3420-
}
34213427
} catch (error) {
34223428
console.error("Gateway test error:", error);
34233429
if (responseDiv) {
@@ -3434,6 +3440,9 @@ async function handleGatewayTestSubmit(e) {
34343440
if (resultDiv) {
34353441
resultDiv.classList.remove("hidden");
34363442
}
3443+
3444+
testButton.disabled = false;
3445+
testButton.textContent = "Test";
34373446
}
34383447
}
34393448

mcpgateway/templates/admin.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,11 +1742,11 @@ <h2 class="text-xl font-bold mb-4 text-gray-800 dark:text-gray-100">Test Gateway
17421742
<form id="gateway-test-form" class="space-y-4">
17431743
<div>
17441744
<label for="gateway-test-url" class="block text-sm font-medium text-gray-700">Server URL</label>
1745-
<input name="url" type="text" id="gateway-test-url" class="mt-1 block w-full rounded-md shadow-sm p-1" />
1745+
<input required name="url" type="text" id="gateway-test-url" class="mt-1 block w-full rounded-md shadow-sm p-1" />
17461746
</div>
17471747
<div>
17481748
<label for="gateway-test-method" class="block text-sm font-medium text-gray-700">Method</label>
1749-
<select name="method" id="gateway-test-method" class="mt-1 block w-full rounded-md shadow-sm p-1">
1749+
<select required name="method" id="gateway-test-method" class="mt-1 block w-full rounded-md shadow-sm p-1">
17501750
<option>GET</option>
17511751
<option>POST</option>
17521752
<option>PUT</option>
@@ -1772,7 +1772,7 @@ <h2 class="text-xl font-bold mb-4 text-gray-800 dark:text-gray-100">Test Gateway
17721772
<div class="flex">
17731773
<button type="submit" id="gateway-test-submit"
17741774
class="w-full text-center px-3 py-1 bg-indigo-600 text-white rounded hover:bg-indigo-700">
1775-
Send
1775+
Test
17761776
</button>
17771777
<p class="p-1"></p>
17781778
<button id="gateway-test-close" type="button"

0 commit comments

Comments
 (0)