Handling Param errors
5nyper opened this issue · comments
I'm not quite sure how to handle Unhandled exception: Invalid request query string
I've tried checking if req.get.length == 0
but no such luck, any ideas?
Can you show me the query string (the part of the url after '?'), which is causing the issue?
The error is thrown when you have something like this
GET /hello?a=1&b
note that b doesn't have a value assigned to it. To my knowledge, this is an invalid url(I might be wrong and am unsure how to interpret this).
I should probably handle this error(invalid url) in the framework instead of giving a runtime exception which shuts down the server
Yes, it is valid:
http://stackoverflow.com/questions/4557387/is-a-url-query-parameter-valid-if-it-has-no-value
Frameworks like ASP.NET MVC make it as key-less value (to me it's weird). I think it should simply be allowed and the user should check for nil himself.
Yeah, I get the error when its like example.com/bin2txt?binary
Any way to handle this in the meantime?
not really, let me do a quick fix
I've made the fix.
on the following request
example.com/bin2txt?binary
the request.get hash will contain a key value pair
binary = true
so, you can do
if request.get.fetch("binary", false)
# something
end
This actually raises problems and compile errors with my own code, I have this:
if req.get.fetch("binary", false)
ok(Bin.new("").to_s)
else
str = ""
req.get["binary"].tr("%20"," ").split("").each { |i|
if i == " "
str += "00" + i[0].ord.to_s(2) + " "
else
str += "0" + i[0].ord.to_s(2) + " "
end
}
ok(Bin.new(str).to_s)
end
and I cant perform .tr or .split since it's a boolean :/
So what would be an acceptable solution?
request.get["binary"] = binary
seem okay to you?
I've changed the code so it sets value less params to "". Your code should work now.
yeah that worked for the previous exception, but now with bin2txt?binary=
results in Unhandled exception: Index out of bounds
yeah I need to rewrite the parameter parsing logic. I'll do it as soon as possible
Sounds good, thanks!
you can now access query string directly by request.query_string. Currently get and post hashes get populated with parameters of the form x=y. You can parse the query string manually for ?=a, ?a= and ?a type parameters. I'll add automatic parsing for these later.
P.S. query string for POST requests is stored in request.body
Nice
request.post and request.get now get populated correctly
?=a => request.get[""] = a
?a= => request.get["a"] = ""
?a => request.get["a"] = ""
Note that post and get objects now map between String and Array(String). Each key can have multiple values. eg ?a=pqr&a=xyz will fill request.get["a"] with ["pqr", "xyz"]
request.get[key] will still return a string (first item in the array)
If you want the array, you can do request.get.fetchAll(key)
So, for ?a=pqr&a=xyz,
requset.get["a"] # = "pqr"
request.get.fetchAll["a"] # = ["pqr", "xyz"]
Nice job!