Skip to content

Commit 96352d6

Browse files
author
Andy C
committed
[doc] Document error handling idiom
doc/unicode.md: Fix typos Thanks to bar-g for the feedback.
1 parent 2f2caed commit 96352d6

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

doc/unicode.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,15 @@ Other:
233233
- Eggex matching depends on ERE semantics.
234234
- `mystr ~ / [ \y01 ] /`
235235
- `case (x) { / dot / }`
236-
- `Str.{trim,trimLeft,trimRight}` respect unicode space, like JavaScript does
237-
- TODO: `Str.{upper,lower}` also need unicode case folding
238-
- are they different than the bash operations?
239-
- TODO: `s.split()` doesn't have a default "split by space", which should
240-
probably respect unicode space, like `trim()` does
241-
- TODO: `for offset, rune in (runes(mystr))` decodes UTF-8, like Go
242-
243-
Not unicode aware:
244-
245-
- `strcmp()` does byte-wise and UTF-8 wise comparisons?
236+
- [String methods](ref/chap-type-method.html)
237+
- `Str.{trim,trimStart,trimEnd}` respect unicode space, like JavaScript does
238+
- TODO: `Str.{upper,lower}` also need unicode case folding
239+
- are they different than the bash operations?
240+
- TODO: `s.split()` doesn't have a default "split by space", which should
241+
probably respect unicode space, like `trim()` does
242+
- [Builtin functions](ref/chap-builtin-func.html)
243+
- TODO: `for offset, rune in (runes(mystr))` should decode UTF-8, like Go
244+
- `strcmp()` should do byte-wise and UTF-8 wise comparisons?
246245

247246
### Data Languages
248247

doc/ysh-error.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,85 @@ The `error` builtin is more informative:
9797
echo "$[_error.code] $[_error.message] foo=$[_error.foo]"
9898
# => 99 Custom message foo=zz"
9999

100+
## Tips
101+
102+
### Proc Return Status Should Be Either OK-Fail, or True-False-Fail
103+
104+
<style>
105+
table {
106+
margin-left: 2em;
107+
background-color: #eee;
108+
}
109+
thead {
110+
background-color: white;
111+
}
112+
</style>
113+
114+
That is, use **one** of these styles:
115+
116+
<div style="display: flex; gap: 20px;">
117+
<table cellpadding="10" cellspacing="5">
118+
119+
- thead
120+
- Return Status
121+
- Meaning
122+
- tr
123+
- 0
124+
- OK
125+
- tr
126+
- 1 or more
127+
- Fail
128+
129+
</table>
130+
131+
<table cellpadding="10" cellspacing="5">
132+
133+
- thead
134+
- Return Status
135+
- Meaning
136+
- tr
137+
- 0
138+
- True
139+
- tr
140+
- 1
141+
- False
142+
- tr
143+
- 2 or more
144+
- Fail
145+
146+
</table>
147+
</div>
148+
149+
For example, here's a proc that does is **not** follow the style:
150+
151+
proc is-different (left, right) {
152+
mkdir /tmp/dest # may return 1 on failure
153+
154+
cp $left $right /tmp/dest # may return 1 on failure
155+
156+
diff -u $left $right # 0-true, 1-false, 2-failure
157+
}
158+
159+
The exit code isn't well-defined, because `mkdir` and `cp` use the OK-fail
160+
paradigm, while `diff` uses the **boolean** paradigm:
161+
162+
Explicitly checking for failure fixes it:
163+
164+
proc different (left, right) {
165+
if ! mkdir /tmp/dest {
166+
return 2 # 2-failure
167+
}
168+
if ! cp $left $right /tmp/dest {
169+
return 2 # 2-failure
170+
}
171+
172+
diff -u $left $right # 0-true, 1-false, 2-failure
173+
}
174+
100175
## Related
101176

102177
- [YSH vs. Shell Idioms > Error Handling](idioms.html#error-handling)
103-
- [YSH Fixes Shell's Error Handling (`errexit`)](error-handling.html) - Long
104-
design doc.
178+
- [YSH Fixes Shell's Error Handling (`errexit`)](error-handling.html) - A
179+
detailed design doc
105180

106181

0 commit comments

Comments
 (0)