OILS / spec / ysh-builtin-error.test.sh View on Github | oilshell.org

265 lines, 130 significant
1# spec/ysh-builtin-error
2
3## our_shell: ysh
4## oils_failures_allowed: 1
5
6#### try requires an argument
7
8try
9echo status=$?
10
11## status: 3
12## STDOUT:
13## END
14
15#### User errors behave like builtin errors
16func divide(a, b) {
17 if (b === 0) {
18 error 'divide by zero' (code=3)
19 }
20
21 return (a / b)
22}
23
24# errors can be caught with try
25try { = divide(42, 0) }
26echo status=$_status
27
28= divide(42, 0)
29
30## status: 3
31## STDOUT:
32status=3
33## END
34
35#### _error register is initially empty dict
36
37echo $[type(_error)]
38echo $[len(_error)]
39
40## STDOUT:
41Dict
420
43## END
44
45#### error builtin sets _error.message, which can be used by programs
46
47func divide(a, b) {
48 if (b === 0) {
49 error "divide by zero: $a / $b" (code=3)
50 }
51 return (a / b)
52}
53
54try { = divide(42, 0) }
55echo status=$_status
56echo message=$[_error.message]
57
58proc p {
59 echo $[divide(5, 0)]
60}
61
62try { p }
63echo status=$_status
64echo message=$[_error.message]
65
66## STDOUT:
67status=3
68message=divide by zero: 42 / 0
69status=3
70message=divide by zero: 5 / 0
71## END
72
73#### error builtin adds named args as properties on _error Dict
74
75try {
76 error 'bad' (code=99)
77}
78pp line (_error)
79
80# Note: myData co
81try {
82 error 'bad' (code=99, myData={spam:'eggs'})
83}
84pp line (_error)
85
86try {
87 error 'bad' (code=99, message='cannot override')
88}
89pp line (_error)
90
91## STDOUT:
92(Dict) {"code":99,"message":"bad"}
93(Dict) {"myData":{"spam":"eggs"},"code":99,"message":"bad"}
94(Dict) {"message":"bad","code":99}
95## END
96
97#### Errors within multiple functions
98func inverse(x) {
99 if (x === 0) {
100 error '0 does not have an inverse' # default status is 1
101 }
102
103 return (1 / x)
104}
105
106func invertList(list) {
107 var result = []
108 for item in (list) {
109 call result->append(inverse(item))
110 }
111 return (result)
112}
113
114= invertList([1, 2, 0])
115## status: 10
116## STDOUT:
117## END
118
119#### Impact of errors on var declaration
120func alwaysError() {
121 error "it's an error" (status=100)
122}
123
124try {
125 var mylist = [1 + 2, alwaysError()]
126
127 echo this will never be printed
128}
129= mylist # undefined! status becomes 1
130## status: 1
131## STDOUT:
132## END
133
134#### default error code is 10
135error 'some error'
136## status: 10
137## STDOUT:
138## END
139
140#### error code should be an integer
141error 'error' (code='a string?')
142## status: 3
143## STDOUT:
144## END
145
146#### Error code should be named arg, not positional
147error msg (100)
148## status: 3
149## STDOUT:
150## END
151
152#### error cannot take word args
153error uh-oh ('error', status=1)
154## status: 3
155## STDOUT:
156## END
157
158#### error requires arguments
159error
160## status: 2
161## STDOUT:
162## END
163
164#### error cannot have a code of 0
165error ('error', code=0)
166## status: 2
167## STDOUT:
168## END
169
170#### try { error oops }
171
172try { error oops }
173echo status=$_status
174
175## STDOUT:
176status=10
177## END
178
179#### Handle _error.code
180
181proc failing {
182 error 'failed' (code=99)
183}
184
185try {
186 failing
187}
188if (_error.code === 99) {
189 echo PASS
190}
191
192try {
193 failing
194}
195case (_error.code) {
196 (0) { echo success }
197 (1) { echo one }
198 (else) { echo CASE PASS }
199}
200
201## STDOUT:
202PASS
203CASE PASS
204## END
205
206
207#### failed builtin usage
208
209set +o errexit
210
211try { echo ok }
212
213failed (42)
214echo status=$?
215
216try { echo ok }
217
218# Too many args
219failed a b
220echo status=$?
221
222## STDOUT:
223ok
224status=2
225ok
226status=2
227## END
228
229#### failed builtin
230
231try {
232 echo hi
233}
234if failed {
235 echo 'should not get here'
236} else {
237 echo 'ok 1'
238}
239
240try {
241 #test -n ''
242
243 # Hm json read sets the regular error
244 # Should we raise error.Structured?
245 #json read <<< '{'
246
247 var x = fromJson('{')
248
249 # Hm the error is in a SUBPROCESS HERE
250 #echo '{' | json read
251}
252if failed {
253 echo 'ok 2'
254} else {
255 echo 'should not get here'
256}
257
258## STDOUT:
259hi
260ok 1
261ok 2
262## END
263
264
265