1 # spec/ysh-builtin-error
2
3 ## our_shell: ysh
4 ## oils_failures_allowed: 1
5
6 #### try requires an argument
7
8 try
9 echo status=$?
10
11 ## status: 3
12 ## STDOUT:
13 ## END
14
15 #### User errors behave like builtin errors
16 func 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
25 try { = divide(42, 0) }
26 echo status=$_status
27
28 = divide(42, 0)
29
30 ## status: 3
31 ## STDOUT:
32 status=3
33 ## END
34
35 #### _error register is initially empty dict
36
37 echo $[type(_error)]
38 echo $[len(_error)]
39
40 ## STDOUT:
41 Dict
42 0
43 ## END
44
45 #### error builtin sets _error.message, which can be used by programs
46
47 func divide(a, b) {
48 if (b === 0) {
49 error "divide by zero: $a / $b" (code=3)
50 }
51 return (a / b)
52 }
53
54 try { = divide(42, 0) }
55 echo status=$_status
56 echo message=$[_error.message]
57
58 proc p {
59 echo $[divide(5, 0)]
60 }
61
62 try { p }
63 echo status=$_status
64 echo message=$[_error.message]
65
66 ## STDOUT:
67 status=3
68 message=divide by zero: 42 / 0
69 status=3
70 message=divide by zero: 5 / 0
71 ## END
72
73 #### error builtin adds named args as properties on _error Dict
74
75 try {
76 error 'bad' (code=99)
77 }
78 pp line (_error)
79
80 # Note: myData co
81 try {
82 error 'bad' (code=99, myData={spam:'eggs'})
83 }
84 pp line (_error)
85
86 try {
87 error 'bad' (code=99, message='cannot override')
88 }
89 pp 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
98 func 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
106 func 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
120 func alwaysError() {
121 error "it's an error" (status=100)
122 }
123
124 try {
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
135 error 'some error'
136 ## status: 10
137 ## STDOUT:
138 ## END
139
140 #### error code should be an integer
141 error 'error' (code='a string?')
142 ## status: 3
143 ## STDOUT:
144 ## END
145
146 #### Error code should be named arg, not positional
147 error msg (100)
148 ## status: 3
149 ## STDOUT:
150 ## END
151
152 #### error cannot take word args
153 error uh-oh ('error', status=1)
154 ## status: 3
155 ## STDOUT:
156 ## END
157
158 #### error requires arguments
159 error
160 ## status: 2
161 ## STDOUT:
162 ## END
163
164 #### error cannot have a code of 0
165 error ('error', code=0)
166 ## status: 2
167 ## STDOUT:
168 ## END
169
170 #### try { error oops }
171
172 try { error oops }
173 echo status=$_status
174
175 ## STDOUT:
176 status=10
177 ## END
178
179 #### Handle _error.code
180
181 proc failing {
182 error 'failed' (code=99)
183 }
184
185 try {
186 failing
187 }
188 if (_error.code === 99) {
189 echo PASS
190 }
191
192 try {
193 failing
194 }
195 case (_error.code) {
196 (0) { echo success }
197 (1) { echo one }
198 (else) { echo CASE PASS }
199 }
200
201 ## STDOUT:
202 PASS
203 CASE PASS
204 ## END
205
206
207 #### failed builtin usage
208
209 set +o errexit
210
211 try { echo ok }
212
213 failed (42)
214 echo status=$?
215
216 try { echo ok }
217
218 # Too many args
219 failed a b
220 echo status=$?
221
222 ## STDOUT:
223 ok
224 status=2
225 ok
226 status=2
227 ## END
228
229 #### failed builtin
230
231 try {
232 echo hi
233 }
234 if failed {
235 echo 'should not get here'
236 } else {
237 echo 'ok 1'
238 }
239
240 try {
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 }
252 if failed {
253 echo 'ok 2'
254 } else {
255 echo 'should not get here'
256 }
257
258 ## STDOUT:
259 hi
260 ok 1
261 ok 2
262 ## END
263
264
265