2015-12-04 02:03:07 +03:00
|
|
|
# errorredirect.py
|
|
|
|
#
|
|
|
|
# Copyright 2015 Facebook, Inc.
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
"""redirect error message
|
|
|
|
|
|
|
|
Redirect error message, the stack trace, of an uncaught exception to
|
2016-04-04 22:48:01 +03:00
|
|
|
a custom shell script. This is useful for further handling the error,
|
2015-12-04 02:03:07 +03:00
|
|
|
for example posting it to a support group and logging it somewhere.
|
|
|
|
|
|
|
|
The config option errorredirect.script is the shell script to execute.
|
|
|
|
If it's empty, the extension will do nothing and fallback to the old
|
|
|
|
behavior.
|
|
|
|
|
2015-12-17 23:28:31 +03:00
|
|
|
Two environment variables are set: TRACE is the stack trace, which
|
|
|
|
is the same as piped content. WARNING is the warning message, which
|
|
|
|
usually contains contact message and software versions, etc.
|
2015-12-04 02:03:07 +03:00
|
|
|
|
|
|
|
Examples::
|
|
|
|
|
|
|
|
[errorredirect]
|
|
|
|
script = tee hgerr.log && echo 'Error written to hgerr.log'
|
|
|
|
|
|
|
|
[errorredirect]
|
2015-12-17 23:28:31 +03:00
|
|
|
script = echo "$WARNING$TRACE" >&2
|
|
|
|
|
|
|
|
[errorredirect]
|
|
|
|
script = (echo "$WARNING"; cat) | cat >&2
|
2015-12-04 02:03:07 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def wrapui(ui):
|
|
|
|
class errorredirectui(ui.__class__):
|
2015-12-17 23:28:31 +03:00
|
|
|
def errorredirect(self, content, env):
|
2015-12-04 02:03:07 +03:00
|
|
|
script = self.config('errorredirect', 'script')
|
|
|
|
if not script:
|
|
|
|
return
|
|
|
|
p = subprocess.Popen(script, shell=True, stdin=subprocess.PIPE,
|
|
|
|
env=dict(os.environ.items() + env.items()))
|
|
|
|
p.communicate(content)
|
|
|
|
# prevent hg from printing the stack trace
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def log(self, event, *msg, **opts):
|
|
|
|
if event == 'commandexception':
|
2015-12-17 23:28:31 +03:00
|
|
|
warning, traceback = msg[-2:]
|
|
|
|
self.errorredirect(traceback,
|
|
|
|
{'WARNING': warning, 'TRACE': traceback})
|
2015-12-04 02:03:07 +03:00
|
|
|
return super(errorredirectui, self).log(event, *msg, **opts)
|
|
|
|
|
|
|
|
ui.__class__ = errorredirectui
|
|
|
|
|
|
|
|
def uisetup(ui):
|
|
|
|
wrapui(ui)
|