Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Caching] TypeError: can't dump hash with default proc #47

Closed
johnmckinght opened this issue Jul 14, 2016 · 5 comments
Closed

[Caching] TypeError: can't dump hash with default proc #47

johnmckinght opened this issue Jul 14, 2016 · 5 comments

Comments

@johnmckinght
Copy link

johnmckinght commented Jul 14, 2016

hi,
i have an example rails api only app and able to access the resource with flexirest.
at this point when i add relation to the api like; has_many todo_list, the result request from flexirest throwing an error TypeError: can't dump hash with default proc
here is the verbose log from rails console

user = TodoUser.new
user.find(1)
Flexirest TodoUser:/users/1 - Trying to read from cache
Flexirest TodoUser#find - Etag cached copy found with etag W/"41e531b5dbb2eed26d6adb311236335e"
Flexirest TodoUser#find - Requesting http://dev0.todo.org/api/v1/users/1
Flexirest Verbose Log:
Request
GET /api/v1/users/1 HTTP/1.1
If-None-Match : W/"41e531b5dbb2eed26d6adb311236335e"
Accept : application/hal+json, application/json;q=0.5
Authorization : Token token=(redacted),email=admin@todo.org
Content-Type : application/json; charset=utf-8
Body:
{}
Response
<< Status : 200
<< date : Thu, 14 Jul 2016 03:28:39 GMT
<< server : Apache/2.2.22 (Debian)
<< cache-control : max-age=0, private, must-revalidate
<< etag : W/"7ad58789e399fd597f2164af5a614395"
<< x-frame-options : SAMEORIGIN
<< x-xss-protection : 1; mode=block
<< x-content-type-options : nosniff
<< x-runtime : 0.028518
<< x-request-id : 4e888626-7044-4563-84e8-ab203f8dd4b3
<< x-powered-by : Phusion Passenger 5.0.28
<< status : 200 OK
<< keep-alive : timeout=5, max=100
<< connection : Keep-Alive
<< transfer-encoding : chunked
<< content-type : application/json; charset=utf-8
<< Body:
{"id":1,"email":"admin@todo.org","name":"Administrator","username":"admin","activated":true,"activated_at":"2016-07-01T19:25:07.000Z","created_at":"2016-07-01T19:24:55.000Z","updated_at":"2016-07-14T03:28:22.000Z","todo_list":[{"id":1,"name":"testname1","todo_user_id":1,"created_at":"2016-07-06T19:01:07.000Z","updated_at":"2016-07-06T19:12:27.000Z"},{"id":2,"name":"testname2","todo_user_id":1,"created_at":"2016-07-06T19:13:27.000Z","updated_at":"2016-07-06T19:21:43.000Z"}]}
Flexirest TodoUser#find - Response received 605 bytes
Flexirest TodoUser:/users/1 - Writing to cache
Flexirest (48.5ms) TodoUser#find
TypeError: can't dump hash with default proc
from /home/dev0/.rvm/gems/ruby-2.3.0/gems/flexirest-1.3.22/lib/flexirest/caching.rb:73:in dump' from /home/dev0/.rvm/gems/ruby-2.3.0/gems/flexirest-1.3.22/lib/flexirest/caching.rb:73:inwrite_cached_response'
from /home/dev0/.rvm/gems/ruby-2.3.0/gems/flexirest-1.3.22/lib/flexirest/request.rb:215:in `block (2 levels) in call'
--snipp

i do quick investigating flexirest/caching.rb line 73 and little google find myself here its about error of marshal dump that can't be done if it's an Object.new/Lambda i don't quite understand as i never use Marshal before.

now the quick fix for me is just rescue the error and implement suggest from the link like so
in a write_cached_response method i alter just before cache_store.write was execute

begin
    cache_store.write(key, Marshal.dump(cached_response), {}) if cached_response.etag.present? || cached_response.expires
rescue TypeError
    cached_response.result = {}.merge(result)
    cache_store.write(key, Marshal.dump(cached_response), {}) if cached_response.etag.present? || cached_response.expires
end

i was able to handle the error and it works but honestly is this ok?
thanks

@johnmckinght
Copy link
Author

I'm sorry i feel so stupid
the error occur because i assign user = TodoUser.new then call user.find(1) that's why Marshal.dump(cached_respond) in flexirest/caching.rb fail.
but if i directly call TodoUser.find(1) instead it's fine.

@andyjeffries
Copy link
Collaborator

Ahhh. Most of the time, code like that will have worked, but I guess it's a weirdness of a method when passing through a single integer. I'll try to add a test case, see if it fails.

@andyjeffries
Copy link
Collaborator

Found and fixed the bug, let me know if it works for you. However, TodoUser.find(1) is the more normal way of writing it.

@andyjeffries
Copy link
Collaborator

v1.3.24 has the fix.

@johnmckinght
Copy link
Author

yes @andyjeffries thanks it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants