Paws the VII (B4 and After)

Well I left off in my last post with having 2 of the 3 tests I created failed;

ok 10210 - Got _status eq 200 from result ok 10211 - Call S3->RestoreObject from t/10_responses/s3-restore-object.3.response not ok 10212 - Got request_id eq 195AF4042CD6DF80 from result

# Failed test 'Got request_id eq 195AF4042CD6DF80 from result'
# at t/10_responses.t line 136.
# got: 'AC65B3BF8AC3AA34'
# expected: '195AF4042CD6DF80'
not ok 10213 - Got message eq Object restore is already in progress from result

# Failed test 'Got message eq Object restore is already in progress from result'
# at t/10_responses.t line 136.
# got: 'Conflict'
# expected: 'Object restore is already in progress'
ok 10214 - Got code eq 409 from result

10213 was easy to fix I just had to change the test value in the test file 's3-restore-object.3.response.test.yml'


–  expected: 195AF4042CD6DF80
++  expected: AC65B3BF8AC3AA34

and now for 10213.

Seems I am still getting that 'Conflict' value and not my new 'Object restore is already in progress' message. So the first thing I am going to do if to see if my changes to boto made a change in the code.

I did a quick search and I could not find any generated code (in the auto-lib dir) that had any of the new structures or classes I create in the s3 service-2.json file. So back to boto I go.

I reviewed the 'service-2.json' file and found this;


 "errors":[
        {"shape":"ObjectAlreadyInActiveTierError"}
      ],

it seems somewhere along the line I must of took out my error so I will add that back in.


 "errors":[
        {"shape":"ObjectAlreadyInActiveTierError"},
        {"shape":"ObjectRestoreAlreadyInProgress"}
      ],

and on another recompile I still do not see any changes in the generated code and even more searching about I never found any of the 'errors' classing being generated. I am going to leave this and check out what is happening in the non-generated code.

In 'lib/Paws/Net/RestXMLResponse.pm' I found this sub


 sub process {
    my ($self, $call_object, $response) = @_;
    if ( $response->status >= 300 ) {
        return $self->error_to_exception($call_object, $response);
    } else {
        return $self->response_to_object($call_object, $response);
    }
  }

so my error is taking a path to 'error_to_exception' which is this block


