Skip to content

Instantly share code, notes, and snippets.

@paul
Created December 23, 2008 19:07
Show Gist options
  • Save paul/39422 to your computer and use it in GitHub Desktop.
Save paul/39422 to your computer and use it in GitHub Desktop.
class Exceptions < Application
skip_before :parse_supplied_sscj1
skip_before :ensure_authenticated
provides :sscj1
# ...
# handle RecordInvalid exceptions (406)
def record_invalid
@errors = request.exceptions.first.errors
rename_error_fields!
render
end
# handle client errors
def client_error
# Re-Raise so we get the pretty merb error document instead.
raise request.exceptions.first if content_type == :html
@exceptions = request.exceptions
@show_details = Merb::Config[:exception_details]
# Leave the :standard_error part, so that the other actions that call
# this know what template to render
render :client_error
end
# Everything else (500)
def standard_error
# Re-Raise so we get the pretty merb error document instead.
raise request.exceptions.first if content_type == :html
@exceptions = request.exceptions
@show_details = Merb::Config[:exception_details]
# Leave the :standard_error part, so that the other actions that call
# this know what template to render
render :standard_error
end
# We want some of the error fields to show up
# under the service attribute names, rather than
# the database column names, so here's what to
# rename them to
ERROR_FIELDS_TO_RENAME = {
:template_path => :template_href
}
def rename_error_fields!
ERROR_FIELDS_TO_RENAME.each do |from, to|
v = @errors.delete(from)
@errors[to] = v
end
end
end
<%= j( {
:_type => "ResourceInvalid",
:request_uri => request.env['REQUEST_URI'],
:parameters => JSON.parse(request.raw_post),
:errors => @errors.to_hash.map { |f,msg| {:field => f, :messages => msg} }
} ) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment