mysociety / popit

DEPRECATED - Development on PopIt has stopped and it is no longer being maintained
https://goo.gl/Vvej4Q
Other
76 stars 33 forks source link

500 errors on PUT updates #814

Closed mhl closed 9 years ago

mhl commented 9 years ago

Some updates to people at the moment are causing this 500 error (from production.log):

CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"
    at ObjectId.cast (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/schema/objectid.js:117:13)
    at /data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:292:29
    at EmbeddedDocument.Document.$__try (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:775:8)
    at init (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:291:16)
    at EmbeddedDocument.Document.init (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:250:3)
    at DocumentArray.cast (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/schema/documentarray.js:145:27)
    at /data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:292:29
    at Model.Document.$__try (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:775:8)
    at init (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:291:16)
    at Model.Document.init (/data/vhost/popit.mysociety.org/popit-2015-03-31T09-47-52/node_modules/popit-api/node_modules/mongoose/lib/document.js:250:3)
127.0.0.1 - - [31/Mar/2015:13:42:46 +0000] "PUT /api/v0.1/persons/4493 HTTP/1.1" 500 125 "-" "python-requests/2.5.3 CPython/2.7.3 Linux/3.2.0-4-amd64"
mhl commented 9 years ago

I've mongoexported the current YNMP database, imported it locally and tried to reproduce this by updating candidate 4493 and I get that same error on a PUT of this data:

{
    "birth_date": null, 
    "contact_details": [], 
    "email": "cllrjamesbarber@gmail.com", 
    "gender": "male", 
    "honorific_prefix": null, 
    "honorific_suffix": null, 
    "identifiers": [], 
    "links": [], 
    "name": "James Barber", 
    "other_names": [], 
    "party_memberships": null, 
    "standing_in": null, 
    "versions": [
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_page_url": "https://www.facebook.com/james.barber.1238292", 
                "facebook_personal_url": "http://facebook.com/james.barber", 
                "gender": "male", 
                "homepage_url": "http://jamesbarber.mycouncillor.org.uk/", 
                "honorific_prefix": "", 
                "honorific_suffix": "", 
                "id": "4493", 
                "identifiers": [], 
                "image": "http://yournextmp.popit.mysociety.org/persons/4493/image/5481e8f6b150e238702c061b", 
                "linkedin_url": "", 
                "name": "James Barber", 
                "other_names": [], 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "", 
                "proxy_image": "http://candidates.127.0.0.1.xip.io:3000/image-proxy/http%3A%2F%2Fyournextmp.popit.mysociety.org%2Fpersons%2F4493%2Fimage%2F5481e8f6b150e238702c061b", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber", 
                "wikipedia_url": ""
            }, 
            "information_source": "Just testing", 
            "timestamp": "2015-03-31T14:07:23.469835", 
            "username": "mark", 
            "version_id": "519e34dcc5952126"
        }, 
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_page_url": "https://www.facebook.com/james.barber.1238292", 
                "facebook_personal_url": "http://facebook.com/james.barber", 
                "gender": "male", 
                "homepage_url": "http://jamesbarber.mycouncillor.org.uk/", 
                "id": "4493", 
                "identifiers": [], 
                "image": "http://yournextmp.popit.mysociety.org/persons/4493/image/5481e8f6b150e238702c061b", 
                "name": "James Barber", 
                "other_names": [], 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "http://www.libdems.org.uk/james_barber", 
                "proxy_image": "http://yournextmp.popit.mysociety.org/image-proxy/http%3A%2F%2Fyournextmp.popit.mysociety.org%2Fpersons%2F4493%2Fimage%2F5481e8f6b150e238702c061b", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber", 
                "wikipedia_url": ""
            }, 
            "information_source": "Updated candidate from official PPC data (libdem)", 
            "timestamp": "2015-01-31T18:17:24.442896", 
            "version_id": "3ed9d0ac09477ad3"
        }, 
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_page_url": "https://www.facebook.com/james.barber.1238292", 
                "facebook_personal_url": "http://facebook.com/james.barber", 
                "gender": "male", 
                "homepage_url": "http://jamesbarber.mycouncillor.org.uk/", 
                "id": "4493", 
                "image": "http://yournextmp.popit.mysociety.org/persons/4493/image/5481e8f6b150e238702c061b", 
                "name": "James Barber", 
                "other_names": [], 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "http://www.libdems.org.uk/james_barber", 
                "proxy_image": "http://yournextmp.popit.mysociety.org/image-proxy/http%3A%2F%2Fyournextmp.popit.mysociety.org%2Fpersons%2F4493%2Fimage%2F5481e8f6b150e238702c061b", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber", 
                "wikipedia_url": ""
            }, 
            "information_source": "http://jamesbarber.mycouncillor.org.uk/", 
            "timestamp": "2015-01-25T20:39:46.113565", 
            "username": "stef", 
            "version_id": "75fea123a67b2958"
        }, 
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_page_url": "", 
                "facebook_personal_url": "http://facebook.com/james.barber", 
                "gender": null, 
                "homepage_url": "", 
                "id": "4493", 
                "image": "http://yournextmp.popit.mysociety.org/persons/4493/image/5481e8f6b150e238702c061b", 
                "name": "James Barber", 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "http://www.libdems.org.uk/james_barber", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber", 
                "wikipedia_url": ""
            }, 
            "information_source": "Updated candidate from official PPC data (libdem)", 
            "timestamp": "2014-12-11T12:13:25.341454", 
            "version_id": "2630eed273b3b9aa"
        }, 
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_page_url": "", 
                "facebook_personal_url": "facebook.com/james.barber", 
                "gender": "", 
                "homepage_url": "", 
                "id": "4493", 
                "image": null, 
                "name": "James Barber", 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "http://www.libdems.org.uk/james_barber", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber", 
                "wikipedia_url": ""
            }, 
            "information_source": "Updated candidate from official PPC data (libdem)", 
            "timestamp": "2014-12-05T17:18:45.397468", 
            "version_id": "71c6dd84e75cb3ea"
        }, 
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_page_url": "", 
                "facebook_personal_url": "facebook.com/james.barber", 
                "gender": "", 
                "homepage_url": "", 
                "id": "4493", 
                "name": "James Barber", 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "http://www.libdems.org.uk/james_barber", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber", 
                "wikipedia_url": ""
            }, 
            "information_source": "Updated candidate from official PPC data (libdem)", 
            "timestamp": "2014-11-23T09:59:46.815649", 
            "version_id": "74162d6c8dc9c392"
        }, 
        {
            "data": {
                "birth_date": null, 
                "email": "cllrjamesbarber@gmail.com", 
                "facebook_personal_url": "facebook.com/james.barber", 
                "homepage_url": "", 
                "id": "4493", 
                "name": "James Barber", 
                "party_memberships": {
                    "2015": {
                        "id": "party:90", 
                        "name": "Liberal Democrats"
                    }
                }, 
                "party_ppc_page_url": "http://www.libdems.org.uk/james_barber", 
                "standing_in": {
                    "2015": {
                        "mapit_url": "http://mapit.mysociety.org/area/65808", 
                        "name": "Dulwich and West Norwood", 
                        "post_id": "65808"
                    }
                }, 
                "twitter_username": "CllrJamesBarber"
            }, 
            "information_source": "Created new candidate from official PPC data (libdem)", 
            "timestamp": "2014-11-22T08:58:14.286996", 
            "version_id": "546f80eea0f8fd03"
        }
    ]
}
chrismytton commented 9 years ago

Fixed by running the following script against the YourNextMP production database to fix the _id fields of the objects in the images array, which accidentally broken previously.

// fix-image-ids.js
var people = db.persons.find();

var re = new RegExp(/^http\:\/\/yournextmp.popit.mysociety.org\/persons\/(\d+)\/image\/(\w+)/);

var needsSaving;
var updated = 0;

people.forEach(function(person) {
  if (person.images) {
    needsSaving = false;
    person.images.forEach(function(image) {
      if (!(image._id instanceof ObjectId)) {
        var result = re.exec(image.url);
        if (result) {
          image._id = ObjectId(result[2]);
        } else {
          image._id = new ObjectId();
        }
        needsSaving = true;
      }
    });
    if (needsSaving) {
      db.persons.save(person);
      updated++;
    }
  }
});

print("Updated", updated, "records");

Run with:

$ mongo popit_yournextmp fix-image-ids.js
mhl commented 9 years ago

Actually, a similar fix but with s/person/organization/ needs to be done too, since the party images have the same problem. I've got a currently failing test case ready on staging in case you want to try that there first.

chrismytton commented 9 years ago

Fixed for the organizations as well, mongo scripts are available here - https://gist.github.com/chrismytton/8991474e2bfa6538a467.