introduce exception_handle decorator
make try/except block more clean
This commit is contained in:
parent
07dfa0b170
commit
52556d5e48
2 changed files with 57 additions and 24 deletions
|
@ -27,6 +27,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))
|
||||||
from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdns
|
from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdns
|
||||||
|
|
||||||
|
|
||||||
|
@shell.exception_handle(self_=False, exit_code=1)
|
||||||
def main():
|
def main():
|
||||||
shell.check_python()
|
shell.check_python()
|
||||||
|
|
||||||
|
@ -37,10 +38,8 @@ def main():
|
||||||
os.chdir(p)
|
os.chdir(p)
|
||||||
|
|
||||||
config = shell.get_config(True)
|
config = shell.get_config(True)
|
||||||
|
|
||||||
daemon.daemon_exec(config)
|
daemon.daemon_exec(config)
|
||||||
|
|
||||||
try:
|
|
||||||
logging.info("starting local at %s:%d" %
|
logging.info("starting local at %s:%d" %
|
||||||
(config['local_address'], config['local_port']))
|
(config['local_address'], config['local_port']))
|
||||||
|
|
||||||
|
@ -64,9 +63,6 @@ def main():
|
||||||
|
|
||||||
daemon.set_user(config.get('user', None))
|
daemon.set_user(config.get('user', None))
|
||||||
loop.run()
|
loop.run()
|
||||||
except Exception as e:
|
|
||||||
shell.print_exception(e)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -23,6 +23,9 @@ import json
|
||||||
import sys
|
import sys
|
||||||
import getopt
|
import getopt
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
from shadowsocks.common import to_bytes, to_str, IPNetwork
|
from shadowsocks.common import to_bytes, to_str, IPNetwork
|
||||||
from shadowsocks import encrypt
|
from shadowsocks import encrypt
|
||||||
|
|
||||||
|
@ -53,6 +56,40 @@ def print_exception(e):
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
|
def exception_handle(self_, err_msg=None, exit_code=None):
|
||||||
|
"""
|
||||||
|
:param self_: if function passes self as first arg
|
||||||
|
:param err_msg:
|
||||||
|
:param exit_code:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
def process_exception(e):
|
||||||
|
print_exception(e)
|
||||||
|
if err_msg:
|
||||||
|
logging.error(err_msg)
|
||||||
|
if exit_code:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
def decorator(func):
|
||||||
|
if self_:
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(self, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
func(self, *args, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
process_exception(e)
|
||||||
|
else:
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
func(*args, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
process_exception(e)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
def print_shadowsocks():
|
def print_shadowsocks():
|
||||||
version = ''
|
version = ''
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue