aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2022-02-25 17:51:01 +0000
committerubq323 <ubq323@ubq323.website>2022-02-25 18:16:25 +0000
commitdb885a449440a187e9dd476d07b799e5cd479cd4 (patch)
tree38170a92d52a6bcd5669e413564da629fb656aea
parent84ab4edfd7257683665c6908eaa951297d881b72 (diff)
add better error handling to webhook sending
this fixes the bug where you get a 500 when trying to send a webhook about a thing with a very long title, for example
-rw-r--r--apioforum/webhooks.py145
1 files changed, 77 insertions, 68 deletions
diff --git a/apioforum/webhooks.py b/apioforum/webhooks.py
index 01f539e..a9b3a72 100644
--- a/apioforum/webhooks.py
+++ b/apioforum/webhooks.py
@@ -19,8 +19,9 @@ def webhook_type(t):
return inner
class WebhookType(abc.ABC):
- def __init__(self, url):
+ def __init__(self, url, wh_id):
self.url = url
+ self.wh_id = wh_id
@abc.abstractmethod
def on_new_thread(self,thread):
@@ -40,15 +41,23 @@ def get_webhooks(forum_id):
print(f"unknown webhook type {wh_type}")
continue
wh_url = wh['url']
- wo = webhook_types[wh_type](wh_url)
+ wo = webhook_types[wh_type](wh_url, wh['id'])
yield wo
def do_webhooks_thread(forum_id,thread):
for wh in get_webhooks(forum_id):
- wh.on_new_thread(thread)
+ try:
+ wh.on_new_thread(thread)
+ except:
+ # should probably log the error somewhere
+ flash(f"error executing webhook with id {wh.wh_id}")
+
def do_webhooks_post(forum_id,post):
for wh in get_webhooks(forum_id):
- wh.on_new_post(post)
+ try:
+ wh.on_new_post(post)
+ except:
+ flash(f"error executing webhook with id {wh.wh_id}")
@webhook_type("fake")
class FakeWebhook(WebhookType):
@@ -83,71 +92,71 @@ class DiscordWebhook(WebhookType):
return {"name":name,"value":value,"inline":True}
def on_new_thread(self,thread):
- f = self.field
- db = get_db()
- forum = db.execute("select * from forums where id = ?",(thread['forum'],)).fetchone()
- username = thread['creator']
- userpage = url_for('user.view_user',username=username,_external=True)
-
- forumpage = url_for('forum.view_forum',forum_id=forum['id'],_external=True)
-
- post = db.execute("select * from posts where thread = ? order by id asc limit 1",(thread['id'],)).fetchone()
-
- payload = {
- "username":"apioforum",
- "avatar_url":"https://d.gh0.pw/lib/exe/fetch.php?media=wiki:logo.png",
- "embeds":[
- {
- "title":"new thread: "+thread['title'],
- "description":abridge_post(post['content']),
- "url": url_for('thread.view_thread',thread_id=thread['id'],_external=True),
- "color": 0xff00ff,
- "fields":[
- f('author',f"[{username}]({userpage})"),
- f('forum',f"[{forum['name']}]({forumpage})"),
- ],
- "footer":{
- "text":thread['created'].isoformat(' '),
- },
- },
- ],
- }
- self.send(payload)
+ f = self.field
+ db = get_db()
+ forum = db.execute("select * from forums where id = ?",(thread['forum'],)).fetchone()
+ username = thread['creator']
+ userpage = url_for('user.view_user',username=username,_external=True)
+
+ forumpage = url_for('forum.view_forum',forum_id=forum['id'],_external=True)
+
+ post = db.execute("select * from posts where thread = ? order by id asc limit 1",(thread['id'],)).fetchone()
+
+ payload = {
+ "username":"apioforum",
+ "avatar_url":"https://d.gh0.pw/lib/exe/fetch.php?media=wiki:logo.png",
+ "embeds":[
+ {
+ "title":"new thread: "+thread['title'],
+ "description":abridge_post(post['content']),
+ "url": url_for('thread.view_thread',thread_id=thread['id'],_external=True),
+ "color": 0xff00ff,
+ "fields":[
+ f('author',f"[{username}]({userpage})"),
+ f('forum',f"[{forum['name']}]({forumpage})"),
+ ],
+ "footer":{
+ "text":thread['created'].isoformat(' '),
+ },
+ },
+ ],
+ }
+ self.send(payload)
def on_new_post(self,post):
- from .thread import post_jump
- f = self.field
- db = get_db()
-
- thread = db.execute("select * from threads where id = ?",(post['thread'],)).fetchone()
- threadpage = url_for('thread.view_thread',thread_id=thread['id'],_external=True)
-
- forum = db.execute("select * from forums where id = ?",(thread['forum'],)).fetchone()
- forumpage = url_for('forum.view_forum',forum_id=forum['id'],_external=True)
-
- username = post['author']
- userpage = url_for('user.view_user',username=username,_external=True)
-
- payload = {
- "username":"apioforum",
- "avatar_url":"https://d.gh0.pw/lib/exe/fetch.php?media=wiki:logo.png",
- "embeds":[
- {
- "title":"re: "+thread['title'],
- "description":abridge_post(post['content']),
- "url": post_jump(post['id'],external=True),
- "color": 0x00ffff,
- "fields":[
- f('author',f"[{username}]({userpage})"),
- f('thread',f"[{thread['title']}]({threadpage})"),
- f('forum',f"[{forum['name']}]({forumpage})"),
- ],
- "footer":{
- "text":post['created'].isoformat(' '),
- },
- },
- ],
- }
- self.send(payload)
+ from .thread import post_jump
+ f = self.field
+ db = get_db()
+
+ thread = db.execute("select * from threads where id = ?",(post['thread'],)).fetchone()
+ threadpage = url_for('thread.view_thread',thread_id=thread['id'],_external=True)
+
+ forum = db.execute("select * from forums where id = ?",(thread['forum'],)).fetchone()
+ forumpage = url_for('forum.view_forum',forum_id=forum['id'],_external=True)
+
+ username = post['author']
+ userpage = url_for('user.view_user',username=username,_external=True)
+
+ payload = {
+ "username":"apioforum",
+ "avatar_url":"https://d.gh0.pw/lib/exe/fetch.php?media=wiki:logo.png",
+ "embeds":[
+ {
+ "title":"re: "+thread['title'],
+ "description":abridge_post(post['content']),
+ "url": post_jump(post['id'],external=True),
+ "color": 0x00ffff,
+ "fields":[
+ f('author',f"[{username}]({userpage})"),
+ f('thread',f"[{thread['title']}]({threadpage})"),
+ f('forum',f"[{forum['name']}]({forumpage})"),
+ ],
+ "footer":{
+ "text":post['created'].isoformat(' '),
+ },
+ },
+ ],
+ }
+ self.send(payload)