Mojo::UserAgent transaction error message confusion

61 views
Skip to first unread message

Natxo Asenjo

unread,
Mar 7, 2017, 4:00:26 PM3/7/17
to Mojolicious
hi,

using Mojo::UserAgent

$ mojo version
CORE
  Perl        (v5.24.1, linux)
  Mojolicious (7.27, Doughnut)

OPTIONAL
  EV 4.0+                 (n/a)
  IO::Socket::Socks 0.64+ (n/a)
  IO::Socket::SSL 1.94+   (2.038)
  Net::DNS::Native 0.15+  (n/a)

I am writing an api client to phpipam (https://phpipam.net/api/api_documentation/). This module is greatly simplifying my work, by the way, thanks!

I have a question regarding the error messages from the transactions. I have this code:

    my $tx = $ua->get( "$prot://$url$api/user/all/" => { 'token' => $token } );

    if ( $tx->success ) {
        $allusers = $tx->res->json('/data');
        return $allusers;
    }
    else {
        my $err = $tx->error;
        die "Could not get info on all users:  $err->{code} response -> $err->{message}";
   }

Which works perfectly fine until my api user identified by the token does not have the right permissions to the api. Then I get the right error code on $err->{code} but not the right message in $err->{message}, it seems.

When I use export MOJO_USERAGENT_DEBUG=1 and run my script, I see that the server returns something else:

-- Client <<< Server (http://192.168.5.22/phpipam/api/kkk/user/all/)
HTTP/1.1 503 Service Unavailable\x0d
Date: Tue, 07 Mar 2017 20:55:16 GMT\x0d
Server: Apache/2.4.6 (CentOS) PHP/5.4.16\x0d
X-Powered-By: PHP/5.4.16\x0d
Expires: Thu, 19 Nov 1981 08:52:00 GMT\x0d
Cache-Control: no-cache\x0d
Pragma: no-cache\x0d
Content-Length: 73\x0d
Connection: close\x0d
Content-Type: application/json; charset=utf-8\x0d
\x0d
{"code":503,"success":0,"message":"Invalid app permissions","time":0.017}
Could not get info on all users:  503 response -> Service Unavailable at lib/phpipam/API.pm line 116

So I would expect in $err->{message} "Invalid app permissions" instead of "Service Unavailable".

Am I doing something wrong?

TIA.

--
regards,
Natxo

Dotan Dimet

unread,
Mar 7, 2017, 4:10:08 PM3/7/17
to mojol...@googlegroups.com
״Service Unavailable״ is the error message of the HTTP transaction, which appears in the HTTP headers.
״Invalid app permissions" is an error message specific to the application you're talking to, encoded in the body of the transaction.
Mojo::UserAgent gives you the HTTP error, but it knows nothing about the specifics of the phpipam API or whatever application you're talking to.
You need to extract the error details from the JSON in your own client code.
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Natxo Asenjo

unread,
Mar 7, 2017, 4:18:29 PM3/7/17
to Mojolicious

Replying to myself.

On Tuesday, March 7, 2017 at 10:00:26 PM UTC+1, Natxo Asenjo wrote:

It works if I use this

        die "Could not get info on all users:  $err->{code} response " . $tx->res->json('/message');

I guess this is what I need to use.

Natxo Asenjo

unread,
Mar 8, 2017, 5:26:48 AM3/8/17
to Mojolicious
hi Dotan,


On Tuesday, March 7, 2017 at 10:10:08 PM UTC+1, Dotan Dimet wrote:
״Service Unavailable״ is the error message of the HTTP transaction, which appears in the HTTP headers.
״Invalid app permissions" is an error message specific to the application you're talking to, encoded in the body of the transaction.
Mojo::UserAgent gives you the HTTP error, but it knows nothing about the specifics of the phpipam API or whatever application you're talking to.
You need to extract the error details from the JSON in your own client code.


Yes, thanks for your explanation. I came to that same conclusion on my own after you posted your answer, I had not read it yet.

Thanks again!

--
regards,
Natxo
Reply all
Reply to author
Forward
0 new messages