santiagocabrera96
Programming Clojure, Fourth Edition: ex-info should include the cause. (p. 224)
In page 224, the following code is suggested.
(defn load-resource [path]
(try
(if (forbidden? path)
(throw (ex-info "Forbidden resource"
{:status 403 :resource path}))
(slurp path))
(catch java.io.FileNotFoundException e
(throw (ex-info "Missing resource"
{:status 404 :resource path})))
(catch java.io.IOException e
(throw (ex-info "Server error"
{:status 500 :resource path})))))
I’ve seen this in practice a lot and not including the cause when catching an exception generates headaches to debug later.
I’d suggest to do a bit of explanation of adding the cause exception to the ex info in the catch clauses like this:
(defn load-resource [path]
(try
(if (forbidden? path)
(throw (ex-info "Forbidden resource"
{:status 403 :resource path}))
(slurp path))
(catch java.io.FileNotFoundException e
(throw (ex-info "Missing resource"
{:status 404 :resource path}
e)))
(catch java.io.IOException e
(throw (ex-info "Server error"
{:status 500 :resource path}
e)))))
This is really useful for me to add context to exceptions that might happen in calling functions on sequences to understand what element failed.
Here’s a dummy example where I can call a function that might throw, and I would want more context on where it failed, and I can add context to the existing ex-info.
(defn randomly-fails []
(when (> (rand-int 10) 8)
(throw (ex-info "Randomly failed!" {}))))
(run! #(try (randomly-fails)
(catch clojure.lang.ExceptionInfo e
(throw (ex-info (ex-message e)
(assoc (ex-data e) :n %)
e))))
(range 100))
=> #'examples.interop/randomly-fails
Execution error (ExceptionInfo) at examples.interop/randomly-fails (form-init16509100671821839256.clj:3).
Randomly failed!
*e
=>
#error
{:cause "Randomly failed!",
:data {},
:via [{:type clojure.lang.ExceptionInfo,
:message "Randomly failed!",
:data {:n 7},
:at [examples.interop$eval2303$fn__2304 invoke "form-init16509100671821839256.clj" 6]}
{:type clojure.lang.ExceptionInfo,
:message "Randomly failed!",
:data {},
:at [examples.interop$randomly_fails invokeStatic "form-init16509100671821839256.clj" 3]}],
Popular Pragmatic Bookshelf topics
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New
#book-python-testing-with-pytest-second-edition
Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
Hey there,
I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
Is there any plan for volume 2? :slight_smile:
New
Other popular topics
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New
If you want a quick and easy way to block any website on your Mac using Little Snitch simply…
File > New Rule:
And select Deny, O...
New
Author Spotlight:
VM Brasseur
@vmbrasseur
We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
Big O Notation can make your code faster by orders of magnitude. Get the hands-on info you need to master data structures and algorithms ...
New
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc.
However, I don’t...
New
Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic.
Sam Ruby @rubys
...
New
Explore the power of Ash Framework by modeling and building the domain for a real-world web application.
Rebecca Le @sevenseacat and ...
New
Fight complexity and reclaim the original spirit of agility by learning to simplify how you develop software. The result: a more humane a...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /svelte
- /typescript
- /onivim
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /deepseek
- /zig
- /centos
- /php
- /scala
- /react-native
- /lisp
- /sublime-text
- /textmate
- /nixos
- /debian
- /agda
- /deno
- /django
- /kubuntu
- /arch-linux
- /nodejs
- /spring
- /ubuntu
- /revery
- /manjaro
- /julia
- /diversity
- /lua
- /markdown
- /quarkus









