<commit_before># frozen_string_literal: true

module Api
  module V0
    class BaseController < ApplicationController
      include Api::OpenidConnect::ProtectedResourceEndpoint

      protected

      def current_user
        current_token ? current_token.authorization.user : nil
      end
    end
  end<commit_msg>Add exception handlers in base API controller<commit_after># frozen_string_literal: true

module Api
  module V0
    class BaseController < ApplicationController
      include Api::OpenidConnect::ProtectedResourceEndpoint

      protected

      rescue_from Exception do |e|
        logger.error e.message
        render json: {error: e.message}, status: 500
      end

      rescue_from ActiveRecord::RecordNotFound do
        render json: {error: I18n.t("api.error.not_found")}, status: 404
      end

      rescue_from ActiveRecord::RecordInvalid do |e|
        render json: {error: e.to_s}, status: 400
      end

      rescue_from ActionController::ParameterMissing do |e|
        render json: {
          error:   I18n.t("api.error.wrong_parameters"),
          message: e.message
        }, status: 400
      end

      def current_user
        current_token ? current_token.authorization.user : nil
      end
    end
  end
{'fraction_non_alphanumeric': 0.0681635926222935, 'fraction_numerical': 0.014434643143544507, 'mean_word_length': 2.7477477477477477, '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': '4140135', 'n_tokens_mistral': 356, 'n_tokens_neox': 339, 'n_words': 94}