Skip to content

Commit

Permalink
adding errors descriptor file + respond_with errors functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
brutski committed Aug 13, 2014
1 parent d7bbc53 commit 0ccd1a9
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 19 deletions.
7 changes: 5 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '3.2.0'

gem 'crichton', git: 'git@github.com:mdsol/crichton.git', tag: '0.0.1'
gem 'crichton', git: 'git@github.com:mdsol/crichton.git', tag: '0.0.3'
gem 'launchy'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'rack', '=1.4.1' # To silence deprecation warnings.

gem 'rack-accept', '~> 0.4'
gem 'strongly_typed_parameters', '~> 0.0'

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'nokogiri'
Expand All @@ -19,7 +22,7 @@ gem 'yajl-ruby', '~> 1.2.0'
gem 'faraday', '~> 0.9.0'

group :test, :development do
gem 'debugger', '~> 1.5.0'
# gem 'debugger', '~> 1.5.0'
end

group :test do
Expand Down
34 changes: 17 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
GIT
remote: git@github.com:mdsol/crichton.git
revision: ad53dbea77ec60706367af763e80b3ee17bd7608
tag: 0.0.1
revision: c8b05f00dd92cc811b44f742bfd3981803aeabd5
tag: 0.0.3
specs:
crichton (0.0.1)
crichton (0.0.3)
activesupport (>= 3.2.0)
addressable (~> 2.3.0)
builder (>= 3.0.0)
colorize (~> 0.6.0)
dice_bag (~> 0.7.0)
dice_bag (~> 0.8)
diffy (~> 3.0.1)
i18n (>= 0.6.5)
nokogiri (>= 1.6.0)
Expand Down Expand Up @@ -48,26 +48,19 @@ GEM
arel (3.0.3)
builder (3.0.4)
colorize (0.6.0)
columnize (0.8.9)
crack (0.4.2)
safe_yaml (~> 1.0.0)
debugger (1.5.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.2.0)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.4)
dice_bag (0.7.0)
dice_bag (0.8.0)
rake
diff-lcs (1.2.5)
diffy (3.0.5)
diffy (3.0.6)
equivalent-xml (0.3.0)
nokogiri (>= 1.4.3)
erubis (2.7.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
hike (1.2.3)
i18n (0.6.9)
i18n (0.6.11)
journey (1.0.4)
jquery-rails (3.1.1)
railties (>= 3.0, < 5.0)
Expand All @@ -83,10 +76,12 @@ GEM
mini_portile (0.6.0)
multi_json (1.10.1)
multipart-post (2.0.0)
nokogiri (1.6.2.1)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
polyglot (0.3.5)
rack (1.4.1)
rack-accept (0.4.5)
rack (>= 0.4)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.4)
Expand Down Expand Up @@ -129,12 +124,16 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.9)
strongly_typed_parameters (0.0.1)
actionpack (~> 3.0)
activemodel (~> 3.0)
railties (~> 3.0)
thor (0.14.6)
tilt (1.4.1)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.39)
tzinfo (0.3.41)
webmock (1.13.0)
addressable (>= 2.2.7)
crack (>= 0.3.2)
Expand All @@ -145,16 +144,17 @@ PLATFORMS

DEPENDENCIES
crichton!
debugger (~> 1.5.0)
equivalent-xml (~> 0.3.0)
faraday (~> 0.9.0)
jquery-rails
launchy
nokogiri
rack (= 1.4.1)
rack-accept (~> 0.4)
rails (= 3.2.0)
rspec (~> 2.13.0)
simplecov (~> 0.7.1)
sqlite3
strongly_typed_parameters (~> 0.0)
webmock (~> 1.13.0)
yajl-ruby (~> 1.2.0)
74 changes: 74 additions & 0 deletions api_descriptors/sample_errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
id: Sample_errors

doc: Describes the semantics, states and state transitions associated with Errors.

links:
self: Sample_errors
help: Sample_errors/help

semantics:
title:
doc: Title SHOULD describe the error in a concise, generic manner.
href: http://alps.io/schema.org/Text
details:
doc: Error explanation and reason for the error.
href: http://alps.io/schema.org/Text
error_code:
doc: Error code is the service's internal error code that describes the error.
href: http://example.org/profiles/ErrorCodes
http_status:
doc: HTTP status is the HTTP status returned, if the service returned one.
href: http://alps.io/schema.org/Integer
retry_after:
doc: This semantic element specifies to the client how long to wait before making another request.
href: http://alps.io/schema.org/Integer
logref:
doc: It is a unique ID used for logging or otherwise tracking the error.
href: http://alps.io/schema.org/Text
stack_trace:
doc: Error stacktrace.
href: http://alps.io/schema.org/Text
help:
doc: The link directs the user to a document that describes the error.
href: http://alps.io/schema.org/URL
contact:
doc: Contact link should provide a contact.
href: http://alps.io/schema.org/Text

safe:
describes_link:
name: describes
doc: The link to the resource that the error describes.

resources:
sample_errors:
doc: Describes an error.
links:
self: Sample_errors#error
help: Sample_errors/help
descriptors:
- href: title
- href: details
- href: error_code
- href: http_status
- href: retry_after
- href: logref
- href: stack_trace
- href: describes_link
states:
default:
transitions:
describes_link:
name: describes
next:
- location: exit
help_link:
name: help
next:
- default

http_protocol:
describes_link:
uri_source: describes_url
help_link:
uri_source: help_url
22 changes: 22 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
require 'sample_errors'

class ApplicationController < ActionController::Base
protect_from_forgery

rescue_from "StandardError" do |e|
error_cause = {ActionController::UnpermittedParameters => :unprocessable_entity,
ActiveRecord::RecordNotFound => :not_found,
}[e.exception.class]
error_cause ||= :unprocessable_entity
errors = Sample_errors.new({
title: e.message,
details: '',
error_code: error_cause,
http_status: Rack::Utils.status_code(error_cause),
stack_trace: e.backtrace.first,
controller: self
})

respond_to do |format|
format.html { render text: errors.to_media_type(:xhtml, { semantics: :styled_microdata }), status: error_cause }
format.hale_json { render text: errors.to_media_type(:hale_json), status: error_cause }
end
end
end
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Application < Rails::Application
# Enable the asset pipeline
config.assets.enabled = true

require 'rack/accept'
config.middleware.use Rack::Accept
config.action_controller.action_on_unpermitted_parameters = :raise

# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
Expand Down
14 changes: 14 additions & 0 deletions lib/sample_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'crichton/tools/base_errors'

class Sample_errors < Crichton::Tools::BaseErrors
include Crichton::Representor::State
represents :sample_errors

def initialize(data)
super(data)
end

def describes_url
controller.request.path
end
end

0 comments on commit 0ccd1a9

Please sign in to comment.