Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use :metadata over extras

  • Loading branch information...
commit e74a34add6b78803f049ae925f9b9bb7272c4495 1 parent c755665
@Sutto authored
View
15 CHANGELOG.md
@@ -2,6 +2,21 @@
**Please Note**: This change log only covers v1.3 forwards - apologies for anything missing prior to that.
+## Version 1.5
+
+* Add in a built in `:invalid_resource` error, with support for passing through error messages.
+* Support for ActiveRecord exceptions out of the box (See the README).
+* Don't hide errors by default in `development` and `test`, respect `config.rocket_pants.pass_through_errors` and `RocketPants.pass_through_errors`.
+* Allow specifying `:metadata` in context on errors / expose and add it directly to the response.
+* Allow specifying `:metadata` in expose on objects.
+* `RocketPants::Base.map_error!` now accepts lambdas as the target value / convertor.
+
+## Version 1.4
+
+* Add in a built in `:bad_request` error.
+* Provide `:base` on `RocketPants::Errors.register!` will set the base class.
+* Fixed integration with `will_paginate` and expanded our integration specs to be better.
+
## Version 1.3
* Fixed a bug where any request with an etag would return `304 Not Modified` when the caching middleware was enabled.
View
2  README.md
@@ -363,7 +363,7 @@ Will look up the translation `rocket_pants.errors.throttled` in your I18N files,
Finally, You can use this to also pass custom values to include in the response, e.g:
```ruby
-error! :throttled, :extras => {:code => 123}
+error! :throttled, :metadata => {:code => 123}
```
Will return something similar to:
View
12 lib/rocket_pants/controller/error_handling.rb
@@ -55,7 +55,7 @@ def lookup_error_message(exception)
name = lookup_error_name(exception)
message = (exception.message == exception.class.name) ? 'An unknown error has occurred.' : exception.message
context = lookup_error_context(exception).reverse_merge(:scope => :"rocket_pants.errors", :default => message)
- I18n.t name, context.except(:extra)
+ I18n.t name, context.except(:metadata)
end
# Lookup error name will automatically handle translating errors to a
@@ -85,11 +85,15 @@ def lookup_error_context(exception)
exception.respond_to?(:context) ? exception.context : {}
end
+ def lookup_error_extras(exception)
+ {}
+ end
+
# Returns extra error details for a given object, making it useable
# for hooking in external exceptions.
- def lookup_error_extras(exception)
+ def lookup_error_metadata(exception)
context = lookup_error_context exception
- context.fetch :extras, {}
+ context.fetch(:metadata, {}).merge lookup_error_extras(exception)
end
# Renders an exception as JSON using a nicer version of the error name and
@@ -115,7 +119,7 @@ def render_error(exception)
render_json({
:error => lookup_error_name(exception).to_s,
:error_description => lookup_error_message(exception)
- }.merge(lookup_error_extras(exception)))
+ }.merge(lookup_error_metadata(exception)))
end
end
View
2  lib/rocket_pants/errors.rb
@@ -72,7 +72,7 @@ def initialize(errors, *args)
def context
super.tap do |ctx|
- extras = (ctx[:extras] ||= {})
+ extras = (ctx[:metadata] ||= {})
extras[:messages] = errors.to_hash if errors
end
end
View
10 spec/rocket_pants/controller/error_handling_spec.rb
@@ -103,14 +103,14 @@
content['hello'].should == 'There'
end
- it 'should default to extracting extras from the context' do
- def error.context; {:extras => {:hello => 'There'}} ; end
+ it 'should default to extracting metadata from the context' do
+ def error.context; {:metadata => {:hello => 'There'}} ; end
get :test_error
content['hello'].should == 'There'
end
it 'should let you pass through data via the context in the controller' do
- controller_class.send(:define_method, :demo_exception) { error! :throttled, :extras => {:hello => "There"}}
+ controller_class.send(:define_method, :demo_exception) { error! :throttled, :metadata => {:hello => "There"}}
get :demo_exception
content['hello'].should == 'There'
end
@@ -129,10 +129,10 @@ def error.context; {:extras => {:hello => 'There'}} ; end
content['error'].should == 'throttled'
end
- it 'should let you register a custom error mapping with extras' do
+ it 'should let you register a custom error mapping with metadata' do
controller_class.error_mapping[TestController::ErrorOfDoom] = lambda do |exception|
RocketPants::Throttled.new(exception).tap do |e|
- e.context = {:extras => {:test => true}}
+ e.context = {:metadata => {:test => true}}
end
end
get :test_error
View
6 spec/rocket_pants/error_spec.rb
@@ -114,13 +114,13 @@ def temporary_constant(items)
o = Object.new
mock(o).to_hash { error_messages }
error = RocketPants::InvalidResource.new(o)
- error.context.should == {:extras => {:messages => error_messages}}
+ error.context.should == {:metadata => {:messages => error_messages}}
end
it 'should not override messages' do
error = RocketPants::InvalidResource.new(error_messages)
- error.context = {:other => true, :extras => {:test => true}}
- error.context.should == {:extras => {:messages => error_messages, :test => true}, :other => true}
+ error.context = {:other => true, :metadata => {:test => true}}
+ error.context.should == {:metadata => {:messages => error_messages, :test => true}, :other => true}
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.