1 |
# spec/ysh-builtin-error
|
2 |
|
3 |
## our_shell: ysh
|
4 |
## oils_failures_allowed: 0
|
5 |
|
6 |
|
7 |
func divide(a, b) {
|
8 |
if (b === 0) {
|
9 |
error 'divide by zero' (status=3)
|
10 |
}
|
11 |
|
12 |
return (a / b)
|
13 |
}
|
14 |
|
15 |
# errors can be caught with try
|
16 |
try { = divide(42, 0) }
|
17 |
echo status=$_status
|
18 |
|
19 |
= divide(42, 0) # sets status to 3
|
20 |
## status: 3
|
21 |
## STDOUT:
|
22 |
status=3
|
23 |
## END
|
24 |
|
25 |
|
26 |
|
27 |
echo $[type(_error)]
|
28 |
echo $[len(_error)]
|
29 |
|
30 |
## STDOUT:
|
31 |
Dict
|
32 |
0
|
33 |
## END
|
34 |
|
35 |
|
36 |
|
37 |
func divide(a, b) {
|
38 |
if (b === 0) {
|
39 |
error "divide by zero: $a / $b" (status=3)
|
40 |
}
|
41 |
return (a / b)
|
42 |
}
|
43 |
|
44 |
try { = divide(42, 0) }
|
45 |
echo status=$_status
|
46 |
echo message=$[_error.message]
|
47 |
|
48 |
proc p {
|
49 |
echo $[divide(5, 0)]
|
50 |
}
|
51 |
|
52 |
try p
|
53 |
echo status=$_status
|
54 |
echo message=$[_error.message]
|
55 |
|
56 |
# Design bug: this isn't caught!
|
57 |
|
58 |
# try echo $[divide(3, 0]
|
59 |
|
60 |
## STDOUT:
|
61 |
status=3
|
62 |
message=divide by zero: 42 / 0
|
63 |
status=3
|
64 |
message=divide by zero: 5 / 0
|
65 |
## END
|
66 |
|
67 |
|
68 |
|
69 |
try {
|
70 |
error 'bad' (status=99)
|
71 |
}
|
72 |
pp line (_error)
|
73 |
|
74 |
# Note: myData co
|
75 |
try {
|
76 |
error 'bad' (status=99, myData={spam:'eggs'})
|
77 |
}
|
78 |
pp line (_error)
|
79 |
|
80 |
try {
|
81 |
error 'bad' (status=99, message='cannot override')
|
82 |
}
|
83 |
pp line (_error)
|
84 |
|
85 |
## STDOUT:
|
86 |
(Dict) {"status":99,"message":"bad"}
|
87 |
(Dict) {"myData":{"spam":"eggs"},"status":99,"message":"bad"}
|
88 |
(Dict) {"message":"bad","status":99}
|
89 |
## END
|
90 |
|
91 |
|
92 |
func inverse(x) {
|
93 |
if (x === 0) {
|
94 |
error '0 does not have an inverse' # default status is 1
|
95 |
}
|
96 |
|
97 |
return (1 / x)
|
98 |
}
|
99 |
|
100 |
func invertList(list) {
|
101 |
var result = []
|
102 |
for item in (list) {
|
103 |
call result->append(inverse(item))
|
104 |
}
|
105 |
return (result)
|
106 |
}
|
107 |
|
108 |
= invertList([1, 2, 0])
|
109 |
## status: 10
|
110 |
## STDOUT:
|
111 |
## END
|
112 |
|
113 |
|
114 |
func alwaysError() {
|
115 |
error "it's an error" (status=100)
|
116 |
}
|
117 |
|
118 |
try {
|
119 |
var mylist = [1 + 2, alwaysError()]
|
120 |
|
121 |
echo this will never be printed
|
122 |
}
|
123 |
= mylist # undefined! status becomes 1
|
124 |
## status: 1
|
125 |
## STDOUT:
|
126 |
## END
|
127 |
|
128 |
|
129 |
error 'some error'
|
130 |
## status: 10
|
131 |
## STDOUT:
|
132 |
## END
|
133 |
|
134 |
|
135 |
error 'error' (status='a string?')
|
136 |
## status: 3
|
137 |
## STDOUT:
|
138 |
## END
|
139 |
|
140 |
|
141 |
error msg (100)
|
142 |
## status: 3
|
143 |
## STDOUT:
|
144 |
## END
|
145 |
|
146 |
|
147 |
error uh-oh ('error', status=1)
|
148 |
## status: 3
|
149 |
## STDOUT:
|
150 |
## END
|
151 |
|
152 |
|
153 |
error
|
154 |
## status: 2
|
155 |
## STDOUT:
|
156 |
## END
|
157 |
|
158 |
|
159 |
error ('error', status=0)
|
160 |
## status: 2
|
161 |
## STDOUT:
|
162 |
## END
|
163 |
|
164 |
|
165 |
|
166 |
try { error oops }
|
167 |
echo status=$_status
|
168 |
|
169 |
try error oops
|
170 |
echo status=$_status
|
171 |
|
172 |
## STDOUT:
|
173 |
status=10
|
174 |
status=10
|
175 |
## END
|