about summary refs log tree commit diff stats
path: root/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-13_Error_Handling.html
blob: d28d63d01844152efc7a2a4469ff1dd71d415ba6 (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>13_Error_Handling - Documentation</title>

    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav>
    <li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Tutorials</li><li class="nav-item"><a href="tutorial-00_Introduction.html">00_Introduction</a></li><li class="nav-item"><a href="tutorial-01_Function_Calls.html">01_Function_Calls</a></li><li class="nav-item"><a href="tutorial-02_Function_Composition.html">02_Function_Composition</a></li><li class="nav-item"><a href="tutorial-03_Table_Operations.html">03_Table_Operations</a></li><li class="nav-item"><a href="tutorial-04_Currying.html">04_Currying</a></li><li class="nav-item"><a href="tutorial-05_Pattern_Matching.html">05_Pattern_Matching</a></li><li class="nav-item"><a href="tutorial-06_Immutable_Tables.html">06_Immutable_Tables</a></li><li class="nav-item"><a href="tutorial-07_Function_References.html">07_Function_References</a></li><li class="nav-item"><a href="tutorial-08_Combinators.html">08_Combinators</a></li><li class="nav-item"><a href="tutorial-09_Expression_Based.html">09_Expression_Based</a></li><li class="nav-item"><a href="tutorial-10_Tables_Deep_Dive.html">10_Tables_Deep_Dive</a></li><li class="nav-item"><a href="tutorial-11_Standard_Library.html">11_Standard_Library</a></li><li class="nav-item"><a href="tutorial-12_IO_Operations.html">12_IO_Operations</a></li><li class="nav-item"><a href="tutorial-13_Error_Handling.html">13_Error_Handling</a></li><li class="nav-item"><a href="tutorial-14_Advanced_Combinators.html">14_Advanced_Combinators</a></li><li class="nav-item"><a href="tutorial-15_Integration_Patterns.html">15_Integration_Patterns</a></li><li class="nav-item"><a href="tutorial-16_Best_Practices.html">16_Best_Practices</a></li><li class="nav-item"><a href="tutorial-README.html">README</a></li><li class="nav-heading"><a href="global.html">Globals</a></li><li class="nav-item"><span class="nav-item-type type-member">M</span><span class="nav-item-name"><a href="global.html#callStackTracker">callStackTracker</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#debugError">debugError</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#debugLog">debugLog</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#executeFile">executeFile</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#initializeStandardLibrary">initializeStandardLibrary</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#interpreter">interpreter</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#lexer">lexer</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#main">main</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#parser">parser</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#readFile">readFile</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#run">run</a></span></li>
</nav>

<div id="main">
    
    <h1 class="page-title">13_Error_Handling</h1>
    

    <section>

<header>
    
</header>

<article>
    <h1>Error Handling</h1>
<h2>What is Error Handling?</h2>
<p>Error handling in Baba Yaga is based on functional programming principles - instead of throwing exceptions, we use pattern matching and return values to handle errors gracefully.</p>
<h2>Basic Error Handling</h2>
<h3>Using Pattern Matching</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Handle division by zero */
safe_divide : x y -> 
  when y = 0 then &quot;Error: Division by zero&quot;
  _ then x / y;

/* Test the function */
..out safe_divide 10 2;   /* 5 */
..out safe_divide 10 0;   /* Error: Division by zero */
</code></pre>
<h3>Return Error Values</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Return structured error information */
divide_with_error : x y -> 
  when y = 0 then {error: true, message: &quot;Division by zero&quot;, dividend: x}
  _ then {error: false, result: x / y};

/* Handle the result */
result : divide_with_error 10 0;
when result.error is
  true then ..out &quot;Error: &quot; + result.message
  false then ..out &quot;Result: &quot; + result.result;
</code></pre>
<h2>Assertions for Validation</h2>
<h3>Input Validation</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Validate function inputs */
factorial : n -> 
  ..assert &quot;n must be non-negative&quot; n >= 0;
  when n is
    0 then 1
    _ then n * (factorial (n - 1));

/* Test validation */
..out factorial 5;   /* 120 */
/* factorial -1; */  /* Would fail assertion */
</code></pre>
<h3>Data Validation</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Validate table structure */
validate_user : user -> 
  ..assert &quot;user must have name&quot; t.has user &quot;name&quot;;
  ..assert &quot;user must have age&quot; t.has user &quot;age&quot;;
  ..assert &quot;age must be positive&quot; user.age > 0;
  user;

/* Test validation */
valid_user : {name: &quot;Alice&quot;, age: 30};
invalid_user : {name: &quot;Bob&quot;};  /* Missing age */

validated : validate_user valid_user;
/* validate_user invalid_user; */  /* Would fail assertion */
</code></pre>
<h2>Error Patterns</h2>
<h3>Maybe Pattern</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Maybe pattern for optional values */
find_user : id users -> 
  when t.has users id then {just: true, value: t.get users id}
  _ then {just: false};

/* Handle maybe results */
users : {
  alice: {name: &quot;Alice&quot;, age: 30},
  bob: {name: &quot;Bob&quot;, age: 25}
};

result : find_user &quot;alice&quot; users;
when result.just is
  true then ..out &quot;Found: &quot; + result.value.name
  false then ..out &quot;User not found&quot;;

not_found : find_user &quot;charlie&quot; users;
when not_found.just is
  true then ..out &quot;Found: &quot; + not_found.value.name
  false then ..out &quot;User not found&quot;;
</code></pre>
<h3>Either Pattern</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Either pattern for success/error */
parse_number : input -> 
  parsed : parseInt input;
  when parsed = NaN then {left: &quot;Invalid number: &quot; + input}
  _ then {right: parsed};

/* Handle either results */
valid : parse_number &quot;42&quot;;
when valid.left is
  _ then ..out &quot;Error: &quot; + valid.left
  _ then ..out &quot;Success: &quot; + valid.right;

invalid : parse_number &quot;abc&quot;;
when invalid.left is
  _ then ..out &quot;Error: &quot; + invalid.left
  _ then ..out &quot;Success: &quot; + invalid.right;
</code></pre>
<h2>Error Recovery</h2>
<h3>Fallback Values</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Provide fallback values */
get_config : key default_value config -> 
  when t.has config key then t.get config key
  _ then default_value;

/* Use with fallbacks */
config : {debug: true, timeout: 30};
debug_mode : get_config &quot;debug&quot; false config;      /* true */
retries : get_config &quot;retries&quot; 3 config;           /* 3 (fallback) */
</code></pre>
<h3>Retry Logic</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Simple retry with exponential backoff */
retry_operation : operation max_attempts -> 
  attempt_operation : attempt -> 
    when attempt > max_attempts then {error: &quot;Max attempts exceeded&quot;}
    _ then 
      result : operation;
      when result.error is
        true then 
          delay : power 2 attempt;  /* Exponential backoff */
          ..out &quot;Attempt &quot; + attempt + &quot; failed, retrying in &quot; + delay + &quot;ms&quot;;
          attempt_operation (attempt + 1)
        false then result;
  
  attempt_operation 1;
</code></pre>
<h2>Error Propagation</h2>
<h3>Chaining Error Handling</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Chain operations that might fail */
process_user_data : user_id -> 
  /* Step 1: Find user */
  user_result : find_user user_id users;
  when user_result.just is
    false then {error: &quot;User not found: &quot; + user_id}
    _ then 
      user : user_result.value;
      
      /* Step 2: Validate user */
      validation_result : validate_user user;
      when validation_result.error is
        true then {error: &quot;Invalid user data&quot;}
        _ then 
          /* Step 3: Process user */
          processed : process_user user;
          {success: true, data: processed};
</code></pre>
<h2>Testing Error Conditions</h2>
<h3>Test Error Cases</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Test both success and error cases */
test_safe_divide : -> 
  /* Test successful division */
  ..assert &quot;10 / 2 = 5&quot; safe_divide 10 2 = 5;
  
  /* Test division by zero */
  error_result : safe_divide 10 0;
  ..assert &quot;Division by zero returns error&quot; error_result = &quot;Error: Division by zero&quot;;
  
  ..out &quot;All tests passed&quot;;

/* Run the tests */
test_safe_divide;
</code></pre>
<h3>Property-Based Testing</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Test properties of error handling */
test_divide_properties : -> 
  /* Property: safe_divide x 1 = x */
  ..assert &quot;x / 1 = x&quot; safe_divide 42 1 = 42;
  
  /* Property: safe_divide x 0 always returns error */
  ..assert &quot;x / 0 always errors&quot; safe_divide 5 0 = &quot;Error: Division by zero&quot;;
  ..assert &quot;x / 0 always errors&quot; safe_divide -3 0 = &quot;Error: Division by zero&quot;;
  
  /* Property: safe_divide 0 x = 0 (when x ≠ 0) */
  ..assert &quot;0 / x = 0&quot; safe_divide 0 5 = 0;
  
  ..out &quot;All properties verified&quot;;
</code></pre>
<h2>Best Practices</h2>
<h3>Keep Error Handling Explicit</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Good: Explicit error handling */
process_data : data -> 
  when data = null then {error: &quot;No data provided&quot;}
  _ then 
    result : transform data;
    when result.error is
      true then result
      false then {success: true, data: result.data};

/* Avoid: Silent failures */
bad_process : data -> 
  transform data;  /* What if this fails? */
</code></pre>
<h3>Use Descriptive Error Messages</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Good: Descriptive errors */
validate_age : age -> 
  when age &lt; 0 then &quot;Age cannot be negative: &quot; + age
  when age > 150 then &quot;Age seems unrealistic: &quot; + age
  _ then age;

/* Avoid: Generic errors */
bad_validate : age -> 
  when age &lt; 0 then &quot;Invalid input&quot;  /* Too generic */
  _ then age;
</code></pre>
<h3>Handle Errors at the Right Level</h3>
<pre class="prettyprint source lang-plaintext"><code>/* Handle errors where you have context */
process_user : user_id -> 
  user : find_user user_id;
  when user.just is
    false then 
      ..emit &quot;user_not_found&quot; {user_id: user_id, timestamp: now()};
      &quot;User not found&quot;
    _ then 
      process_user_data user.value;
</code></pre>
<h2>Next Steps</h2>
<p>Now that you understand error handling, explore:</p>
<ul>
<li><a href="15_Integration_Patterns.md">Integration Patterns</a> for external system error handling</li>
<li><a href="14_Advanced_Combinators.md">Advanced Combinators</a> for error handling patterns</li>
<li><a href="16_Best_Practices.md">Best Practices</a> for writing robust code</li>
</ul>
</article>

</section>

</div>

<br class="clear">

<footer>
    Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.4</a> on Tue Jul 29 2025 23:15:00 GMT-0400 (Eastern Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>