Cleanest way to report errors in Haskell

I’m working on learning Haskell, and I’ve come across three different ways of dealing with errors in functions I write:

  1. I can simply write error "Some error message.", which throws an exception.
  2. I can have my function return Maybe SomeType, where I may or may not be able to return what I’d like to return.
  3. I can have my function return Either String SomeType, where I can return either an error message or what I was asked to return in the first place.

My question is: Which method of dealing with errors should I use, and why? Maybe I should different methods, depending on the context?

My current understanding is:

  • It’s “difficult” to deal with exceptions in purely functional code, and in Haskell one wants to keep things as purely functional as possible.
  • Returning Maybe SomeType is the proper thing to do if the function will either fail or succeed (i.e., there aren’t different ways it can fail).
  • Returning Either String SomeType is the proper thing to do if a function can fail in any one of various ways.

Alright, first rule of error handling in Haskell: Never use error.

It’s just terrible in every way. It exists purely as an act of history and the fact that Prelude uses it is terrible. Don’t use it.

The only conceivable time you could use it is when something is so internally terrible that something must be wrong with the very fabric of reality, thus rendering the outcome of your program moot.

Now the question becomes Maybe vs Either. Maybe is nicely suited to something like head, which may or may not return a value but there is only one possible reason to fail. Nothing is saying something like “it broke, and you already know why”. Some would say it indicates a partial function.

The most robust form of error handling is Either + an error ADT.

For example in one of my hobby compilers, I have something like

data CompilerError = ParserError ParserError
                   | TCError TCError
                   ...
                   | ImpossibleError String

data ParserError = ParserError (Int, Int) String
data TCError = CouldntUnify Ty Ty
             | MissingDefinition Name
             | InfiniteType Ty
             ...

type ErrorM m = ExceptT CompilerError m -- from MTL

Now I define a bunch of error types, nesting them so that I have one glorious top level error. This can be an error from any stage of compilation or an ImpossibleError, which signifies a compiler bug.

Each of these error types try to keep as much information for as long as possible for pretty printing or other analysis. More importantly, by not having a string I can test that running an illtyped program through the type checker actually generates a unification error! Once something is a String, it’s gone forever and any information it contained is opaque to the compiler/tests, so just Either String isn’t great either.

Finally I pack this type into ExceptT, a new monad transformer from MTL. This is essentially EitherT and comes with a nice batch of functions for throwing and catching errors in a pure, pleasant way.

Finally, it’s worth mentioning that Haskell has the mechanisms to support handling exceptions like other languages do, except that catching an exception lives in IO. I know some people like to use these for IO heavy applications where everything could potentially fail, but so infrequently that they don’t like to think about it. Whether you use these impure exceptions or just ExceptT Error IO is really a matter of taste. Personally I opt for ExceptT because I like being reminded of the chance of failure.


As a summary,

  • Maybe – I can fail in one obvious way
  • Either CustomType – I can fail, and I’ll tell you what happened
  • IO + exceptions – I sometimes fail. Check my docs to see what I throw when I do
  • error – I hate you too user

3

I wouldn’t separate 2 and 3 based on how many ways something can fail. As soon as you think there’s only one possible way, another will show its face. The metric should instead be “do my callers care why something failed? Can they actually do anything about it?”.

Beyond that, it’s not immediately clear to me that Either String SomeType can produce an error condition. I would make a simple algebraic data type with the conditions with a more descriptive name.

Which you use depends on the nature of the problem you face, and the idioms of the software package you’re working with. Though I would tend to avoid #1.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật