<commit_before>-module(erldns_encoder).

-include("dns_records.hrl").

-export([encode_message/1]).

encode_message(Response) ->
  try dns:encode_message(Response) of
    M -> M
  catch
    Exception:Reason ->
      lager:error("Error encoding ~p (~p:~p)", [Response, Exception, Reason]),
      encode_message(build_error_response(Response))
  end.

%% Populate a response with a servfail error
build_error_response(Response) when is_record(Response, dns_message) ->
  build_error_response(Response, ?DNS_RCODE_SERVFAIL);
build_error_response({_, Response}) ->
  build_error_response(Response, ?DNS_RCODE_SERVFAIL).
build_error_response(Response, Rcode) ->
  Response#dns_message{anc = 0, auc = 0, adc = 0, qr = true, aa = true, rc = Rcode, answers=[], authority=[], additional=[]}.<commit_msg>Do not catch exceptions if the config is false.<commit_after>-module(erldns_encoder).

-include("dns_records.hrl").

-export([encode_message/1]).

encode_message(Response) ->
  case application:get_env(erldns, catch_exceptions) of
    {ok, false} -> dns:encode_message(Response);
    _ ->
      try dns:encode_message(Response) of
        M -> M
      catch
        Exception:Reason ->
          lager:error("Error encoding ~p (~p:~p)", [Response, Exception, Reason]),
          encode_message(build_error_response(Response))
      end
  end.

%% Populate a response with a servfail error
build_error_response(Response) when is_record(Response, dns_message) ->
  build_error_response(Response, ?DNS_RCODE_SERVFAIL);
build_error_response({_, Response}) ->
  build_error_response(Response, ?DNS_RCODE_SERVFAIL).
build_error_response(Response, Rcode) ->
  Response#dns_message{anc = 0, auc = 0, adc = 0, qr = true, aa = true, rc = Rcode, answers=[], authority=[], additional=[]}.
{'fraction_non_alphanumeric': 0.1367231638418079, 'fraction_numerical': 0.004519774011299435, 'mean_word_length': 4.883720930232558, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '734025', 'n_tokens_mistral': 596, 'n_tokens_neox': 579, 'n_words': 123}