summaryrefslogtreecommitdiff
path: root/topics/use-exceptions-to-indicate-errors.gmi
diff options
context:
space:
mode:
authorAlexander_Kabui2022-09-05 20:08:20 +0300
committerAlexander_Kabui2022-09-05 20:08:20 +0300
commit93cfcd34d73be3c4ab6811b11d9703e7ac091d1b (patch)
treeeb25dfda7d42acc03b039eefdbaa4510591354e2 /topics/use-exceptions-to-indicate-errors.gmi
parent47b0a949735cbf776b276db08a7e497cdefbfb72 (diff)
parentf52cfbb325ad28cd743ea94b83859977f0063230 (diff)
downloadgn-gemtext-93cfcd34d73be3c4ab6811b11d9703e7ac091d1b.tar.gz
Merge branch 'main' of https://github.com/genenetwork/gn-gemtext-threads into main
Diffstat (limited to 'topics/use-exceptions-to-indicate-errors.gmi')
-rw-r--r--topics/use-exceptions-to-indicate-errors.gmi16
1 files changed, 16 insertions, 0 deletions
diff --git a/topics/use-exceptions-to-indicate-errors.gmi b/topics/use-exceptions-to-indicate-errors.gmi
new file mode 100644
index 0000000..e302dd3
--- /dev/null
+++ b/topics/use-exceptions-to-indicate-errors.gmi
@@ -0,0 +1,16 @@
+# Use exceptions to indicate errors
+
+Often, we indicate that a function has encountered an error by returning a None value. Here's why this is a bad idea and why you should use exceptions instead.
+
+When we return None values to indicate errors, we have to take care to check the return value of every function call and propagate errors higher and higher up the function call stack until we reach a point where the error is handled. This clutters up the code, and is one reason why writing correct code in languages like C that don't have exceptions is a pain.
+
+With exceptions, we only have to create an exception handler (try/except block in Python) at the highest level. Any exception raised by functions below that level are automatically passed on to the except block with no additional programmer effort.
+
+Here's an example where we run mapping, and if there's an error, we return an error page. Else, we return the results page. Notice that we do not check the return value template_vars.
+```
+try:
+ template_vars = run_mapping.RunMapping(start_vars, temp_uuid)
+ return render_template("mapping_results.html", **template_vars)
+except:
+ return render_template("mapping_error.html")
+```