Search code examples
erlangerlang-otplager

How to terminate gen_server gracefully without crash report


In my gen_server I am terminating it like this:

handle_info({'EXIT', _From, _Reason}, State) ->
    {stop, partner_fled, State};

But still logger prints it out as an error

14:56:43.349 [error] gen_server <0.3290.0> terminated with reason: partner_fled
14:56:43.349 [error] CRASH REPORT Process <0.3290.0> with 0 neighbours exited with reason: partner_fled in gen_server:terminate/7 line 812

The code is working as intended, but I don't want the logger to print it out as its a graceful termination.

By the way I am using lager, but I think removing it will only change the format of logging.


Solution

  • From the manual:

    Notice that for any other reason than normal, shutdown, or {shutdown,Term}, the gen_server process is assumed to terminate because of an error and an error report is issued using error_logger:format/2.

    So, if you don't want any error report to be printed, you can either change your reason to normal or shutdown or if you do want to return a custom Term as well, {shutdown, Term}.

    handle_info({'EXIT', _From, _Reason}, State) ->
        {stop, {shutdown, partner_fled}, State};