/
error.py
163 lines (118 loc) · 4.19 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
error.py
"""
from __future__ import print_function
from _devbuild.gen.syntax_asdl import loc_e, loc_t
from mycpp import mylib
# Break circular dependency.
#from asdl import runtime
NO_SPID = -1
if mylib.PYTHON:
class Usage(Exception):
"""For flag parsing errors in builtins and main()
Called by e_usage(). TODO: Should settle on a single interface that can be
translated. Sometimes we use 'raise error.Usage()'
"""
def __init__(self, msg, span_id=NO_SPID):
# type: (str, int) -> None
self.msg = msg
self.span_id = span_id
class _ErrorWithLocation(Exception):
"""A parse error that can be formatted.
Formatting is in ui.PrintError.
"""
def __init__(self, msg, location):
# type: (str, loc_t) -> None
Exception.__init__(self)
self.msg = msg
self.location = location
def HasLocation(self):
# type: () -> bool
return self.location.tag_() != loc_e.Missing
def UserErrorString(self):
# type: () -> str
return self.msg
def __repr__(self):
# type: () -> str
return '<%s %r>' % (self.msg, self.location)
def __str__(self):
# type: () -> str
# The default doesn't work very well?
return repr(self)
# Need a better constructor
if mylib.PYTHON:
class Runtime(Exception):
"""An error that's meant to be caught, i.e. it's non-fatal.
Thrown by core/state.py and caught by builtins
"""
def __init__(self, msg):
# type: (str) -> None
self.msg = msg
def UserErrorString(self):
# type: () -> str
return self.msg
class Parse(_ErrorWithLocation):
"""Used in the parsers."""
def __init__(self, msg, location):
# type: (str, loc_t) -> None
_ErrorWithLocation.__init__(self, msg, location)
class FailGlob(_ErrorWithLocation):
"""Raised when a glob matches nothing when failglob is set.
Meant to be caught.
"""
def __init__(self, msg, location):
# type: (str, loc_t) -> None
_ErrorWithLocation.__init__(self, msg, location)
class RedirectEval(_ErrorWithLocation):
"""Used in the CommandEvaluator.
A bad redirect causes the SimpleCommand to return with status 1. To make it
fatal, use set -o errexit.
"""
def __init__(self, msg, location):
# type: (str, loc_t) -> None
_ErrorWithLocation.__init__(self, msg, location)
class FatalRuntime(_ErrorWithLocation):
"""An exception that propagates to the top level.
Used in the evaluators, and also also used in test builtin for invalid
argument.
"""
def __init__(self, exit_status, msg, location):
# type: (int, str, loc_t) -> None
_ErrorWithLocation.__init__(self, msg, location)
self.exit_status = exit_status
def ExitStatus(self):
# type: () -> int
return self.exit_status
class Strict(FatalRuntime):
"""Depending on shell options, these errors may be caught and ignored.
For example, if options like these are ON:
set -o strict_arith
set -o strict_word_eval
then we re-raise the error so it's caught by the top level. Otherwise
we catch it and return a dummy value like '' or -1 (i.e. what bash commonly
does.)
TODO: Have levels, like:
OIL_STRICT_PRINT=2 # print warnings at level 2 and above
OIL_STRICT_DIE=1 # abort the program at level 1 and above
"""
def __init__(self, msg, location):
# type: (str, loc_t) -> None
FatalRuntime.__init__(self, 1, msg, location)
class ErrExit(FatalRuntime):
"""For set -e.
Travels between WordEvaluator and CommandEvaluator.
"""
def __init__(self, exit_status, msg, location, show_code=False):
# type: (int, str, loc_t, bool) -> None
FatalRuntime.__init__(self, exit_status, msg, location)
self.show_code = show_code
class Expr(FatalRuntime):
""" e.g. KeyError, IndexError, ZeroDivisionError """
def __init__(self, msg, location):
# type: (str, loc_t) -> None
# Unique status of 3 for expression errors -- for both the caught and
# uncaught case.
#
# Caught: try sets _status register to 3
# Uncaught: shell exits with status 3
FatalRuntime.__init__(self, 3, msg, location)