Skip to content

Commit

Permalink
changed return value and arguments of error method in Mojo::Message
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 29, 2014
1 parent 79823db commit 862b766
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 107 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -6,6 +6,7 @@
- Changed lock and unlock callbacks in Mojo::IOLoop to not receive an
invocant.
- Changed return value of path_for method in Mojolicious::Routes::Match.
- Changed return value and arguments of error method in Mojo::Message.
- Removed deprecated support for "X-Forwarded-HTTPS".
- Removed return values from wait method in Mojo::IOLoop::Delay.
- Removed list context support from header method in Mojo::Headers.
Expand Down
19 changes: 8 additions & 11 deletions lib/Mojo/Message.pm
Expand Up @@ -77,13 +77,12 @@ sub error {

# Set
if (@_) {
$self->{error} = [@_];
$self->{error} = shift;
return $self->finish;
}

# Get
return unless my $err = $self->{error};
return wantarray ? @$err : $err->[0];
return $self->{error};
}

sub extract_start_line {
Expand Down Expand Up @@ -180,7 +179,7 @@ sub parse {
if $self->headers->is_limit_exceeded;

# Check buffer size
return $self->error('Maximum buffer size exceeded', 400)
return $self->error({msg => 'Maximum buffer size exceeded', advise => 400})
if $self->content->is_limit_exceeded;

return $self->emit('progress')->content->is_finished ? $self->finish : $self;
Expand Down Expand Up @@ -253,9 +252,9 @@ sub _cache {
}

sub _limit {
my $self = shift;
my ($self, $msg, $code) = @_;
$self->{limit} = 1;
$self->error(@_);
$self->error({msg => $msg, advise => $code});
}

sub _parse_formdata {
Expand Down Expand Up @@ -496,12 +495,10 @@ make sure it is not excessively large, there's a 10MB limit by default.
=head2 error
my $err = $msg->error;
my ($err, $code) = $msg->error;
$msg = $msg->error('Parser error');
$msg = $msg->error('Parser error', 500);
my $err = $msg->error;
$msg = $msg->error({msg => 'Parser error', advise => 500});
Error and code.
Message error.
=head2 extract_start_line
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Message/Request.pm
Expand Up @@ -61,7 +61,8 @@ sub extract_start_line {
return undef unless $$bufref =~ s/^\s*(.*?)\x0d?\x0a//;

# We have a (hopefully) full request line
$self->error('Bad request start line', 400) and return undef
$self->error({msg => 'Bad request start line', advise => 400})
and return undef
unless $1 =~ $START_LINE_RE;
my $url = $self->method($1)->version($3)->url;
return !!($1 eq 'CONNECT' ? $url->authority($2) : $url->parse($2));
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -95,7 +95,7 @@ sub extract_start_line {

# We have a full response line
return undef unless $$bufref =~ s/^(.*?)\x0d?\x0a//;
$self->error('Bad response start line') and return undef
$self->error({msg => 'Bad response start line'}) and return undef
unless $1 =~ m!^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$!;
$self->content->skip_body(1) if $self->code($2)->is_empty;
return !!$self->version($1)->message($3)->content->auto_relax(1);
Expand Down
28 changes: 11 additions & 17 deletions lib/Mojo/Transaction.pm
Expand Up @@ -12,16 +12,15 @@ has res => sub { Mojo::Message::Response->new };
sub client_close {
my ($self, $close) = @_;

# Remove code from parser errors
my $res = $self->res->finish;
if (my $err = $res->error) { $res->error($err) }

# Premature connection close
elsif ($close && !$res->code) { $res->error('Premature connection close') }
my $res = $self->res->finish;
if ($close && !$res->code && !$res->error) {
$res->error({msg => 'Premature connection close'});
}

# 400/500
elsif ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error($res->message, $res->code);
$res->error({msg => $res->message, code => $res->code});
}

return $self->server_close;
Expand All @@ -36,13 +35,7 @@ sub connection {
return $self->{connection};
}

sub error {
my $self = shift;
my $req = $self->req;
return $req->error if $req->error;
my $res = $self->res;
return $res->error ? $res->error : undef;
}
sub error { $_[0]->req->error || $_[0]->res->error }

sub is_finished { (shift->{state} // '') eq 'finished' }

Expand Down Expand Up @@ -219,8 +212,7 @@ Connection identifier or socket.
=head2 error
my $err = $tx->error;
my ($err, $code) = $tx->error;
my $err = $tx->error;
Error and code.
Expand Down Expand Up @@ -286,8 +278,10 @@ message in L</"error">, 400 and 500 responses also a code.
# Sensible exception handling
if (my $res = $tx->success) { say $res->body }
else {
my ($err, $code) = $tx->error;
say $code ? "$code response: $err" : "Connection error: $err";
my $err = $tx->error;
say $err->{code}
? "$err->{code} response: $err->{msg}"
: "Connection error: $err->{msg}";
}
=head1 SEE ALSO
Expand Down
15 changes: 10 additions & 5 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -130,7 +130,7 @@ sub _connect_proxy {

# CONNECT failed (connection needs to be kept alive)
unless ($tx->keep_alive && $tx->res->is_status_class(200)) {
$old->req->error('Proxy connection failed');
$old->req->error({msg => 'Proxy connection failed'});
return $self->$cb($old);
}

Expand Down Expand Up @@ -232,7 +232,10 @@ sub _enqueue {

sub _error {
my ($self, $id, $err, $timeout) = @_;
if (my $tx = $self->{connections}{$id}{tx}) { $tx->res->error($err) }

if (my $tx = $self->{connections}{$id}{tx}) {
$tx->res->error({msg => $err});
}
elsif (!$timeout) { return $self->emit(error => $err) }
$self->_handle($id, 1);
}
Expand Down Expand Up @@ -388,8 +391,10 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'});
if (my $res = $tx->success) { say $res->body }
else {
my ($err, $code) = $tx->error;
say $code ? "$code response: $err" : "Connection error: $err";
my $err = $tx->error;
say $err->{code}
? "$err->{code} response: $err->{msg}"
: "Connection error: $err->{msg}";
}
# Quick JSON API request with Basic authentication
Expand Down Expand Up @@ -675,7 +680,7 @@ L<Mojo::UserAgent::Transactor/"tx">.
$tx->res->on(progress => sub {
my $res = shift;
return unless my $server = $res->headers->server;
$res->error('Oh noes, it is IIS!') if $server =~ /IIS/;
$res->error({msg => 'Oh noes, it is IIS!'}) if $server =~ /IIS/;
});
$tx = $ua->start($tx);
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious.pm
Expand Up @@ -112,7 +112,8 @@ sub dispatch {
$plugins->emit_hook(before_routes => $c);
my $res = $tx->res;
return if $res->code;
if (my $code = ($tx->req->error)[1]) { $res->code($code) }
my $err = $tx->req->error;
if (my $code = $err->{advise}) { $res->code($code) }
elsif ($tx->is_websocket) { $res->code(426) }
$c->render_not_found unless $self->routes->dispatch($c) || $tx->res->code;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/cpanify.pm
Expand Up @@ -29,7 +29,7 @@ sub run {

unless ($tx->success) {
my $code = $tx->res->code // 0;
my $msg = $tx->error;
my $msg = $tx->error->{msg};
if ($code == 401) { $msg = 'Wrong username or password.' }
elsif ($code == 409) { $msg = 'File already exists on CPAN.' }
die qq{Problem uploading file "$file". ($msg)\n};
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -65,9 +65,10 @@ sub run {
$verbose = 1 if $method eq 'HEAD';
STDOUT->autoflush(1);
my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));
my ($err, $code) = $tx->error;
my $err = $tx->error;
$url = encode 'UTF-8', $url;
warn qq{Problem loading URL "$url". ($err)\n} if $err && !$code;
warn qq{Problem loading URL "$url". ($err->{msg})\n}
if $err && !$err->{code};

# JSON Pointer
return unless defined $selector;
Expand Down
5 changes: 3 additions & 2 deletions lib/Test/Mojo.pm
Expand Up @@ -336,8 +336,9 @@ sub _request_ok {

# Perform request
$self->tx($self->ua->start($tx));
my ($err, $code) = $self->tx->error;
Test::More::diag $err if !(my $ok = !$err || $code) && $err;
my $err = $self->tx->error;
Test::More::diag $err->{msg}
if !(my $ok = !$err->{msg} || $err->{code}) && $err;
my $desc = encode 'UTF-8', "@{[uc $tx->req->method]} $url";
return $self->_test('ok', $ok, $desc);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/ojo.pm
Expand Up @@ -41,10 +41,10 @@ sub import {
sub _request {
my $ua = shift;

my $tx = $ua->start($ua->build_tx(@_));
my ($err, $code) = $tx->error;
warn qq/Problem loading URL "@{[$tx->req->url->to_abs]}". ($err)\n/
if $err && !$code;
my $tx = $ua->start($ua->build_tx(@_));
my $err = $tx->error;
warn qq/Problem loading URL "@{[$tx->req->url->to_abs]}". ($err->{msg})\n/
if $err && !$err->{code};

return $tx->res;
}
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/daemon.t
Expand Up @@ -234,7 +234,7 @@ is scalar @{$daemon->acceptors}, 0, 'no active acceptors';
$tx = $ua->inactivity_timeout(0.5)
->get("http://127.0.0.1:$port/throttle2" => {Connection => 'close'});
ok !$tx->success, 'not successful';
is $tx->error, 'Inactivity timeout', 'right error';
is $tx->error->{msg}, 'Inactivity timeout', 'right error';
$daemon->start;
$tx = $ua->inactivity_timeout(10)
->get("http://127.0.0.1:$port/throttle3" => {Connection => 'close'});
Expand Down

0 comments on commit 862b766

Please sign in to comment.