]> git.openstreetmap.org Git - rails.git/commitdiff
api06: simplify exception handling and add exception handling to the diff
authorGabriel Ebner <gabriel@svn.openstreetmap.org>
Tue, 13 May 2008 12:15:06 +0000 (12:15 +0000)
committerGabriel Ebner <gabriel@svn.openstreetmap.org>
Tue, 13 May 2008 12:15:06 +0000 (12:15 +0000)
upload.

app/controllers/changeset_controller.rb
app/controllers/node_controller.rb
app/controllers/relation_controller.rb
app/controllers/way_controller.rb
lib/osm.rb

index db3a2629512b0251d0c645d751914be380675dc4..322ce79f7552f024a382061df1e22554650911e0 100644 (file)
@@ -184,5 +184,8 @@ class ChangesetController < ApplicationController
     end
 
     render :text => res.to_s, :content_type => "text/xml"
+
+  rescue OSM::APIError => ex
+    render ex.render_opts
   end
 end
index f1ee8f66b413820f724b963cc0983baa5d5772b4..1e0deb14004bfb5fc6c99f0388c64e92af65a20a 100644 (file)
@@ -72,10 +72,8 @@ class NodeController < ApplicationController
       node.delete_with_history(@user)
     rescue ActiveRecord::RecordNotFound
       render :nothing => true, :status => :not_found
-    rescue OSM::APIAlreadyDeletedError
-      render :text => "", :status => :gone
-    rescue OSM::APIPreconditionFailedError
-      render :text => "", :status => :precondition_failed
+    rescue OSM::APIError => ex
+      render ex.render_opts
     end
   end
 
index dabf6eaa8bef7fbe2a1e9710b32c230ca8d7fa2e..c49ecd4d701733b035271a922e3466c40bba0881 100644 (file)
@@ -49,10 +49,6 @@ class RelationController < ApplicationController
     begin
       relation = Relation.find(params[:id])
       new_relation = Relation.from_xml(request.raw_post)
-      if new_relation.version != relation.version
-        render :text => "Version mismatch: Provided " + new_relation.version.to_s + ", server had: " + relation.version.to_s, :status => :bad_request
-        return
-      end  
 
       if new_relation and new_relation.id == relation.id
        relation.update_from new_relation, user
@@ -62,13 +58,8 @@ class RelationController < ApplicationController
       end
     rescue ActiveRecord::RecordNotFound
       render :nothing => true, :status => :not_found
-    rescue OSM::APIPreconditionFailedError
-      render :text => "", :status => :precondition_failed
-    rescue OSM::APIVersionMismatchError => ex
-      render :text => "Version mismatch: Provided " + ex.provided.to_s +
-       ", server had: " + ex.latest.to_s, :status => :bad_request
-    rescue
-      render :nothing => true, :status => :internal_server_error
+    rescue OSM::APIError => ex
+      render ex.render_opts
     end
   end
 
@@ -77,14 +68,10 @@ class RelationController < ApplicationController
     begin
       relation = Relation.find(params[:id])
       relation.delete_with_history(@user)
-    rescue OSM::APIAlreadyDeletedError
-      render :text => "", :status => :gone
-    rescue OSM::APIPreconditionFailedError
-      render :text => "", :status => :precondition_failed
+    rescue OSM::APIError => ex
+      render ex.render_opts
     rescue ActiveRecord::RecordNotFound
       render :nothing => true, :status => :not_found
-    rescue
-      render :nothing => true, :status => :internal_server_error
     end
   end
 
index 21f2ea76567cc639f669724232e904b510a7a53e..cf1634fa55699cccdae1084536256c3f7c5fc4bd 100644 (file)
@@ -40,6 +40,8 @@ class WayController < ApplicationController
       else
         render :text => "", :status => :gone
       end
+    rescue OSM::APIError => ex
+      render ex.render_opts
     rescue ActiveRecord::RecordNotFound
       render :nothing => true, :status => :not_found
     end
@@ -56,11 +58,8 @@ class WayController < ApplicationController
       else
         render :nothing => true, :status => :bad_request
       end
-    rescue OSM::APIPreconditionFailedError
-      render :text => "", :status => :precondition_failed
-    rescue OSM::APIVersionMismatchError => ex
-      render :text => "Version mismatch: Provided " + ex.provided.to_s +
-       ", server had: " + ex.latest.to_s, :status => :bad_request
+    rescue OSM::APIError => ex
+      render ex.render_opts
     rescue ActiveRecord::RecordNotFound
       render :nothing => true, :status => :not_found
     end
@@ -74,10 +73,8 @@ class WayController < ApplicationController
 
       # if we get here, all is fine, otherwise something will catch below.  
       render :nothing => true
-    rescue OSM::APIAlreadyDeletedError
-      render :text => "", :status => :gone
-    rescue OSM::APIPreconditionFailedError
-      render :text => "", :status => :precondition_failed
+    rescue OSM::APIError => ex
+      render ex.render_opts
     rescue ActiveRecord::RecordNotFound
       render :nothing => true, :status => :not_found
     end
index a8cb103d2f829540bf06f69ae0437f3c3029b11e..8798866e5dc918ab5fc63d94bbf217924cc5effd 100644 (file)
@@ -10,6 +10,7 @@ module OSM
 
   # The base class for API Errors.
   class APIError < RuntimeError
+    def render_opts { :text => "", :status => :internal_server_error } end
   end
 
   # Raised when an API object is not found.
@@ -18,10 +19,12 @@ module OSM
 
   # Raised when a precondition to an API action fails sanity check.
   class APIPreconditionFailedError < APIError
+    def render_opts { :text => "", :status => :precondition_failed } end
   end
 
   # Raised when to delete an already-deleted object.
   class APIAlreadyDeletedError < APIError
+    def render_opts { :text => "", :status => :gone } end
   end
 
   # Raised when the provided version is not equal to the latest in the db.
@@ -31,6 +34,11 @@ module OSM
     end
 
     attr_reader :provided, :latest
+
+    def render_opts
+      { :text => "Version mismatch: Provided " + ex.provided.to_s +
+       ", server had: " + ex.latest.to_s, :status => :bad_request }
+    end
   end
 
   # Helper methods for going to/from mercator and lat/lng.