<commit_before>class AnswersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_question

  def create
    redirect_to question_path(@question) if @question.has_accepted_answer? #there's already accepted answer - redirect to question

    @answer = Answer.new(answer_params)
    @answer.user = current_user
    @answer.question = @question

    if @answer.save
      Thread.new do
        QuestionMailer.new_answer(@question.user, @question).deliver
        ActiveRecord::Base.connection.close
      end
      redirect_to question_path(@question), notice: "Answer was successfully created."
    else
      redirect_to question_path(@question), alert: "There was an error when adding answer."
    end
  end

  def like
    @answer = Answer.find(params[:answer_id])

    if current_user.likes? @answer
      redirect_to question_path(@question) #already liked!!
    else
      @like = Like.new
      @like.user = current_user
      @like.answer = @answer

      @like.save
      @answer.user.add_points(5)
      respond_to :js
      #render 'like.js'
    end
  end

  def accept
    if current_user.author_of? @question and not @question.has_accepted_answer?
      @answer = Answer.find(params[:answer_id])
      @answer.accepted = true
      @answer.save
      @answer.user.add_points(25)
      Thread.new do
        AnswerMailer.accepted(@answer.user, @answer.question).deliver
        ActiveRecord::Base.connection.close
      end
    end
      redirect_to question_path(@question)
  end

  private

    def set_question
      @question = Question.find(params[:question_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def answer_params
      params.require(:answer).permit(:contents)
    end
end
<commit_msg>Added exceptions catching to async mailers.<commit_after>class AnswersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_question

  def create
    redirect_to question_path(@question) if @question.has_accepted_answer? #there's already accepted answer - redirect to question

    @answer = Answer.new(answer_params)
    @answer.user = current_user
    @answer.question = @question

    if @answer.save
      Thread.new do
        begin
          QuestionMailer.new_answer(@question.user, @question).deliver
        rescue
        ensure
          ActiveRecord::Base.connection.close
        end
      end
      redirect_to question_path(@question), notice: "Answer was successfully created."
    else
      redirect_to question_path(@question), alert: "There was an error when adding answer."
    end
  end

  def like
    @answer = Answer.find(params[:answer_id])

    if current_user.likes? @answer
      redirect_to question_path(@question) #already liked!!
    else
      @like = Like.new
      @like.user = current_user
      @like.answer = @answer

      @like.save
      @answer.user.add_points(5)
      respond_to :js
      #render 'like.js'
    end
  end

  def accept
    if current_user.author_of? @question and not @question.has_accepted_answer?
      @answer = Answer.find(params[:answer_id])
      @answer.accepted = true
      @answer.save
      @answer.user.add_points(25)
      Thread.new do
        begin
          AnswerMailer.accepted(@answer.user, @answer.question).deliver
        rescue
        ensure
          ActiveRecord::Base.connection.close
        end
      end
    end
      redirect_to question_path(@question)
  end

  private

    def set_question
      @question = Question.find(params[:question_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def answer_params
      params.require(:answer).permit(:contents)
    end
end

{'fraction_non_alphanumeric': 0.08417419012214551, 'fraction_numerical': 0.0015932023366967605, 'mean_word_length': 2.8955532574974145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '7303904', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1058, 'n_words': 269}