diff options
author | franck cuny <franck@lumberjaph.net> | 2010-09-01 16:18:54 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-09-01 16:18:54 +0200 |
commit | 7b44e7ebef9f12323301af4aa38f6678cccc9dad (patch) | |
tree | cdb20d642ff1f908f549202aaebf948a3951850d | |
parent | format (diff) | |
download | net-http-api-spec-7b44e7ebef9f12323301af4aa38f6678cccc9dad.tar.gz |
try catch
Diffstat (limited to '')
-rw-r--r-- | lib/Net/HTTP/API/Spec.pm | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/Net/HTTP/API/Spec.pm b/lib/Net/HTTP/API/Spec.pm index 39f9680..6ee175b 100644 --- a/lib/Net/HTTP/API/Spec.pm +++ b/lib/Net/HTTP/API/Spec.pm @@ -1,32 +1,50 @@ package Net::HTTP::API::Spec; -use JSON; use Moose; + use IO::All; +use JSON; use Carp; +use Try::Tiny; use Net::HTTP::API::Core; sub new_from_spec { my ($class, $spec_file) = @_; + # TODO: + # - load from an url if (! -f $spec_file) { Carp::confess ("$spec_file does not exists"); } - my $content < io($spec_file); - my $spec = JSON::decode_json($content); + my ($content, $spec); + + $content < io($spec_file); + + try { + $spec = JSON::decode_json($content); + } + catch { + Carp::Confess( "unable to parse JSON spec: " . $_ ); + }; my $net_api_class = Class::MOP::Class->create_anon_class( superclasses => ['Net::HTTP::API::Core']); - my $net_api_object = $net_api_class->new_object; + my $net_api_object; + try { + $net_api_object = $net_api_class->new_object; + + $net_api_object = _declare_api($net_api_object, $spec->{declare}); + $net_api_object = _add_methods($net_api_object, $spec->{methods}); - $net_api_object = _declare_api($net_api_object, $spec->{declare}); - $net_api_object = _add_methods($net_api_object, $spec->{methods}); + }catch{ + Carp::Confess("unable to create new Net::HTTP::API object: ".$_); + }; - $net_api_object; + return $net_api_object; } sub _declare_api { |