Merge pull request #2962 from evazion/fix-services-not-implemented

Fail gracefully when attempting to use unconfigured features (#2954)
This commit is contained in:
Albert Yi
2017-04-05 12:35:47 -07:00
committed by GitHub
14 changed files with 57 additions and 46 deletions

View File

@@ -84,6 +84,13 @@ class ApplicationController < ActionController::Base
elsif exception.is_a?(::ActiveRecord::RecordNotFound) elsif exception.is_a?(::ActiveRecord::RecordNotFound)
@error_message = "That record was not found" @error_message = "That record was not found"
render :template => "static/error", :status => 404 render :template => "static/error", :status => 404
elsif exception.is_a?(NotImplementedError)
flash[:notice] = "This feature isn't available: #{@exception.message}"
respond_to do |fmt|
fmt.html { redirect_to :back }
fmt.json { render template: "static/error", status: 501 }
fmt.xml { render template: "static/error", status: 501 }
end
else else
render :template => "static/error", :status => 500 render :template => "static/error", :status => 500
end end

View File

@@ -1,25 +0,0 @@
module SavedSearches
module CheckAvailability
extend ActiveSupport::Concern
included do
before_filter :check_availability
end
def check_availability
if !SavedSearch.enabled?
respond_to do |format|
format.html do
flash[:notice] = "Listbooru service is not configured. Saved searches are not available."
redirect_to :back
end
format.json do
render json: {success: false, reason: "Listbooru service is not configured"}.to_json, status: 501
end
end
return false
end
end
end
end

View File

@@ -4,8 +4,7 @@ class IqdbQueriesController < ApplicationController
def create def create
if !Danbooru.config.iqdbs_server if !Danbooru.config.iqdbs_server
render :nothing => true raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.")
return
end end
if params[:url] if params[:url]
@@ -32,4 +31,4 @@ protected
@results = @download.matches @results = @download.matches
render :layout => false, :action => "create_by_post" render :layout => false, :action => "create_by_post"
end end
end end

View File

@@ -29,17 +29,7 @@ private
def check_availabililty def check_availabililty
if !PoolArchive.enabled? if !PoolArchive.enabled?
respond_to do |format| raise NotImplementedError.new("Archive service is not configured. Pool versions are not saved.")
format.html do
flash[:notice] = "Archive service is not configured. Pool versions are not saved."
redirect_to :back
end
format.json do
render json: {success: false, reason: "Archive service is not configured"}.to_json, status: 501
end
end
return false
end end
end end
end end

View File

@@ -1,5 +1,6 @@
class PostVersionsController < ApplicationController class PostVersionsController < ApplicationController
before_filter :member_only before_filter :member_only
before_filter :check_availabililty
respond_to :html, :xml, :json respond_to :html, :xml, :json
def index def index
@@ -25,4 +26,12 @@ class PostVersionsController < ApplicationController
format.js format.js
end end
end end
private
def check_availabililty
if !PostArchive.enabled?
raise NotImplementedError.new("Archive service is not configured. Post versions are not saved.")
end
end
end end

View File

@@ -1,7 +1,6 @@
class SavedSearchesController < ApplicationController class SavedSearchesController < ApplicationController
include SavedSearches::CheckAvailability
before_filter :member_only before_filter :member_only
before_filter :check_availability
respond_to :html, :xml, :json, :js respond_to :html, :xml, :json, :js
def index def index
@@ -56,4 +55,10 @@ private
def saved_searches def saved_searches
CurrentUser.user.saved_searches CurrentUser.user.saved_searches
end end
def check_availability
if !SavedSearch.enabled?
raise NotImplementedError.new("Listbooru service is not configured. Saved searches are not available.")
end
end
end end

View File

@@ -2,6 +2,14 @@ require "big_query"
module GoogleBigQuery module GoogleBigQuery
class Base class Base
def self.enabled?
File.exists?(Danbooru.config.google_api_json_key_path)
end
def initialize
raise NotImplementedError.new("Google Big Query is not configured.") unless GoogleBigQuery::Base.enabled?
end
def query(q) def query(q)
client.query(q) client.query(q)
end end

View File

@@ -1,5 +1,15 @@
# queries reportbooru to find missed post searches # queries reportbooru to find missed post searches
class MissedSearchService class MissedSearchService
def self.enabled?
Danbooru.config.reportbooru_server.present?
end
def initialize
if !MissedSearchService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Missed searches are not available.")
end
end
def each_search(&block) def each_search(&block)
fetch_data.scan(/(.+?) (\d+)\.0\n/).each(&block) fetch_data.scan(/(.+?) (\d+)\.0\n/).each(&block)
end end

View File

@@ -2,7 +2,15 @@
class PopularSearchService class PopularSearchService
attr_reader :date, :scale attr_reader :date, :scale
def self.enabled?
Danbooru.config.reportbooru_server.present?
end
def initialize(date, scale) def initialize(date, scale)
if !PopularSearchService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Popular searches are not available.")
end
@date = date @date = date
@scale = scale @scale = scale
end end

View File

@@ -24,7 +24,7 @@ module Reports
end end
def fetch_similar_user_ids(endpoint = "user_similarity") def fetch_similar_user_ids(endpoint = "user_similarity")
return NotImplementedError unless Danbooru.config.reportbooru_server raise NotImplementedError.new("the Reportbooru service isn't configured. User similarity is not available.") unless Danbooru.config.reportbooru_server
params = { params = {
"key" => Danbooru.config.reportbooru_key, "key" => Danbooru.config.reportbooru_key,

View File

@@ -43,7 +43,7 @@ class PoolArchive < ActiveRecord::Base
def self.queue(pool) def self.queue(pool)
# queue updates to sqs so that if archives goes down for whatever reason it won't # queue updates to sqs so that if archives goes down for whatever reason it won't
# block pool updates # block pool updates
raise "Archive service is not configured" if !enabled? raise NotImplementedError.new("Archive service is not configured.") if !enabled?
json = { json = {
pool_id: pool.id, pool_id: pool.id,

View File

@@ -35,7 +35,7 @@ class PoolVersion < ActiveRecord::Base
extend SearchMethods extend SearchMethods
def self.export_to_archives(starting_version_id = 0) def self.export_to_archives(starting_version_id = 0)
raise "SQS URL not setup" if Danbooru.config.aws_sqs_archives_url.nil? raise NotImplementedError.new("SQS URL not setup") if Danbooru.config.aws_sqs_archives_url.nil?
credentials = Aws::Credentials.new( credentials = Aws::Credentials.new(
Danbooru.config.aws_access_key_id, Danbooru.config.aws_access_key_id,

View File

@@ -60,7 +60,7 @@ class PostArchive < ActiveRecord::Base
def queue(post) def queue(post)
# queue updates to sqs so that if archives goes down for whatever reason it won't # queue updates to sqs so that if archives goes down for whatever reason it won't
# block post updates # block post updates
raise "Archive service is not configured" if !enabled? raise NotImplementedError.new("Archive service is not configured") if !enabled?
json = { json = {
"post_id" => post.id, "post_id" => post.id,

View File

@@ -4,7 +4,7 @@
<li id="secondary-links-posts-upload" class="nonessential"><%= link_to "Upload", new_upload_path %></li> <li id="secondary-links-posts-upload" class="nonessential"><%= link_to "Upload", new_upload_path %></li>
<li id="secondary-links-posts-popular"><%= link_to "Popular", popular_explore_posts_path %></li> <li id="secondary-links-posts-popular"><%= link_to "Popular", popular_explore_posts_path %></li>
<li id="secondary-links-posts-hot"><%= link_to "Hot", posts_path(:tags => "order:rank") %></li> <li id="secondary-links-posts-hot"><%= link_to "Hot", posts_path(:tags => "order:rank") %></li>
<% if Danbooru.config.reportbooru_server %> <% if PopularSearchService.enabled? %>
<li id="secondary-links-posts-searches"><%= link_to "Searches", searches_explore_posts_path %></li> <li id="secondary-links-posts-searches"><%= link_to "Searches", searches_explore_posts_path %></li>
<% end %> <% end %>
<% unless CurrentUser.is_anonymous? %> <% unless CurrentUser.is_anonymous? %>