extending the serializer
I am trying to write a new serializer plugin called `Dancer::Serializer::Tgz` modeled after Data::Dumper, JSON, etc. The idea is that, in conjunction with `Dancer::Plugin::REST` and `Dancer::Serializer::Mutable`, the user would be able to request data in .tgz format on demand like so -- $ curl http://example.com/employees.tgz Here is my baby code, and it gives me a rather unhelpful "an internal error occurred". What am I doing wrong? Note: in this case, only the serializer (downloading data) makes sense; the deserializer doesn't make any sense. ----- package Dancer::Serializer::Tgz; use strict; use warnings; use Carp; use Dancer ':syntax'; use Archive::Tar; use base 'Dancer::Serializer::Abstract'; # helpers sub from_tgz { my ($string) = @_; my $s = Dancer::Serializer::Tgz->new; $s->deserialize($string); } sub to_tgz { my ($data) = @_; my $s = Dancer::Serializer::Tgz->new; $s->serialize($data); } sub serialize { my ($self, $entity) = @_; my $tar = Archive::Tar->new; $tar->add_data('result', to_json($entity)); my $file = '/tmp/result.tgz'; $tar->write($file, COMPRESS_GZIP); send_file($file, content_type => 'application/gzip', system_path => 1); } sub deserialize { my ($self, $content) = @_; croak "not yet implemented"; } sub content_type {'application/gzip'} 1;
On Jun 7, 2011, at 4:51 PM, Mr. Puneet Kishor wrote:
I am trying to write a new serializer plugin called `Dancer::Serializer::Tgz` modeled after Data::Dumper, JSON, etc. The idea is that, in conjunction with `Dancer::Plugin::REST` and `Dancer::Serializer::Mutable`, the user would be able to request data in .tgz format on demand like so --
$ curl http://example.com/employees.tgz
Here is my baby code, and it gives me a rather unhelpful "an internal error occurred". What am I doing wrong?
After much fiddling, I was able to see the following errors -- in my Apache error log, I get Deep recursion on subroutine "Dancer::Error::render" at ../Dancer.pm line 312. Deep recursion on subroutine "Dancer::Error::_render_serialized" at ../Dancer/Error.pm line 167. Deep recursion on subroutine "Dancer::Serializer::Tgz::serialize" at ../Dancer/Error.pm line 177. Deep recursion on subroutine "Dancer::_send_file" ../Dancer.pm line 140. If I uncomment the `sub content_type {'application/gzip'}` I also get Subroutine content_type redefined at ../Dancer/Serializer/Tgz.pm line 43. Also, if I comment the `send_file` command in serialize(), I actually do get a result.tgz. If I untar that result.tgz, I see the following text { "error" : "Serializer (Dancer::Serializer::Tgz) failed at serializing Dancer::Error=HASH(0x100a93008):\nencountered object 'Dancer::Error=HASH(0x100a93008)', but neither allow_blessed nor convert_blessed settings are enabled at (eval 71) line 153.\n" } What am I doing wrong?
Note: in this case, only the serializer (downloading data) makes sense; the deserializer doesn't make any sense.
-----
package Dancer::Serializer::Tgz;
use strict; use warnings; use Carp; use Dancer ':syntax'; use Archive::Tar; use base 'Dancer::Serializer::Abstract';
# helpers
sub from_tgz { my ($string) = @_; my $s = Dancer::Serializer::Tgz->new; $s->deserialize($string); }
sub to_tgz { my ($data) = @_; my $s = Dancer::Serializer::Tgz->new; $s->serialize($data); }
sub serialize { my ($self, $entity) = @_;
my $tar = Archive::Tar->new; $tar->add_data('result', to_json($entity));
my $file = '/tmp/result.tgz'; $tar->write($file, COMPRESS_GZIP); send_file($file, content_type => 'application/gzip', system_path => 1); }
sub deserialize { my ($self, $content) = @_; croak "not yet implemented"; }
sub content_type {'application/gzip'}
1;
On Wednesday 08 June 2011 00:03:44 Mr. Puneet Kishor wrote:
On Jun 7, 2011, at 4:51 PM, Mr. Puneet Kishor wrote:
I am trying to write a new serializer plugin called `Dancer::Serializer::Tgz` modeled after Data::Dumper, JSON, etc. The idea is that, in conjunction with `Dancer::Plugin::REST` and `Dancer::Serializer::Mutable`, the user would be able to request data in .tgz format on demand like so --
$ curl http://example.com/employees.tgz
Here is my baby code, and it gives me a rather unhelpful "an internal error occurred". What am I doing wrong?
After much fiddling, I was able to see the following errors --
in my Apache error log, I get
Deep recursion on subroutine "Dancer::Error::render" at ../Dancer.pm line 312. Deep recursion on subroutine "Dancer::Error::_render_serialized" at ../Dancer/Error.pm line 167. Deep recursion on subroutine "Dancer::Serializer::Tgz::serialize" at ../Dancer/Error.pm line 177. Deep recursion on subroutine "Dancer::_send_file" ../Dancer.pm line 140.
I suspect that the call to send_file() is generating an error of some sort - which gets passed to the serializer to serialize... recursion ahoy.
If I uncomment the `sub content_type {'application/gzip'}` I also get
Subroutine content_type redefined at ../Dancer/Serializer/Tgz.pm line 43.
Ah - you've got "use Dancer ':syntax';" before "use base 'Dancer::Serializer::Abstract';", so Dancer has already exported the content_type() keyword into your package. You can probably fix it with: use Dancer qw(:syntax !content_type);
Also, if I comment the `send_file` command in serialize(), I actually do get a result.tgz. If I untar that result.tgz, I see the following text
{ "error" : "Serializer (Dancer::Serializer::Tgz) failed at serializing Dancer::Error=HASH(0x100a93008):\nencountered object 'Dancer::Error=HASH(0x100a93008)', but neither allow_blessed nor convert_blessed settings are enabled at (eval 71) line 153.\n" }
What am I doing wrong?
For debugging, I'd have the serializer log what it's about to serialize; it seems there's an error somewhere, and it's received a Dancer::Error object, and tried to serialize it with to_json() which then complained that convert_blessed isn't enabled. Or you could add a bit of code to special-case serialisation of D::E objects, e.g.: if (ref $entity eq 'Dancer::Error') { $entity = { error => $entity->message }; } That might reveal the real problem. -- David Precious ("bigpresh") http://www.preshweb.co.uk/ "Programming is like sex. One mistake and you have to support it for the rest of your life". (Michael Sinz)
participants (2)
-
David Precious -
Mr. Puneet Kishor