aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2021-12-23 15:51:06 -0800
committerBill Wendling <isanbard@gmail.com>2021-12-24 01:27:30 -0600
commitc79578a8f822ffa13183a89f8c32bc30a6bf7943 (patch)
treefae219a827b3f58178b3eac8bd69ded3bfedd890
parentd017a9ebf2f973f4aad4d1a5ae83680731a60b72 (diff)
downloadyapf-c79578a8f822ffa13183a89f8c32bc30a6bf7943.tar.gz
Unify the YAPF exceptions
There are many potential exceptions that can be generated. Unify them into a single YapfError with a standardized error message format.
-rw-r--r--yapf/__init__.py13
-rw-r--r--yapf/yapflib/errors.py18
-rw-r--r--yapf/yapflib/yapf_api.py20
-rw-r--r--yapftests/main_test.py4
4 files changed, 39 insertions, 16 deletions
diff --git a/yapf/__init__.py b/yapf/__init__.py
index 4fe2ddf..21eaa01 100644
--- a/yapf/__init__.py
+++ b/yapf/__init__.py
@@ -109,8 +109,8 @@ def main(argv):
style_config=style_config,
lines=lines,
verify=args.verify)
- except tokenize.TokenError as e:
- raise errors.YapfError('%s:%s' % (e.args[1][0], e.args[0]))
+ except Exception as e:
+ raise errors.YapfError(errors.FormatErrorMsg(e))
file_resources.WriteReformattedCode('<stdout>', reformatted_source)
return 0
@@ -123,7 +123,7 @@ def main(argv):
(args.exclude or []) +
exclude_patterns_from_ignore_file)
if not files:
- raise errors.YapfError('Input filenames did not match any python files')
+ raise errors.YapfError('input filenames did not match any python files')
changed = FormatFiles(
files,
@@ -234,11 +234,10 @@ def _FormatFile(filename,
print_diff=print_diff,
verify=verify,
logger=logging.warning)
- except tokenize.TokenError as e:
- raise errors.YapfError('%s:%s:%s' % (filename, e.args[1][0], e.args[0]))
- except SyntaxError as e:
- e.filename = filename
+ except errors.YapfError:
raise
+ except Exception as e:
+ raise errors.YapfError(errors.FormatErrorMsg(e))
if not in_place and not quiet and reformatted_code:
file_resources.WriteReformattedCode(filename, reformatted_code, encoding,
diff --git a/yapf/yapflib/errors.py b/yapf/yapflib/errors.py
index 3946275..5726ff1 100644
--- a/yapf/yapflib/errors.py
+++ b/yapf/yapflib/errors.py
@@ -11,7 +11,23 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-"""YAPF error object."""
+"""YAPF error objects."""
+
+
+def FormatErrorMsg(e):
+ """Convert an exception into a standard format.
+
+ The standard error message format is:
+
+ <filename>:<lineno>:<column>: <msg>
+
+ Arguments:
+ e: An exception.
+
+ Returns:
+ A properly formatted error message string.
+ """
+ return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)
class YapfError(Exception):
diff --git a/yapf/yapflib/yapf_api.py b/yapf/yapflib/yapf_api.py
index 0ef2a1e..ac0466e 100644
--- a/yapf/yapflib/yapf_api.py
+++ b/yapf/yapflib/yapf_api.py
@@ -37,10 +37,12 @@ import re
import sys
from lib2to3.pgen2 import parse
+from lib2to3.pgen2 import tokenize
from yapf.yapflib import blank_line_calculator
from yapf.yapflib import comment_splicer
from yapf.yapflib import continuation_splicer
+from yapf.yapflib import errors
from yapf.yapflib import file_resources
from yapf.yapflib import identify_container
from yapf.yapflib import py3compat
@@ -179,8 +181,12 @@ def FormatCode(unformatted_source,
"""
try:
tree = pytree_utils.ParseCodeToTree(unformatted_source)
- except parse.ParseError as e:
- e.msg = filename + ': ' + e.msg
+ except tokenize.TokenError as e:
+ e.msg = e.args[0]
+ e.args = (e.msg, (filename, e.args[1][0], e.args[1][1]))
+ raise
+ except Exception as e:
+ e.args = (e.args[0], (filename, e.args[1][1], e.args[1][2], e.args[1][3]))
raise
reformatted_source = FormatTree(
@@ -236,15 +242,17 @@ def ReadFile(filename, logger=None):
line_ending = file_resources.LineEnding(lines)
source = '\n'.join(line.rstrip('\r\n') for line in lines) + '\n'
return source, line_ending, encoding
- except IOError as err: # pragma: no cover
+ except IOError as e: # pragma: no cover
if logger:
- logger(err)
+ logger(e)
+ e.args = (e.args[0], (filename, e.args[1][1], e.args[1][2], e.args[1][3]))
raise
- except UnicodeDecodeError as err: # pragma: no cover
+ except UnicodeDecodeError as e: # pragma: no cover
if logger:
logger('Could not parse %s! Consider excluding this file with --exclude.',
filename)
- logger(err)
+ logger(e)
+ e.args = (e.args[0], (filename, e.args[1][1], e.args[1][2], e.args[1][3]))
raise
diff --git a/yapftests/main_test.py b/yapftests/main_test.py
index a9069b0..dd89753 100644
--- a/yapftests/main_test.py
+++ b/yapftests/main_test.py
@@ -89,7 +89,7 @@ class RunMainTest(yapf_test_helper.YAPFTest):
def testShouldHandleYapfError(self):
"""run_main should handle YapfError and sys.exit(1)."""
- expected_message = 'yapf: Input filenames did not match any python files\n'
+ expected_message = 'yapf: input filenames did not match any python files\n'
sys.argv = ['yapf', 'foo.c']
with captured_output() as (out, err):
with self.assertRaises(SystemExit):
@@ -126,7 +126,7 @@ class MainTest(yapf_test_helper.YAPFTest):
bad_syntax = ' a = 1\n'
with patched_input(bad_syntax):
with captured_output() as (_, _):
- with self.assertRaisesRegex(SyntaxError, 'unexpected indent'):
+ with self.assertRaisesRegex(yapf.errors.YapfError, 'unexpected indent'):
yapf.main([])
def testHelp(self):