frc-frecon / frecon

An API for building scouting apps for FRC competitions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting 422s

vmai opened this issue · comments

I'm getting 422s when I try to submit a new competition or teams list from the respective pages. This started happening when I pulled frecon this morning. Everything works fine when I manually submit these in the frecon console.

Could you supply a sample request made by the implementation?

I've done a little bit of chatting with @fishyfish235 after returning from water slides and it seems like it's a problem with request handling.

I sent him a patch to test out and it looks like, for him, the process_request function returns a hash that looks just about right; for competitions, the hash has "name" and "location" fields (both with String values), and for teams, the hash has a "number" field with a String value.

I think it's probably a problem with how the fields are getting converted from Strings in the post_data Hash into attribute names for the new object. It seems like those fields (which are the required ones) aren't getting stored properly, or at least aren't present for the validations.

From here, I see a few options:

  1. We grab the values for the required attributes from the post_data hash and give those to the [model].new call.
  2. We convert all of the keys in the Hash from Strings to Symbols. We could then even pass the post_data Hash through a filtering function and filter out any bad key-value pairs.
  3. Find another function that Mongoid::Attributes::Dynamic supplies which allows validations to take place.

My answers to each proposed solution:

  1. This really should not be needed. We should figure out the actual cause of the problem before making a change like this, which means reading the actual error output.
  2. Not needed. See the terminal sample below.
  3. I don't follow what you mean. Do you mean a different way of saving or validating ourselves?
[1] pry(FReCon)> post_data = {"number" => 700, "name" => "Seven Hundreds", "location" => "New York, NY"}

=> {"number"=>700, "name"=>"Seven Hundreds", "location"=>"New York, NY"}
[2] pry(FReCon)> team = Team.new
=> #<FReCon::Team _id: 53d679af527d872510000001, created_at: nil, updated_at: nil, number: nil, location: nil, logo_path: nil, name: nil>
[3] pry(FReCon)> team.attributes = post_data
=> {"number"=>700, "name"=>"Seven Hundreds", "location"=>"New York, NY"}
[4] pry(FReCon)> tea
NameError: undefined local variable or method `tea' for FReCon:Module
from (pry):4:in `__binding__'
[5] pry(FReCon)> team
=> #<FReCon::Team _id: 53d679af527d872510000001, created_at: nil, updated_at: nil, number: 700, location: "New York, NY", logo_path: nil, name: "Seven Hundreds">
[6] pry(FReCon)> team.save
D, [2014-07-28T12:26:34.179333 #4133] DEBUG -- :   MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 2.5875ms
D, [2014-07-28T12:26:34.201722 #4133] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=frecon collection=f_re_con_teams selector={"number"=>700} flags=[] limit=-1 skip=0 batch_size=nil fields={:_id=>1} runtime: 21.8427ms
D, [2014-07-28T12:26:34.203577 #4133] DEBUG -- :   MOPED: 127.0.0.1:27017 INSERT       database=frecon collection=f_re_con_teams documents=[{"_id"=>BSON::ObjectId('53d679af527d872510000001'), "number"=>700, "name"=>"Seven Hundreds", "location"=>"New York, NY", "updated_at"=>2014-07-28 16:26:34 UTC, "created_at"=>2014-07-28 16:26:34 UTC}] flags=[]
D, [2014-07-28T12:26:34.203652 #4133] DEBUG -- :                          COMMAND      database=frecon command={:getlasterror=>1, :w=>1} runtime: 0.7763ms
=> true
[7] pry(FReCon)> quit

In order to debug this, I suggest the following routes:

  1. Find the errors array in the response and post what errors are in that array.
  2. Drop your current database, as I made a backwards-incompatible change recently because I had accidentally written it such that all data was stored in the same collection of the database. That was dumb. It could very well be that uniqueness validations are failing.
  3. Apply a patch that changes save into save! so that an error will be thrown if it fails to save.

This is what is in the errors array: {"errors":["Location can't be blank","Name can't be blank"]} I'm getting a similar errors array when trying to submit a teams list as well. The fields are most definitely not blank when I'm submitting these things though. Dropping the database also also doesn't seem to be affecting it as I'm still getting the same errors.

I was able to recreate the situation by using curl, so now I can fix it.

Let me know if it works now.

Yep, works now.