yazz.com
yazz.com

Reputation: 58786

Returning an error code in Erlang

I'm writing some Erlang code and I'm very unsure whether I should let everything fail fast. One problem I find with fail fast is that for the developer/user the exception generated is pretty meaningless. Any idea what I should return which would not give esoteric and hard to read stack traces?

Upvotes: 1

Views: 339

Answers (4)

Yasir Arsanukayev
Yasir Arsanukayev

Reputation: 9676

Since Erlang is a functional language errors are often easily spot if you tend to write Pure functions, because pure function given one arguments will always return the same result. Thus, having the stack trace, once you found the failed function you can figure out what caused an error. Opposed to imperative programming you don't need to spend a lot of time debugging your code, often you don't even need the debugger, and searching the error turns to an interesting game. This article is useful.

Upvotes: 1

Roberto Aloi
Roberto Aloi

Reputation: 30985

The basic principle in Erlang is:

Make it crash!

I found quite useful to avoid the so called defensive programming. The concept is explained in a bit more detail in the Erlang Programming Rules page:

http://www.erlang.se/doc/programming_rules.shtml#HDR11

Moreover, even if some of the Erlang errors can be a bit cryptic, a nice way to deal with is trace them! Tracing in Erlang is pretty simple. Have a look to this quick reference:

http://aloiroberto.wordpress.com/2009/02/23/tracing-erlang-functions/

or simply refer to the official documentation.

Upvotes: 1

Yasir Arsanukayev
Yasir Arsanukayev

Reputation: 9676

I'd recommend you logging with error_logger(3) and have developer look what actually happens behind the scenes. It's recommended to follow OTP principles in order to collect all data which is returned by VM when process crashes.

Upvotes: 2

ndim
ndim

Reputation: 37767

I would advise to fail fast and learn how to read stack traces.

Upvotes: 1

Related Questions