File Coverage

blib/lib/Activiti/Rest/Response.pm
Criterion Covered Total %
statement 15 40 37.5
branch 0 22 0.0
condition 0 6 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 20 75 26.6


line stmt bran cond sub pod time code
1             package Activiti::Rest::Response;
2 1     1   7 use Activiti::Sane;
  1         3  
  1         6  
3 1     1   415 use Activiti::Rest::Error;
  1         4  
  1         35  
4 1     1   9 use Moo;
  1         2  
  1         4  
5 1     1   403 use JSON qw();
  1         3  
  1         27  
6 1     1   637 use Encode qw();
  1         16027  
  1         574  
7              
8             has content => (
9             is => 'ro',
10             predicate => 'has_content'
11             );
12             has parsed_content => (
13             is => 'ro',
14             predicate => 'has_parsed_content'
15             );
16             has content_type => (
17             is => 'ro',
18             required => 1
19             );
20             has code => (
21             is => 'ro',
22             required => 1
23             );
24              
25             sub from_http_response {
26 0     0 0   my($class,$res)=@_;
27              
28             #status code indicates 'failure'
29 0 0         unless($res->is_success){
30              
31 0           my $code = $res->code;
32              
33             #before version 5.17
34             # { "errorMessage": "", "statusCode": "statusCode" }
35             #from version 5.17
36             # { "message": "", "exception": "" }
37 0           my $content_hash = JSON::decode_json($res->content);
38 0   0       my $exception = $content_hash->{exception} || $content_hash->{errorMessage};
39             #can return multiple values (e.g. 'application/json','charset=utf-8')
40 0           my $ct = $res->content_type;
41 0           my $args = {
42             status_code => $res->code,
43             message => $res->message,
44             content => $res->content,
45             content_type => $ct,
46             error_message => $exception,
47             exception => $exception
48             };
49              
50              
51             #The operation failed. The operation requires an Authentication header to be set. If this was present in the request, the supplied credentials are not valid or the user is not authorized to perform this operation.
52 0 0         if($code eq "401"){
    0          
    0          
    0          
    0          
    0          
    0          
53 0           Activiti::Rest::Error::UnAuthorized->throw($args);
54             }
55              
56             #The operation is forbidden and should not be re-attempted. This does not imply an issue with authentication not authorization, it's an operation that is not allowed. Example: deleting a task that is part of a running process is not allowed and will never be allowed, regardless of the user or process/task state.
57             elsif($code eq "403"){
58 0           Activiti::Rest::Error::Forbidden->throw($args);
59             }
60              
61             #The operation failed.The requested resource was not found.
62             elsif($code eq "404"){
63 0           Activiti::Rest::Error::NotFound->throw($args);
64             }
65              
66             #The operation failed. The used method is not allowed for this resource. Eg. trying to update (PUT) a deployment-resource will result in a 405 status.
67             elsif($code eq "405"){
68 0           Activiti::Rest::Error::MethodNotAllowed->throw($args);
69             }
70              
71             #The operation failed. The operation causes an update of a resource that has been updated by another operation, which makes the update no longer valid. Can also indicate a resource that is being created in a collection where a resource with that identifier already exists.
72             elsif($code eq "409"){
73 0           Activiti::Rest::Error::Conflict->throw($args);
74             }
75              
76             #The operation failed. The request body contains an unsupported media type. Also occurs when the request-body JSON contains an unknown attribute or value that doesn't have the right format/type to be accepted.
77             elsif($code eq "415"){
78 0           Activiti::Rest::Error::UnsupportedMediaType->throw($args);
79             }
80              
81             #The operation failed. An unexpected exception occurred while executing the operation. The response-body contains details about the error.
82             elsif($code eq "500"){
83 0           Activiti::Rest::Error::InternalServerError->throw($args);
84             }
85              
86             #common error
87             else{
88 0           Activiti::Rest::Error->throw($args);
89             }
90              
91             }
92              
93 0           my $content_type = $res->header('Content-Type');
94 0           my $content_length = $res->header('Content-Length');
95              
96 0           my(%new_args) = (
97             code => $res->code,
98             content_type => $content_type
99             );
100              
101 0 0 0       if($res->code ne "204" && defined($content_type)){
102              
103 0           $new_args{content} = $res->content;
104              
105 0 0         if($content_type =~ /json/o){
    0          
106 0           $new_args{parsed_content} = JSON::decode_json($res->content);
107             }elsif($content_type =~ /(xml|html)/o){
108 0           $new_args{parsed_content} = $res->decoded_content();
109             }
110              
111             }
112              
113 0           __PACKAGE__->new(%new_args);
114             }
115              
116             1;