xref: /aosp_15_r20/external/yapf/yapf/yapflib/errors.py (revision 7249d1a64f4850ccf838e62a46276f891f72998e)
1# Copyright 2015 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""YAPF error objects."""
15
16from lib2to3.pgen2 import tokenize
17
18
19def FormatErrorMsg(e):
20  """Convert an exception into a standard format.
21
22  The standard error message format is:
23
24      <filename>:<lineno>:<column>: <msg>
25
26  Arguments:
27    e: An exception.
28
29  Returns:
30    A properly formatted error message string.
31  """
32  if isinstance(e, SyntaxError):
33    return '{}:{}:{}: {}'.format(e.filename, e.lineno, e.offset, e.msg)
34  if isinstance(e, tokenize.TokenError):
35    return '{}:{}:{}: {}'.format(e.filename, e.args[1][0], e.args[1][1],
36                                 e.args[0])
37  return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)
38
39
40class YapfError(Exception):
41  """Parent class for user errors or input errors.
42
43  Exceptions of this type are handled by the command line tool
44  and result in clear error messages, as opposed to backtraces.
45  """
46  pass
47