Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Map ActiveRecord::RecordNotUnique #19

Merged
merged 4 commits into from

3 participants

@sj26

Turns into a RocketPants::Conflict which uses HTTP 409 Conflict.

@Sutto Sutto merged commit 86dc70e into Sutto:master
@fredwu

Should probably add this to the readme ;)

Owner

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 14, 2012
  1. @sj26

    Whitespace~

    sj26 authored
  2. @sj26

    Add a conflict error

    sj26 authored
  3. @sj26
  4. @sj26

    Add a spec

    sj26 authored
This page is out of date. Refresh to see the latest.
View
3  lib/rocket_pants/active_record.rb
@@ -5,6 +5,7 @@ module ActiveRecordIntegration
included do
map_error! ActiveRecord::RecordNotFound, RocketPants::NotFound
+ map_error! ActiveRecord::RecordNotUnique, RocketPants::Conflict
map_error!(ActiveRecord::RecordNotSaved) { RocketPants::InvalidResource.new nil }
map_error! ActiveRecord::RecordInvalid do |exception|
RocketPants::InvalidResource.new exception.record.errors
@@ -16,4 +17,4 @@ module ActiveRecordIntegration
end
end
-end
+end
View
21 lib/rocket_pants/errors.rb
@@ -1,25 +1,25 @@
module RocketPants
require 'rocket_pants/error'
-
+
# A simple map of data about errors that the rocket pants system can handle.
class Errors
-
+
@@errors = {}
-
+
# Returns a hash of all known errors, keyed by their error name.
# @return [Hash{Symbol => RocketPants::Error}] the hash of known errors.
def self.all
@@errors.dup
end
-
+
# Looks up a specific error from the given name, returning nil if none are found.
# @param [#to_sym] name the name of the error to look up.
# @return [Error, nil] the error class if found, otherwise nil.
def self.[](name)
@@errors[name.to_sym]
end
-
+
# Adds a given Error class in the list of all errors, making it suitable
# for lookup via [].
# @see Errors[]
@@ -27,7 +27,7 @@ def self.[](name)
def self.add(error)
@@errors[error.error_name] = error
end
-
+
# Creates an error class to represent a given error state.
# @param [Symbol] name the name of the given error
# @param [Hash] options the options used to create the error class.
@@ -47,7 +47,7 @@ def self.register!(name, options = {})
add klass
klass
end
-
+
# The default set of exceptions.
register! :throttled, :http_status => :service_unavailable
register! :unauthenticated, :http_status => :unauthorized
@@ -55,7 +55,8 @@ def self.register!(name, options = {})
register! :not_implemented, :http_status => :service_unavailable
register! :not_found, :http_status => :not_found
register! :bad_request, :http_status => :bad_request
-
+ register! :conflict, :http_status => :conflict
+
end
class InvalidResource < RocketPants::Error
@@ -78,7 +79,7 @@ def context
end
Errors.add self
-
+
end
-
+
end
View
3  lib/rocket_pants/locale/en.yml
@@ -7,4 +7,5 @@ en:
not_implemented: "The feature you requested has not yet been implemented and hence is currently unavailable."
not_found: "The requested resource could not be found."
bad_request: "The data given to this server does not meet our criteria."
- invalid_resource: "The current resource was deemed invalid."
+ invalid_resource: "The current resource was deemed invalid."
+ conflict: "The request conflicts with an existing resource."
View
9 spec/integration/active_record_spec.rb
@@ -46,4 +46,11 @@ def action_is(&blk)
end
end
-end
+ it 'should automatically map ActiveRecord::RecordNotUnique' do
+ Fish.create! :token => "a"
+ action_is { Fish.create! :token => "a" }
+ get :test_data
+ content['error'].should == 'conflict'
+ end
+
+end
View
2  spec/support/models.rb
@@ -9,6 +9,7 @@
t.string :name
t.string :latin_name
t.integer :child_number
+ t.string :token
end
fish.define_model do
@@ -16,4 +17,5 @@
# Yes, I know it's technically not right.
validates :latin_name, :length => {:minimum => 5}, :format => /\A(\w+) (\w+)\Z/
validates :child_number, :numericality => true
+ validates :token, :unique => true, :allow_nil => true
end
Something went wrong with that request. Please try again.