-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding errors descriptor file + respond_with errors functionality
- Loading branch information
Showing
6 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |