From: Matt Amos Date: Thu, 23 Apr 2009 18:39:36 +0000 (+0000) Subject: Added better error messages on 412 precondition failed. X-Git-Tag: live~8102^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/9ff7ea8d71dedb5b29f6ff0b007568b3e699b196 Added better error messages on 412 precondition failed. --- diff --git a/app/models/node.rb b/app/models/node.rb index 1392fc650..59bba5e60 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -131,19 +131,21 @@ class Node < ActiveRecord::Base # shouldn't be possible to get race conditions. Node.transaction do check_consistency(self, new_node, user) - if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = ? AND current_way_nodes.node_id = ?", true, self.id ]) - raise OSM::APIPreconditionFailedError.new - elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='Node' and member_id=? ", true, self.id]) - raise OSM::APIPreconditionFailedError.new - else - self.changeset_id = new_node.changeset_id - self.visible = false - - # update the changeset with the deleted position - changeset.update_bbox!(bbox) - - save_with_history! - end + way = WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", + :conditions => [ "current_ways.visible = ? AND current_way_nodes.node_id = ?", true, self.id ]) + raise OSM::APIPreconditionFailedError.new("Node #{self.id} is still used by way #{way.id}.") unless way.nil? + + rel = RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", + :conditions => [ "visible = ? AND member_type='Node' and member_id=? ", true, self.id]) + raise OSM::APIPreconditionFailedError.new("Node #{self.id} is still used by way #{way.id}.") unless rel.nil? + + self.changeset_id = new_node.changeset_id + self.visible = false + + # update the changeset with the deleted position + changeset.update_bbox!(bbox) + + save_with_history! end end diff --git a/app/models/relation.rb b/app/models/relation.rb index 36d6943d9..2032e6770 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -236,9 +236,9 @@ class Relation < ActiveRecord::Base Relation.transaction do check_consistency(self, new_relation, user) # This will check to see if this relation is used by another relation - if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='Relation' and member_id=? ", true, self.id ]) - raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is a used in another relation") - end + rel = RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='Relation' and member_id=? ", true, self.id ]) + raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is a used in relation #{rel.id}.") unless rel.nil? + self.changeset_id = new_relation.changeset_id self.tags = {} self.members = [] @@ -249,8 +249,8 @@ class Relation < ActiveRecord::Base def update_from(new_relation, user) check_consistency(self, new_relation, user) - if !new_relation.preconditions_ok? - raise OSM::APIPreconditionFailedError.new + unless preconditions_ok? + raise OSM::APIPreconditionFailedError.new("Cannot update relation #{self.id}: data or member data is invalid.") end self.changeset_id = new_relation.changeset_id self.changeset = new_relation.changeset @@ -262,8 +262,8 @@ class Relation < ActiveRecord::Base def create_with_history(user) check_create_consistency(self, user) - if !self.preconditions_ok? - raise OSM::APIPreconditionFailedError.new + unless self.preconditions_ok? + raise OSM::APIPreconditionFailedError.new("Cannot create relation: data or member data is invalid.") end self.version = 0 self.visible = true diff --git a/app/models/way.rb b/app/models/way.rb index e134d697d..cbbe6ada4 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -199,8 +199,8 @@ class Way < ActiveRecord::Base def update_from(new_way, user) check_consistency(self, new_way, user) - if !new_way.preconditions_ok? - raise OSM::APIPreconditionFailedError.new + unless new_way.preconditions_ok? + raise OSM::APIPreconditionFailedError("Cannot update way #{self.id}: data is invalid.") end self.changeset_id = new_way.changeset_id @@ -213,8 +213,8 @@ class Way < ActiveRecord::Base def create_with_history(user) check_create_consistency(self, user) - if !self.preconditions_ok? - raise OSM::APIPreconditionFailedError.new + unless self.preconditions_ok? + raise OSM::APIPreconditionFailedError("Cannot create way: data is invalid.") end self.version = 0 self.visible = true