hackatime/app/controllers/errors_controller.rb
Mahad Kalam d16c67d1e9
Add maintenance page (#1030)
* Add maintenance page

* Update the page a lil

* Oops

* Fixes!
2026-03-02 16:01:40 +00:00

72 lines
1.9 KiB
Ruby

# frozen_string_literal: true
class ErrorsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :render_maintenance_page, except: :not_found
def bad_request
@status_code = 400
@title = "Bad Request"
@message = "The server cannot process your request due to invalid syntax."
render_error
end
def not_found
@status_code = 404
@title = "Page Not Found"
@message = "The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved."
render_error
end
def unprocessable_entity
@status_code = 422
@title = "Unprocessable Content"
@message = "The request was well-formed but unable to be followed due to semantic errors."
render_error
end
def internal_server_error
@status_code = 500
@title = "Internal Server Error"
@message = "Something went wrong on our end, but we are looking into it!"
@sentry_event_id = Sentry.last_event_id
render_error
end
def service_unavailable
@status_code = 503
@title = "Service Unavailable"
@message = "The service is temporarily unavailable. Please try again later."
render_error
end
private
def render_maintenance_page
return unless ENV["MAINTENANCE_MODE"].present?
render file: Rails.root.join("public", "maintenance.html"), layout: false, status: 503, content_type: "text/html"
end
def render_error
respond_to do |format|
format.html { render "errors/show", status: @status_code, layout: error_layout }
format.json { render json: error_json, status: @status_code }
format.any { render json: error_json, status: @status_code }
end
end
def error_json
response = {
error: @title,
message: @message,
status: @status_code
}
response[:sentry_event_id] = @sentry_event_id if @sentry_event_id.present?
response
end
def error_layout
"errors"
end
end