Skip to content

Commit

Permalink
Add more logging for easier diagnosis of failures
Browse files Browse the repository at this point in the history
When messages don't have a "ts" or "text" key, the full data and the
filename it came from will now be logged out.

Also logs all orphaned threaded messages that are ignored.
  • Loading branch information
pR0Ps committed Feb 24, 2024
1 parent 1702ca4 commit acbb333
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions slack_to_discord/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def slack_filedata(f):
}


def slack_channel_messages(d, channel_name, users, emoji_map, pins):
def slack_channel_messages(datadir, channel_name, users, emoji_map, pins):
def mention_repl(m):
type_ = m.group(1)
target = m.group(2)
Expand All @@ -159,7 +159,18 @@ def mention_repl(m):
return "`@{}`".format(target)
return m.group(0)

channel_dir = os.path.join(d, channel_name)
def getkey(f, d, k):
try:
return d[k]
except KeyError:
relpath = os.path.relpath(f, start=datadir)
__log__.critical(
"The following message from file '%s' does not contain key '%s':\n%r",
relpath, k, d
)
raise

channel_dir = os.path.join(datadir, channel_name)
if not os.path.isdir(channel_dir):
__log__.error("Data for channel '#%s' not found in export", channel_name)

Expand All @@ -168,8 +179,8 @@ def mention_repl(m):
for file in sorted(glob.glob(os.path.join(channel_dir, "*-*-*.json"))):
with open(file, "rb") as fp:
data = json.load(fp)
for d in sorted(data, key=lambda x: x["ts"]):
text = d["text"]
for d in sorted(data, key=lambda x: getkey(file, x, "ts")):
text = getkey(file, d, "text")
text = MENTION_RE.sub(mention_repl, text)
text = LINK_RE.sub(lambda x: x.group(1), text)
text = emoji_replace(text, emoji_map)
Expand Down Expand Up @@ -257,6 +268,7 @@ def mention_repl(m):
if thread_ts != ts:
if thread_ts not in messages:
# Orphan thread message - skip it
__log__.debug("Orphan threaded message - skipping it:\n%r", msg)
continue
messages[thread_ts]["replies"][ts] = msg
else:
Expand Down

0 comments on commit acbb333

Please sign in to comment.