Skip to content

Custom Error Pages #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/assets/javascripts/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/* local */
@import "variables";
@import "playtime";
@import "error";
29 changes: 29 additions & 0 deletions app/assets/stylesheets/error.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Error pages css
.error {
margin: 150px auto;
text-align: center;
}

.error-code {
bottom: 60%;
color: #2d353c;
font-size: 96px;
line-height: 100px;
}

.error-desc {
font-size: 12px;
color: #647788;
}

.m-b-10 {
margin-bottom: 10px!important;
}

.m-b-20 {
margin-bottom: 20px!important;
}

.m-t-20 {
margin-top: 20px!important;
}
16 changes: 16 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class ErrorsController < ApplicationController
before_action :skip_authorization
layout "error"

def not_found
render(:status => 404)
end

def internal_server_error
render(:status => 500)
end

def unprocessable_entity
render(:status => 422)
end
end
13 changes: 13 additions & 0 deletions app/views/errors/internal_server_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="error">
<div class="error-code m-b-10 m-t-20"><i class="fa fa-warning"></i></div>
<h3 class="font-bold">Oops, something went wrong</h3>
<div class="error-desc">
The server is unavailable for the moment. We're doing everything we can to correct the problem.
<br/>
Please try again later.
<div>
<i class="fa fa-arrow-left"></i>
<%= link_to 'Go back to Homepage', root_path , class:"login-detail-panel-button btn" %>
</div>
</div>
</div>
12 changes: 12 additions & 0 deletions app/views/errors/not_found.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="error">
<div class="error-code m-b-10 m-t-20"><i class="fa fa-warning"></i></div>
<h3 class="font-bold">We couldn't find the page</h3>
<div class="error-desc">
Sorry, but the page you are looking for was either not found or does not exist. <br/>
Try refreshing the page or click the button below to go back to the Homepage.
<div>
<i class="fa fa-arrow-left"></i>
<%= link_to 'Go back to Homepage', root_path , class:"login-detail-panel-button btn" %>
</div>
</div>
</div>
11 changes: 11 additions & 0 deletions app/views/errors/unprocessable_entity.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="error">
<div class="error-code m-b-10 m-t-20"><i class="fa fa-warning"></i></div>
<h3 class="font-bold">The change you wanted was rejected</h3>
<div class="error-desc">
Maybe you tried to change something you didn't have access to.
<div>
<i class="fa fa-arrow-left"></i>
<%= link_to 'Go back to Homepage', root_path , class:"login-detail-panel-button btn" %>
</div>
</div>
</div>
18 changes: 18 additions & 0 deletions app/views/layouts/error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Playtime</title>
<%= csrf_meta_tags %>
<%= favicon_link_tag 'favicon.png' %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= favicon_link_tag 'https://www.playtimeproject.org/wp-content/uploads/2013/03/Logo-no-words-e1363625449703.png'%>
</head>

<body>

<%= yield %>
<div class="footer">
The Playtime Wishlist was built with <i class="fa fa-heart"></i> by <a href="http://rubyforgood.org">Ruby for Good</a>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1

# Serve error pages from the app
config.exceptions_app = self.routes

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# frozen_string_literal: true

Rails.application.routes.draw do
# Error Pages
get '/404', :to => 'errors#not_found'
get '/500', :to => 'errors#internal_server_error'
get '/422', :to => 'errors#unprocessable_entity'

# Pledges
resources :pledges, except: [:new] do
patch :claim
Expand Down
67 changes: 0 additions & 67 deletions public/404.html

This file was deleted.

67 changes: 0 additions & 67 deletions public/422.html

This file was deleted.

66 changes: 0 additions & 66 deletions public/500.html

This file was deleted.

26 changes: 26 additions & 0 deletions spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe ErrorsController, type: :controller do

describe "GET #not_found" do
it "returns http success" do
get :not_found
expect(response).to have_http_status(:not_found)
end
end

describe "GET #internal_server_error" do
it "returns http success" do
get :internal_server_error
expect(response).to have_http_status(:internal_server_error)
end
end

describe "GET #unprocessable_entity" do
it "returns http success" do
get :unprocessable_entity
expect(response).to have_http_status(:unprocessable_entity)
end
end

end