Skip to content

Commit

Permalink
python: dbh()で例外吐いたときに余計な例外吐くのを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
misodengaku committed Nov 20, 2023
1 parent a82edde commit 3f022c6
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions webapp/python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def get_tag_handler() -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(tags), OK


Expand Down Expand Up @@ -120,7 +121,8 @@ def get_streamer_theme_handler(username: str) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(theme), OK


Expand Down Expand Up @@ -157,7 +159,7 @@ def reserve_livestream_handler() -> tuple[dict[str, Any], int]:
sql = "SELECT * FROM reservation_slots WHERE start_at >= %s AND end_at <= %s FOR UPDATE"
c.execute(sql, [int(req["start_at"]), int(req["end_at"])])
slots = c.fetchall()
if not slots:
if slots is None:
app.logger.error("予約枠一覧取得でエラー発生")
raise HttpException(
"failed to get reservation_slots",
Expand Down Expand Up @@ -229,7 +231,8 @@ def reserve_livestream_handler() -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

return asdict(livestream), CREATED

Expand Down Expand Up @@ -318,7 +321,8 @@ def search_livestreams_handler() -> tuple[list[dict[str, Any]], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return livestreams, OK


Expand Down Expand Up @@ -374,7 +378,8 @@ def get_my_livestreams_handler() -> tuple[list[dict[str, Any]], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return livestreams, OK


Expand Down Expand Up @@ -438,7 +443,8 @@ def get_user_livestreams_handler(username) -> tuple[list[dict[str, Any]], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return livestreams, OK


Expand Down Expand Up @@ -473,7 +479,8 @@ def get_livestream_handler(livestream_id: int) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(livestream), OK


Expand Down Expand Up @@ -524,7 +531,8 @@ def get_livecomments_handler(livestream_id: int) -> tuple[list[dict[str, Any]],
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return livecomments, OK


Expand Down Expand Up @@ -563,7 +571,7 @@ def post_livecomment_handler(livestream_id: int) -> tuple[dict[str, Any], int]:
if not hit_spam:
raise HttpException("failed to get hitspam", INTERNAL_SERVER_ERROR)
hit_spam = hit_spam["COUNT(*)"]
app.logger.info(f"[hitSpam={hit_spam}] comment = {req['comment']}")
# app.logger.info(f"[hitSpam={hit_spam}] comment = {req['comment']}")
if hit_spam >= 1:
raise HttpException("このコメントがスパム判定されました", BAD_REQUEST)

Expand All @@ -590,7 +598,8 @@ def post_livecomment_handler(livestream_id: int) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(livecomment), CREATED


Expand Down Expand Up @@ -627,7 +636,8 @@ def post_reaction_handler(livestream_id: int) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(reaction), CREATED


Expand Down Expand Up @@ -677,7 +687,8 @@ def get_reactions_handler(
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return reactions, OK


Expand Down Expand Up @@ -745,7 +756,8 @@ def get_livecomment_reports_handler(
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return reports, OK


Expand Down Expand Up @@ -775,7 +787,8 @@ def get_ngwords(livestream_id: int):
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()


# ライブコメント報告
Expand Down Expand Up @@ -817,7 +830,8 @@ def report_livecomment_handler(
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(report), CREATED


Expand Down Expand Up @@ -902,7 +916,8 @@ def moderate_handler(livestream_id: int) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return {"word_id": word_id}, CREATED


Expand All @@ -929,7 +944,8 @@ def enter_livestream_handler(livestream_id: int) -> tuple[str, int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

return "", OK

Expand All @@ -954,7 +970,8 @@ def exit_livestream_handler(livestream_id: int) -> tuple[str, int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

return "", OK

Expand Down Expand Up @@ -1034,7 +1051,8 @@ def register_handler() -> tuple[dict[str, Any], int]:
except pymysql.err.IntegrityError:
raise HttpException("failed to insert user", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(user), CREATED


Expand Down Expand Up @@ -1086,7 +1104,8 @@ def login_handler() -> tuple[str, int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return "", OK


Expand Down Expand Up @@ -1120,7 +1139,8 @@ def get_me_handler() -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(user), OK


Expand Down Expand Up @@ -1152,7 +1172,8 @@ def get_user_handler(username: str) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(user), OK


Expand Down Expand Up @@ -1237,7 +1258,6 @@ def get_user_statistics_handler(username: str) -> tuple[dict[str, Any], int]:
i = len(ranking) - 1
while i >= 0:
entry = ranking[i]
print(entry, username)
if entry["username"] == username:
break
rank += 1
Expand Down Expand Up @@ -1343,7 +1363,8 @@ def get_user_statistics_handler(username: str) -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

return asdict(statistics), OK

Expand Down Expand Up @@ -1380,7 +1401,8 @@ def get_icon_handler(username: str) -> flask.Response:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

if not image:
return flask.send_file(
Expand Down Expand Up @@ -1426,7 +1448,8 @@ def post_icon_handler() -> tuple[dict[str, Any], int]:
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

return {"id": icon_id}, CREATED

Expand Down Expand Up @@ -1548,7 +1571,8 @@ def get_livestream_statistics_handler(livestream_id: int) -> tuple[dict[str, Any
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()
return asdict(user_statistics), OK


Expand All @@ -1569,7 +1593,8 @@ def get_payment_result():
app.logger.exception(err)
raise HttpException("db error", INTERNAL_SERVER_ERROR)
finally:
conn.close()
if "conn" in locals():
conn.close()

return {"total_tip": total_tip}

Expand Down

0 comments on commit 3f022c6

Please sign in to comment.