Skip to content

Commit f91da3c

Browse files
authored
Remove unused code in Markdown sample (GoogleCloudPlatform#5396)
* Update sample * Update tests
1 parent 68db4c2 commit f91da3c

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

run/markdown-preview/editor/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ def index():
3535
def render_handler():
3636
body = request.get_json()
3737
if not body:
38-
raise Exception("Invalid JSON")
38+
return "Error rendering markdown: Invalid JSON", 400
3939

4040
data = body["data"]
41-
parsed_markdown = render.new_request(data)
42-
return parsed_markdown
41+
try:
42+
parsed_markdown = render.new_request(data)
43+
return parsed_markdown, 200
44+
except Exception as err:
45+
return f"Error rendering markdown: {err}", 500
46+
4347
# [END run_secure_request_do]
4448
# [END cloudrun_secure_request_do]
4549

run/markdown-preview/editor/main_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ def test_render_handler_errors(client):
4040
r = client.get("/render")
4141
assert r.status_code == 405
4242

43-
with pytest.raises(Exception) as e:
44-
client.post("/render", data="**markdown**")
45-
assert "Invalid JSON" in str(e.value)
43+
r = client.post("/render", data="**markdown**")
44+
assert r.status_code == 400
45+
assert "Invalid JSON" in r.data.decode()
4646

4747

4848
def test_missing_upstream_url(client):
4949
del os.environ["EDITOR_UPSTREAM_RENDER_URL"]
50-
with pytest.raises(Exception) as e:
51-
client.post("/render",
50+
r = client.post("/render",
5251
data=json.dumps({"data": "**strong text**"}),
5352
headers={"Content-Type": "application/json"})
54-
assert "EDITOR_UPSTREAM_RENDER_URL missing" in str(e.value)
53+
assert r.status_code == 500
54+
assert "EDITOR_UPSTREAM_RENDER_URL missing" in r.data.decode()

run/markdown-preview/editor/render.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ def new_request(data):
3333
raise Exception("EDITOR_UPSTREAM_RENDER_URL missing")
3434

3535
req = urllib.request.Request(url, data=data.encode())
36-
37-
credentials, project = google.auth.default()
3836
auth_req = google.auth.transport.requests.Request()
3937
target_audience = url
4038

0 commit comments

Comments
 (0)