View Source nova_handlers (nova v0.9.15)

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}, State) -> io:format("~n=====================~n", []). io:format("~p:~p was called.~n", []), io:format("State: ~p~n", [State]), io:format(Format, Args), io:format("~n=====================~n", []), {ok, 200, #{}, EmptyBinary}.

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.

Link to this section Summary

Functions

Convert process state when code is changed
This function is called for changing the form and appearance of gen_server status when it is returned from sys:get_status/1,2 or when it appears in termination error logs.
Fetches the handler identified with 'Handle' and returns the callback function for it.
Handling call messages
Handling cast messages
Handling all non call/cast messages
Initializes the server
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.
This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.
Unregisters a handler and makes it unavailable for all controllers.

Link to this section Types

-type handler_callback() :: {Module :: atom(), Function :: atom()} | fun((...) -> handler_return()).
-type handler_return() ::
    {ok, State2 :: nova:state()} |
    {Module :: atom(), State :: nova:state()} |
    {error, Reason :: any()}.

Link to this section Functions

Link to this function

code_change(OldVsn, State, Extra)

View Source
-spec code_change(OldVsn :: term() | {down, term()}, State :: term(), Extra :: term()) ->
               {ok, NewState :: term()} | {error, Reason :: term()}.
Convert process state when code is changed
Link to this function

format_status(Opt, Status)

View Source
-spec format_status(Opt :: normal | terminate, Status :: list()) -> Status :: term().
This function is called for changing the form and appearance of gen_server status when it is returned from sys:get_status/1,2 or when it appears in termination error logs.
-spec get_handler(Handle :: atom()) -> {ok, Callback :: handler_callback()} | {error, not_found}.
Fetches the handler identified with 'Handle' and returns the callback function for it.
Link to this function

handle_call(Request, From, State)

View Source
-spec handle_call(Request :: term(), From :: {pid(), term()}, State :: term()) ->
               {reply, Reply :: term(), NewState :: term()} |
               {reply, Reply :: term(), NewState :: term(), Timeout :: timeout()} |
               {reply, Reply :: term(), NewState :: term(), hibernate} |
               {noreply, NewState :: term()} |
               {noreply, NewState :: term(), Timeout :: timeout()} |
               {noreply, NewState :: term(), hibernate} |
               {stop, Reason :: term(), Reply :: term(), NewState :: term()} |
               {stop, Reason :: term(), NewState :: term()}.
Handling call messages
Link to this function

handle_cast(Request, State)

View Source
-spec handle_cast(Request :: term(), State :: term()) ->
               {noreply, NewState :: term()} |
               {noreply, NewState :: term(), Timeout :: timeout()} |
               {noreply, NewState :: term(), hibernate} |
               {stop, Reason :: term(), NewState :: term()}.
Handling cast messages
Link to this function

handle_info(Info, State)

View Source
-spec handle_info(Info :: timeout() | term(), State :: term()) ->
               {noreply, NewState :: term()} |
               {noreply, NewState :: term(), Timeout :: timeout()} |
               {noreply, NewState :: term(), hibernate} |
               {stop, Reason :: normal | term(), NewState :: term()}.
Handling all non call/cast messages
-spec init(Args :: term()) ->
        {ok, State :: term()} |
        {ok, State :: term(), Timeout :: timeout()} |
        {ok, State :: term(), hibernate} |
        {stop, Reason :: term()} |
        ignore.
Initializes the server
Link to this function

register_handler(Handle, Callback)

View Source
-spec register_handler(Handle :: atom(), Callback :: handler_callback()) ->
                    ok | {error, Reason :: atom()}.
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.
-spec start_link() ->
              {ok, Pid :: pid()} |
              {error, Error :: {already_started, pid()}} |
              {error, Error :: term()} |
              ignore.
Link to this function

terminate(Reason, State)

View Source
-spec terminate(Reason :: normal | shutdown | {shutdown, term()} | term(), State :: term()) -> any().
This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.
Link to this function

unregister_handler(Handle)

View Source
-spec unregister_handler(Handle :: atom()) -> ok.
Unregisters a handler and makes it unavailable for all controllers.