Skip to content

Commit daa412a

Browse files
authored
Merge pull request #129 from GabrielSalla/improve-syntax-error-validation-message
Improve monitor validation message for syntax error
2 parents 1cdda2a + 48b7b41 commit daa412a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/components/http_server/monitor_routes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,12 @@ async def monitor_validate(request: Request) -> Response:
191191
}
192192
return web.json_response(error_response, status=400)
193193
except SyntaxError as e:
194-
error_response = {"status": "error", "error": f"'{e.args[1][3]}' {e.msg}"}
194+
syntax_error_line = e.lineno
195+
syntax_error_code = e.args[1][3].strip()
196+
error_response = {
197+
"status": "error",
198+
"error": f"Syntax error at line {syntax_error_line}: {syntax_error_code}",
199+
}
195200
return web.json_response(error_response, status=400)
196201
except Exception as e:
197202
error_response = {"status": "error", "error": str(e)}

tests/components/http_server/test_monitor_routes.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,26 @@ async def test_monitor_validate_check_fail():
380380
}
381381

382382

383-
async def test_monitor_validate_syntax_error(mocker):
384-
"""The 'monitor validate' route should return an error if the provided module code has any
385-
errors"""
383+
@pytest.mark.parametrize(
384+
"monitor_code, expected_error",
385+
[
386+
("print('a\n", "Syntax error at line 1: print('a"),
387+
("print('a')\n def f(): ...", "Syntax error at line 2: def f(): ..."),
388+
],
389+
)
390+
async def test_monitor_validate_syntax_error(mocker, monitor_code, expected_error):
391+
"""The 'monitor validate' route should return an error if the provided module code has a syntax
392+
error"""
386393
request_payload = {
387-
"monitor_code": "print('a",
394+
"monitor_code": monitor_code,
388395
}
389396

390397
url = BASE_URL + "/validate"
391398
async with aiohttp.ClientSession() as session:
392399
async with session.post(url, json=request_payload) as response:
393400
assert await response.json() == {
394401
"status": "error",
395-
"error": "'print('a' unterminated string literal (detected at line 1)",
402+
"error": expected_error,
396403
}
397404

398405

0 commit comments

Comments
 (0)