sub error_to_exception {
    my ($self, $call_object, $response) = @_;
    my $struct = eval { $self->unserialize_response( $response->content ) };
    if ($@){
      return Paws::Exception->new(
        message => $@,
        code => 'InvalidContent',
        request_id => '', #$request_id,
        http_status => $response->status,
      );
    }
…

Ok Paws raises a new “Paws::Exception” exception. Looking about in other parts of the code I do not see any of the other 'errors' stucts being used in Paws. I think I can then drop those changes and work with that Paws::Exception class in that sub.

I added in a little debugging and had a look at that '$struct' from the above sub;


$VAR1 = {
          'Code' => 'RestoreAlreadyInProgress',
          'RequestId' => 'AC65B3BF8AC3AA34',
          'Message' => 'Object restore is already in progress',
          'HostId' => '1WU8BgcaFHXTlyNBhilk4Xv2sbOJs80FjvAvW9weQkwtKJXcnJg='
        };

So given the above I can make these changes


 return Paws::Exception->new(
 --      message => $@,
++ message => exists($struct->{Message})?$struct->{Message}:$@,
--            code => exists($struct->{Code})?$struct->{Code}:'InvalidContent',
++             code =>'InvalidContent',
        request_id => '', #$request_id,
        http_status => $response->status,
      );

and added in message and code from the $struct and now I will just have to change the test again and it now look like this


---
call: RestoreObject
service: S3
tests:
  - path: request_id
    op: eq
    expected: AC65B3BF8AC3AA34
  - path: message
    op: eq
    expected: Object restore is already in progress
  - path: code
    expected: RestoreAlreadyInProgress
  - path: http_status
    op: eq
    expected: 409

and on this run

ok 10211 - Call S3->RestoreObject from t/10_responses/s3-restore-object.3.response ok 10212 - Got request_id eq AC65B3BF8AC3AA34 from result not ok 10213 - Got message eq Object restore is already in progress from result

# Failed test 'Got message eq Object restore is already in progress from result'
# at t/10_responses.t line 136.
# got: 'Conflict'
# expected: 'Object restore is already in progress'
Use of uninitialized value in concatenation (.) or string at t/10_responses.t line 136.
Use of uninitialized value $type in hash element at /wwwveh/lib/Test/Builder.pm line 884.
Use of uninitialized value $type in concatenation (.) or string at /wwwveh/lib/Test/Builder.pm line 898.
Scalar found where operator expected at (eval in cmp_ok) t/10_responses.t line 136, near "$got $expect"
(Missing operator before $expect?)
not ok 10214 - Got code RestoreAlreadyInProgress from result

# Failed test 'Got code RestoreAlreadyInProgress from result'
# at t/10_responses.t line 136.
Use of uninitialized value $type in hash element at /wwwveh/lib/Test/Builder.pm line 911.
Use of uninitialized value $type in concatenation (.) or string at /wwwveh/lib/Test/Builder.pm line 915.
# An error occurred while using :
# ------------------------------------
# syntax error at (eval in cmp_ok) t/10_responses.t line 136, near "$got $expect"
#
# ------------------------------------
Use of uninitialized value $type in pattern match (m//) at /wwwveh/lib/Test/Builder.pm line 925.
Use of uninitialized value $type in pattern match (m//) at /wwwveh/lib/Test/Builder.pm line 928.
Use of uninitialized value $type in concatenation (.) or string at /wwwveh/lib/Test/Builder.pm line 958.
# '409'
#
# 'RestoreAlreadyInProgress'

so not 100% yet.

It seems I went off the hook here and added in my code changes in the 'error_to_exception' sub much too early that fist exception was only to catch a problem when serializing the content on response so roll that back and look at this block of code that followed the first one;


...
  }
 my ($message, $code, $request_id, $host_id);

$message = status_message($response->status);
$code = $response->status;

if (exists $struct->{RequestId}) {
$request_id = $struct->{RequestId};
} elsif (exists $struct->{RequestID}){
$request_id = $struct->{RequestID};
} elsif ($response->has_header('x-amzn-requestid')) {
$request_id = $response->header('x-amzn-requestid');
} else {
$request_id = '';
}
$host_id = $response->header('x-amz-id-2');

Paws::Exception->new(
message => $message,
code => $code,
request_id => $request_id,
host_id => $host_id,
http_status => $response->status,
);
}

Hmm I think this will work


-- $message = status_message($response->status);
++ $message = exists($struct->{Message})?$struct->{Message}:status_message($response->status);
-- $code = $response->status;
++ $code = exists($struct->{Code}) ? $struct->{Code} : $response->status;


I did check what that 'status_message' code does and it is a standard sub from 'HTTP::Status' that translates the status code number to its code name

Next I will add in one more test to 's3-restore-object.3.response.test.yml'


  - path: host_id
    op: eq
    expected: 1Wlw6Edlx3uplC85VIcAwe12xfU8BgcaFHXTlyNBhilk4Xv2sbOJs80FjvAvW9weQkwtKJXcnJg=

and on the test run I got


ok 10211 - Call S3->RestoreObject from t/10_responses/s3-restore-object.3.response
ok 10212 - Got request_id eq AC65B3BF8AC3AA34 from result
ok 10213 - Got message eq Object restore is already in progress from result
ok 10214 - Got code eq RestoreAlreadyInProgress from result
ok 10215 - Got http_status eq 409 from result
not ok 10216 - Exception accessing host_id:: Can't locate object method "host_id" via package "Paws::Exception" at t/lib/Paws/Crawler.pm line 19.

# Failed test 'Exception accessing host_id: Can't locate object method "host_id" via package "Paws::Exception" at t/lib/Paws/Crawler.pm line 19.'
# at t/10_responses.t line 127.
Use of uninitialized value $got in string eq at (eval in cmp_ok) t/10_responses.t line 136.
not ok 10217 - Got host_id: eq 1Wlw6Edlx3uplC85VIcAwe12xfU8BgcaFHXTlyNBhilk4Xv2sbOJs80FjvAvW9weQkwtKJXcnJg= from result

# Failed test 'Got host_id: eq 1Wlw6Edlx3uplC85VIcAwe12xfU8BgcaFHXTlyNBhilk4Xv2sbOJs80FjvAvW9weQkwtKJXcnJg= from result'
# at t/10_responses.t line 136.
# got: undef
# expected: '1Wlw6Edlx3uplC85VIcAwe12xfU8BgcaFHXTlyNBhilk4Xv2sbOJs80FjvAvW9weQkwtKJXcnJg='
ok 10218 - Call S3->UploadPart from t/10_responses/s3-upload-part.response

Seems I am missing 'host_id' in the Paws::Exception class so I added that in;


  has http_status => (
    is => 'ro',
    isa => 'Int',
  );

++ has host_id => (
++ is => 'ro',
++ isa => 'Str',
++ );

and when I run my tests I get


ok 10205 - Call S3->RestoreObject from t/10_responses/s3-restore-object.1.response
ok 10206 - Got _request_id eq 9C4494F17CFA569D from result
ok 10207 - Got _status eq 202 from result
ok 10208 - Call S3->RestoreObject from t/10_responses/s3-restore-object.2.response
ok 10209 - Got _request_id eq 9C4494F17CFA569D from result
ok 10210 - Got _status eq 200 from result
ok 10211 - Call S3->RestoreObject from t/10_responses/s3-restore-object.3.response
ok 10212 - Got request_id eq AC65B3BF8AC3AA34 from result
ok 10213 - Got message eq Object restore is already in progress from result
ok 10214 - Got code eq RestoreAlreadyInProgress from result
ok 10215 - Got http_status eq 409 from result

but now I am getting errors elsewhere

ok 10014 - Got request_id eq C535DE4BB867ECF3 from result
not ok 10015 - Got message eq Not Found from result

# Failed test 'Got message eq Not Found from result'
# at t/10_responses.t line 136.
# got: 'The specified key does not exist.'
# expected: 'Not Found'
not ok 10016 - Got code eq 404 from result

# Failed test 'Got code eq 404 from result'
# at t/10_responses.t line 136.
# got: 'NoSuchKey'
# expected: '404'
ok 10017 - Call S3->CreateBucket from t/10_responses/s3-create-bucket.1.response
ok 10018 - Got request_id eq 195AF4042CD6DF80 from result
not ok 10019 - Got message eq Bad Request from result

# Failed test 'Got message eq Bad Request from result'
# at t/10_responses.t line 136.
# got: 'The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.'
# expected: 'Bad Request'
not ok 10020 - Got code eq 400 from result

# Failed test 'Got code eq 400 from result'
# at t/10_responses.t line 136.
# got: 'IllegalLocationConstraintException'
# expected: '400'

Well something to look at on my next post

6fd152b433f2136dda6cb1104ad19e2c.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations