Bootstrap Error Pages

Generate Bootstrap friendly error pages.
Icons/chart bar
Used 135 times
Created by
D Dale Zak

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/VB0sQj"
Template Source

Review the code before running this template on your machine.

file 'app/controllers/errors_controller.rb', <<-CODE
class ErrorsController < ApplicationController

  def bad_request
    render(status: :bad_request)
  end

  def unknown_error
    render(status: :bad_request)
  end

  def route_not_found
    render(status: :not_found)
  end

  def resource_not_found
    render(status: :not_found)
  end

  def not_acceptable
    render(status: :not_acceptable)
  end

  def not_authorized
    render(status: :unprocessable_entity)
  end

  def internal_server_error
    render(status: :internal_server_error)
  end

  def service_unavailable
    render(status: :internal_server_error)
  end

end

CODE

inject_into_file 'config/routes.rb', after: 'Rails.application.routes.draw do' do <<-EOF

  match "bad-request", to: "errors#bad_request", as: "bad_request", via: :all
  match "not_authorized", to: "errors#not_authorized", as: "not_authorized", via: :all
  match "route-not-found", to: "errors#route_not_found", as: "route_not_found", via: :all
  match "resource-not-found", to: "errors#resource_not_found", as: "resource_not_found", via: :all
  match "missing-template", to: "errors#missing_template", as: "missing_template", via: :all
  match "not-acceptable", to: "errors#not_acceptable", as: "not_acceptable", via: :all
  match "unknown-error", to: "errors#unknown_error", as: "unknown_error", via: :all
  match "service-unavailable", to: "errors#service_unavailable", as: "service_unavailable", via: :all

  match '/400', to: 'errors#bad_request', via: :all
  match '/401', to: 'errors#not_authorized', via: :all
  match '/403', to: 'errors#not_authorized', via: :all
  match '/404', to: 'errors#resource_not_found', via: :all
  match '/406', to: 'errors#not_acceptable', via: :all
  match '/422', to: 'errors#not_acceptable', via: :all
  match '/500', to: 'errors#unknown_error', via: :all

  EOF
end


file 'app/views/errors/bad_request.html.erb', <<-CODE
<% content_for :title do "Bad Request" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="fas fa-4x fa-bug float-end"></i>
        <h4 class="card-title">Bad Request</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">Hmm, something doesn't seem right with that request.</h6>
        <p class="card-text">Were you trying to do something bad?</p>
      </div>
    </div>
  </div>
</div>
CODE


file 'app/views/errors/internal_server_error.html.erb', <<-CODE
<% content_for :title do "Internal Server Error" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="far fa-4x fa-exclamation-circle float-end"></i>
        <h4 class="card-title">Internal Server Error</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">Hmm, something doesn't seem right with that request.</h6>
        <p class="card-text">Were you trying to do something bad?</p>
      </div>
    </div>
  </div>
</div>
CODE


file 'app/views/errors/not_acceptable.html.erb', <<-CODE
<% content_for :title do "Not Acceptable" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="fas fa-4x fa-ban float-end"></i>
        <h4 class="card-title">Not Acceptable</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">There was a problem with your data.</h6>
        <p class="card-text">Please double check the data you enter and try again.</p>
      </div>
    </div>
  </div>
</div>
CODE


file 'app/views/errors/not_authorized.html.erb', <<-CODE
<% content_for :title do "Not Authorized" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="fas fa-4x fa-ban float-end"></i>
        <h4 class="card-title">Not Authorized</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">Unfortunately you don't have the required permission.</h6>
        <p class="card-text">Maybe you tried to change something you didn't have access to?</p>
      </div>
    </div>
  </div>
</div>
CODE


file 'app/views/errors/resource_not_found.html.erb', <<-CODE
<% content_for :title do "Resource Not Found" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="far fa-4x fa-times-circle float-end"></i>
        <h4 class="card-title">Resource Not Found</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">Unfortunately the resource was not found.</h6>
        <p class="card-text">You may have mistyped the address or the page may have moved.</p>
      </div>
    </div>
  </div>
</div>
CODE


file 'app/views/errors/route_not_found.html.erb', <<-CODE
<% content_for :title do "Route Not Found" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="far fa-4x fa-times-circle float-end"></i>
        <h4 class="card-title">Route Not Found</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">The page you were looking for doesn't exist.</h6>
        <p class="card-text">You may have mistyped the address or the page may have moved.</p>
      </div>
    </div>
  </div>
</div>
CODE

file 'app/views/errors/service_unavailable.html.erb', <<-CODE
<% content_for :title do "Service Unavailable" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="far fa-4x fa-exclamation-circle float-end"></i>
        <h4 class="card-title">Service Unavailable</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">The service is unavailable at the moment.</h6>
        <p class="card-text">Please wait and try again in a few minutes.</p>
      </div>
    </div>
  </div>
</div>
CODE

file 'app/views/errors/unknown_error.html.erb', <<-CODE
<% content_for :title do "Unknown Error" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="far fa-4x fa-question-circle float-end"></i>
        <h4 class="card-title">Unknown Error</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">We're sorry, but something went wrong.</h6>
        <p class="card-text">We'll investigate and fix the problem as soon as possible.</p>
      </div>
    </div>
  </div>
</div>
CODE

file 'app/views/errors/unsupported_version.html.erb', <<-CODE
<% content_for :title do "Unsupported Version" end %>
<div class="row">
  <div class="col-xs-10 col-sm-6 offset-xs-1 offset-sm-3">
    <div class="card my-5">
      <div class="card-body">
        <i class="fas fa-4x fa-block float-end"></i>
        <h4 class="card-title">Unsupported Version</h4>
        <h6 class="card-subtitle mt-2 mb-3 text-muted">Doesn't look like that version exists.</h6>
        <p class="card-text">Maybe double check the URL and try again?</p>
      </div>
    </div>
  </div>
</div>
CODE

inject_into_file 'app/controllers/application_controller.rb', after: 'ActionController::Base' do <<-'RUBY'

  rescue_from Exception, with: :unknown_error if Rails.env.production?
  rescue_from StandardError, with: :unknown_error
  rescue_from ActionController::RoutingError, with: :route_not_found
  rescue_from ActionController::UnknownFormat, with: :bad_request
  rescue_from ActionController::InvalidCrossOriginRequest, with: :bad_request
  rescue_from ActionController::InvalidAuthenticityToken, with: :bad_request
  rescue_from AbstractController::ActionNotFound, with: :route_not_found
  rescue_from ActionView::MissingTemplate, with: :bad_request
  rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
  rescue_from ActiveRecord::RecordNotSaved, with: :not_acceptable
  rescue_from ActionController::RoutingError, with: :route_not_found
  rescue_from AbstractController::DoubleRenderError, with: :bad_request
  rescue_from CanCan::AccessDenied, with: :not_authorized

  def not_authorized(error)
    logger.error "not_authorized #{error}"
    respond_to do |format|
      format.html { render template: "errors/not_authorized", status: 401 }
      format.json { render json: { error: "Not Authorized", status: 401 }, status: 401 }
      format.all { render nothing: true, status: 401 }
    end
  end

  def resource_forbidden(error)
    logger.error "resource_forbidden #{error}"
    respond_to do |format|
      format.html { render template: "errors/not_authorized", status: 403 }
      format.json { render json: { error: "Forbidden", status: 403 }, status: 403 }
      format.all { render nothing: true, status: 403 }
    end
  end

  def resource_not_found(error)
    logger.error "resource_not_found #{error}"
    respond_to do |format|
      format.html { render template: "errors/resource_not_found", status: 404 }
      format.json { render json: { error: "Resource Not Found", status: 404 }, status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end

  def route_not_found(error)
    logger.error "route_not_found #{error}"
    respond_to do |format|
      format.html { render template: "errors/route_not_found", status: 404 }
      format.json { render json: { error: "Route Not Found" }, status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end

  def unsupported_version(error)
    logger.error "unsupported_version #{error}"
    respond_to do |format|
      format.html { render template: "errors/unsupported_version", status: 404 }
      format.json { render json: { error: "Unsupported Version", status: 404 }, status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end

  def not_acceptable(error)
    logger.error "not_acceptable #{error}"
    logger.error error.backtrace.join("\n") unless error.backtrace.nil?
    respond_to do |format|
      format.html { render template: "errors/not_acceptable", status: 406 }
      format.json { render json: { error: "Not Acceptable", status: 406 }, status: 406 }
      format.all { render nothing: true, status: 406 }
    end
  end

  def bad_request(error)
    logger.error "bad_request #{error}"
    logger.error error.backtrace.join("\n") unless error.backtrace.nil?
    respond_to do |format|
      format.html { render template: "errors/bad_request", status: 400 }
      format.json { render json: { error: "Bad Request", status: 400 }, status: 400 }
      format.all { render nothing: true, status: 400 }
    end
  end

  def unknown_error(error)
    logger.error "unknown_error #{error}"
    logger.error error.backtrace.join("\n") unless error.backtrace.nil?
    respond_to do |format|
      format.html { render template: "errors/unknown_error", status: 500 }
      format.json { render json: { error: "Unknown Error", status: 500 }, status: 500 }
      format.all { render nothing: true, status: 500 }
    end
  end

  RUBY
end
Comments

Sign up or Login to leave a comment.