Skip to content

Commit

Permalink
Improved try-except blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SoSaymon committed Jun 8, 2023
1 parent 02e53c7 commit 608abf1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions wordlist_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ def load_wordlist(file: str) -> list[str]:
try:
with open(file, 'r') as f:
return f.readlines()
except FileNotFoundError:
print(f"File not found {file} {FileNotFoundError}")
except IOError:
print(f"IOError {IOError}")
except FileNotFoundError as e:
raise FileNotFoundError(f"File not found: {file}") from e
except IOError as e:
raise IOError(f"IOError occurred {e}") from e
except Exception as e:
print(f"{e}")
raise Exception(f"An error occurred: {e}") from e
finally:
f.close()
return []


def make_dict(line: str) -> dict[str, str]:
Expand All @@ -29,5 +29,18 @@ def make_wordlist(wordlist: list[str]) -> list[dict[str, str]]:


def wordlist_maker(filename: str) -> list[dict[str, str]]:
tmp = load_wordlist(filename)
return make_wordlist(tmp)
try:
wordlist = load_wordlist(filename)
except FileNotFoundError as e:
print(f"File not found: {filename}")
print("Using default wordlist")
wordlist = load_wordlist("wordlist.txt")
except IOError as e:
print(f"IOError occurred {e}")
print("Using default wordlist")
wordlist = load_wordlist("wordlist.txt")
except Exception as e:
print(f"An error occurred: {e}")
print("Restart the program")
exit(1)
return make_wordlist(wordlist)

0 comments on commit 608abf1

Please sign in to comment.