1#!/usr/bin/env python
2#
3# Copyright 2012 The Closure Linter Authors. All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS-IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Utility functions to format errors."""
18
19
20__author__ = ('robbyw@google.com (Robert Walker)',
21              'ajp@google.com (Andy Perelson)',
22              'nnaze@google.com (Nathan Naze)')
23
24
25def GetUnixErrorOutput(filename, error, new_error=False):
26  """Get a output line for an error in UNIX format."""
27
28  line = ''
29
30  if error.token:
31    line = '%d' % error.token.line_number
32
33  error_code = '%04d' % error.code
34  if new_error:
35    error_code = 'New Error ' + error_code
36  return '%s:%s:(%s) %s' % (filename, line, error_code, error.message)
37
38
39def GetErrorOutput(error, new_error=False):
40  """Get a output line for an error in regular format."""
41
42  line = ''
43  if error.token:
44    line = 'Line %d, ' % error.token.line_number
45
46  code = 'E:%04d' % error.code
47
48  error_message = error.message
49  if new_error:
50    error_message = 'New Error ' + error_message
51
52  return '%s%s: %s' % (line, code, error.message)
53