-
Notifications
You must be signed in to change notification settings - Fork 15
/
error.py
129 lines (96 loc) · 4.07 KB
/
error.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
# ==============================================================================
"""The errors of the dsrf parsing library."""
class ValidationError(Exception):
pass
class XsdParsingFailure(ValidationError):
"""Schema parsing failure Exception."""
def __init__(self, xsd_file_name, error):
super(XsdParsingFailure, self).__init__()
self.xsd_file_name = xsd_file_name
self.error = error
def __str__(self):
return 'Unexpected error while parsing the xsd file %s (error = %s).' % (
self.xsd_file_name, self.error)
class RowValidationFailure(ValidationError):
def __init__(self, row_number, file_name, error):
super(RowValidationFailure, self).__init__()
self.row_number = row_number
self.file_name = file_name
self.error = error
def __str__(self):
return 'Row number %s (file=%s) is invalid (error=%s).' % (
self.row_number, self.file_name, self.error)
class CellValidationFailure(ValidationError):
"""Cell validation failure Exception."""
def __init__(self, cell_name, row_number, file_name, block_number, cell_value,
expected_value):
super(CellValidationFailure, self).__init__()
self.cell_name = cell_name
self.row_number = row_number
self.file_name = file_name
self.block_number = block_number
self.cell_value = cell_value
self.expected_value = expected_value
def _get_block_string(self):
# Block number is not populated for HEAD/FOOT rows.
if self.block_number:
return 'Block: %s, ' % self.block_number
return ''
def __str__(self):
return (
'Cell "%s" contains invalid value "%s". Value was expected to be %s. '
'[%sRow: %d, file=%s].' % (
self.cell_name, self.cell_value, self.expected_value,
self._get_block_string(), self.row_number, self.file_name))
class BadUnicodeError(CellValidationFailure):
"""Value was not valid UTF-8."""
def __init__(self, cell_name, row_number, file_name, block_number, cell_value,
error_detail):
super(BadUnicodeError, self).__init__(
cell_name, row_number, file_name, block_number, cell_value, None)
self.error_detail = error_detail
def __str__(self):
return (
'Cell "%s" contained a non-utf8 string: "%r". Error detail: "%s". '
'[%sRow: %d, file=%s].' % (
self.cell_name, self.cell_value, self.error_detail,
self._get_block_string(), self.row_number, self.file_name))
class RequiredCellMissing(CellValidationFailure):
"""Value is required but was not present."""
def __str__(self):
return (
'Cell "%s" is required. Value was expected to be %s. '
'[%sRow: %d, file=%s].' % (
self.cell_name, self.expected_value,
self._get_block_string(), self.row_number, self.file_name))
class ReportValidationFailure(ValidationError):
"""Report cross-files validation failure Exception."""
def __init__(self, error):
super(ReportValidationFailure, self).__init__()
self.error = error
def __str__(self):
return self.error
class FileNameValidationFailure(ReportValidationFailure):
"""File name validation failure Exception."""
def __init__(self, file_name, error):
super(FileNameValidationFailure, self).__init__(error)
self.file_name = file_name
def __str__(self):
return 'File %s has invalid filename (error = %s).' % (
self.file_name, self.error)
class FileNameValidationWarning(FileNameValidationFailure):
"""Non-fatal file name validation failure Exception."""
pass