From 308e3670df3b8ed67498da0b5e07c25a01e528d3 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 5 Feb 2018 12:38:01 -0800 Subject: [PATCH] gracefully handle unavailable pg connection errors --- app/controllers/application_controller.rb | 13 +++++++++++++ app/views/static/service_unavailable.html.erb | 3 +++ 2 files changed, 16 insertions(+) create mode 100644 app/views/static/service_unavailable.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1ffab68c5..928398ed4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -19,6 +19,7 @@ class ApplicationController < ActionController::Base rescue_from User::PrivilegeError, :with => :access_denied rescue_from SessionLoader::AuthenticationFailure, :with => :authentication_failed rescue_from Danbooru::Paginator::PaginationError, :with => :render_pagination_limit + rescue_from PG::ConnectionBad, with: :bad_db_connection # This is raised on requests to `/blah.js`. Rails has already rendered StaticController#not_found # here, so calling `rescue_exception` would cause a double render error. @@ -45,6 +46,18 @@ class ApplicationController < ActionController::Base end end + def bad_db_connection + respond_to do |format| + format.json do + render json: {success: false, reason: "database is unavailable"}.to_json, status: 503 + end + + format.html do + render template: "static/service_unavailable", status: 503 + end + end + end + def api_check if !CurrentUser.is_anonymous? && !request.get? && !request.head? if CurrentUser.user.token_bucket.nil? diff --git a/app/views/static/service_unavailable.html.erb b/app/views/static/service_unavailable.html.erb new file mode 100644 index 000000000..826dc5210 --- /dev/null +++ b/app/views/static/service_unavailable.html.erb @@ -0,0 +1,3 @@ +

Service Unavailable

+ +

Try again later

\ No newline at end of file