nova_handlers

This module is responsible for all the different return types a controller have. Nova is constructed in such way that it's really easy to extend it by using handlers. A handler is basically a module consisting of a function of arity 4. We will show an example of this.

If you implement the following module:


   -module(my_handler).
   -export([init/0,
            handle_console]).
  
   init() ->
      nova_handlers:register_handler(console, {my_handler, handle_console}).
  
   handle_console({console, Format, Args}, {Module, Function}, Request, State) ->
      io:format("~n=====================~n", []).
      io:format("~p:~p was called.~n", []),
      io:format("Request: ~p~nState: ~p~n", [Request, State]),
      io:format(Format, Args),
      io:format("~n=====================~n", []),
      {ok, 200, #{}, <<>>}.
   

The init/0 should be invoked from your applications supervisor and will register the module my_handler as handler of the return type {console, Format, Args}. This means that you can return this tuple in a controller which invokes my_handler:handle_console/4.

A handler can return two different types

{ok, StatusCode, Headers, Body} - This will return a proper reply to the requester.

{error, Reason} - This will render a 500 page to the user.

Types

handler_return/0
nova_handler_callback/0

Functions

register_handler(Handle, Callback) ->
Registers a new handler. This can then be used in a nova controller by returning a tuple where the first element is the name of the handler.
unregister_handler(Handle) ->
  • Handle = atom()
Unregisters a handler and makes it unavailable for all controllers.
get_handler(Handle) ->
  • Handle = atom()
Fetches the handler identified with 'Handle' and returns the callback function for it.