6 Error Handling
In this chapter you will learn how to
- handle all errors without nulls and exceptions
- make sure all corner cases are handled
- indicate all the possible errors in the function signature
- compose bigger functionalities from smaller functions in the presence of different possible errors
- return user-friendly and descriptive errors
There are two ways to write error-free programs; only the third one works.
— Alan Perlis, "Epigrams on Programming"
6.1 Handling lots of different errors, gracefully
We can’t write code that never fails. That’s why we need to embrace all possible errors and make sure our code can handle them and recover from them gracefully. In this chapter we will discover how helpful it is to think about errors in terms of immutable values returned from pure functions. As always, we will learn based on a real-world example:
TV show parsing engine
In this chapter we’ll be dealing with popular TV shows. We’ll start with simple requirements and then add more of them to make a full-blown TV show parsing engine. There will be lots of corner cases to handle. We’ll use them to learn how to use FP techniques to make sure all of them are covered gracefully, without polluting the codebase. |
|