Let it fail?

Hello, I’ve read on many sources that instead of checking for errors people let the code fail in Erlang and Elixir, and this is a concept I can understand.
I’m building a static site generator for learning purposes, because I will never become proficient with a language if I don’t write anything with it, and that seemed a domain I can deal with, having used many generators in the past.
Now, to generate a website I need a predefined directory structure and I came up with something like this:

Then I thought that instead of checking for the existence of the right directories I should probably “let it fail”, but honestly I’m not entirely sure I’m supposed to or how to handle this situation.
Should I try to build the program as the structure is fine and then raise exceptions when something goes wrong? I thought that if the directory structure starts having many requirements that code could become very ugly, repetitive and long.
I would like to hear from more experienced Elixir developers, I’ve started studying the language 10 days ago :slight_smile:
Suggestions on how to improve my code are very welcome, I’m eager to learn.

Thanks in advance,
ngw

3 Likes

it’s in the name. An ‘exception’ happens under exceptional circumstances.

What you need to decide when developing something, is whether a situation commonly occurs in the code you are writing, or whether it only happens very rarely. In the second case, it is none of your programs concern to solve it. It is often more transparent to fail fast, and let some higher-krder system determine if the operation at hand should be redone, skipped, or was so important that the rest of the program should now stop as well.

‘Let it crash’ does not mean ‘never write anything but the happy path’. It does mean ‘dont try to handle more edge cases than would be reasonable’.

When writing something working with the file system, you most definitely want to handle the case when a file could not be found.

When, on the other hand you write a system that is dependent on a configuration file, it does not make sense to ‘handle’ that exception because there is nothing to really ‘solve’ this situation.

5 Likes

In your situation, I think the best way to ‘let it fail’ is to check at the beginning if the directories are there. If they are not, you can crash right then, telling the user that the directory structure is wrong.

What probably should not do, is to put a whole lot of ‘is the directory still there?’ checks during the execution. In the unlikely chance that a user removed a directory while your code was running, it is perfectly fine behavior to just crash.


p.s. it is very common for Elixir/Erlang programmers to first code the ‘happy path’, then add in solutions for the most likely ways something can go wrong, and then see how it will behave in production. When it does crash at some point, you can then revisit if that specific situation is something you should handle, or something that was a mistake by the user.

5 Likes

Do want to interject, it is quite easy to code both for the happy_path and handle errors at the same time (with a default error handler for many happy_paths to share if you want) via: https://hex.pm/packages/happy
I am a huge proponent of the above happy package.

EDIT: Oh, any you can let unhandled errors propogate up with happy_path or you can turn unhandled errors into exceptions via happy_path!.