Skip to content

Commit d2060d9

Browse files
committed
improve exception handling
1 parent 6a038da commit d2060d9

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

sync_community_team/sync_teams.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
#!/usr/bin/env python3
22

3+
# Standard library
4+
import sys
5+
import traceback
6+
37
# First-party/Local
48
from get_community_team_data import get_community_team_data
59
from set_codeowners import create_codeowners_for_data
610
from set_teams_on_github import create_teams_for_data
711

8-
if __name__ == "__main__":
12+
13+
class ScriptError(Exception):
14+
def __init__(self, message, code=None):
15+
self.code = code if code else 1
16+
message = "({}) {}".format(self.code, message)
17+
super(ScriptError, self).__init__(message)
18+
19+
20+
def main():
921
create_teams_for_data(get_community_team_data())
1022
create_codeowners_for_data(get_community_team_data())
23+
24+
25+
if __name__ == "__main__":
26+
try:
27+
main()
28+
except SystemExit as e:
29+
sys.exit(e.code)
30+
except KeyboardInterrupt:
31+
print("INFO (130) Halted via KeyboardInterrupt.", file=sys.stderr)
32+
sys.exit(130)
33+
except ScriptError:
34+
error_type, error_value, error_traceback = sys.exc_info()
35+
print("ERROR {}".format(error_value), file=sys.stderr)
36+
sys.exit(error_value.code)
37+
except Exception:
38+
print("ERROR (1) Unhandled exception:", file=sys.stderr)
39+
print(traceback.print_exc(), file=sys.stderr)
40+
sys.exit(1)

0 commit comments

Comments
 (0)