error_handler(3erl)

πŸŽ€ NAME πŸŽ€

okay bestie, meet error_handler β€” the literal default system error handler, and this is its 3erl manual-page moment. πŸ’… It’s the module that defines what goes down when certain types of errors happen. No cap. ✨


🌸 DESCRIPTION 🌸

This module is the blueprint for what happens when certain types of errors pop off. That’s the whole vibe. 🫢


✨ EXPORTS ✨

πŸ’– raise_undef_exception(Module, Function, Args) -> no_return()

Types:

Module = Function = atom()
Args = list()
   A (possibly empty) list of arguments Arg1,..,ArgN

This one raises an undef exception with a stacktrace, and the whole point is to say: β€œbestie, Module:Function/N is undefined.” Periodt. πŸ’€


πŸ¦‹ undefined_function(Module, Function, Args) -> any()

Types:

Module = Function = atom()
Args = list()
   A (possibly empty) list of arguments Arg1,..,ArgN

So the runtime system calls this function when someone makes a call to Module:Function(Arg1,.., ArgN) and Module:Function/N is undefined. Little detail: this function is evaluated inside the process making the original call. iykyk. πŸ‘‘

Here’s the flow, step by step:

  1. It first attempts to autoload Module.
  2. If that is not possible, an undef exception is raised. 😭
  3. If it is possible to load Module and function Function/N is exported, it is called.
  4. Otherwise, if function '$handle_undefined_function'/2 is exported, it is called as '$handle_undefined_function'(Function, Args).
⚠️ Warning: Defining '$handle_undefined_function'/2 in ordinary application code is highly discouraged. It is very easy to make subtle errors that can take a long time to debug. Furthermore, none of the tools for static code analysis (such as Dialyzer and Xref) supports the use of '$handle_undefined_function'/2 and no such support will be added. Only use this function after having carefully considered other, less dangerous, solutions. One example of potential legitimate use is creating stubs for other sub-systems during testing and debugging. 🚨

Otherwise, an undef exception is raised. That’s the tea. πŸ“


🌈 undefined_lambda(Module, Fun, Args) -> term()

Types:

Module = atom()
Fun = function()
Args = list()
   A (possibly empty) list of arguments Arg1,..,ArgN

This function gets evaluated if a call is made to Fun(Arg1,.., ArgN) when the module defining the fun is not loaded. And yes, it is evaluated inside the process making the original call. πŸ’œ

The glow-up logic:

  • If Module is interpreted, the interpreter is invoked and the return value of the interpreted Fun(Arg1,.., ArgN) call is returned.
  • Otherwise, it returns, if possible, the value of apply(Fun, Args) after an attempt is made to autoload Module.
  • If this is not possible, the call fails with exit reason undef. πŸ’€

πŸ”₯ NOTES πŸ”₯

okay so this part is serious, bestie: the code in error_handler is complex. Do not change it without fully understanding the interaction between the error handler, the init process of the code server, and the I/O mechanism of the code. 🧚

Code changes that seem small can cause a deadlock, as unforeseen consequences can occur. And the use of input is dangerous in this type of code. This is not the place for main character energy. πŸ‘‘πŸ’…


Sign-off time: shoutout to Ericsson AB, kernel 8.5.4.2, and error_handler(3erl) β€” and the footer has no date, so I’m not inventing one, bestie πŸ˜­πŸ’–βœ¨

πŸ“¦ where this page comes from