Navigation Menu

Skip to content

Commit

Permalink
moved error handling from user agent to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 20, 2013
1 parent a7f13df commit 8107e06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
17 changes: 15 additions & 2 deletions lib/Mojo/Transaction.pm
Expand Up @@ -10,8 +10,20 @@ has req => sub { Mojo::Message::Request->new };
has res => sub { Mojo::Message::Response->new };

sub client_close {
my $self = shift;
$self->res->finish;
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') }

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

return $self->server_close;
}

Expand Down Expand Up @@ -179,6 +191,7 @@ implements the following new ones.
=head2 client_close
$tx->client_close;
$tx->client_close(1);
Transaction closed client-side, used to implement user agents.
Expand Down
27 changes: 4 additions & 23 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -204,7 +204,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');
return $self->_finish($old, $cb);
return $self->$cb($old);
}

# Prevent proxy reassignment and start real transaction
Expand Down Expand Up @@ -280,24 +280,6 @@ sub _error {
$self->_handle($id => $err);
}

sub _finish {
my ($self, $tx, $cb, $close) = @_;

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

# Premature connection close
elsif ($close && !$res->code) { $res->error('Premature connection close') }

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

$self->$cb($tx);
}

sub _handle {
my ($self, $id, $close) = @_;

Expand All @@ -318,7 +300,7 @@ sub _handle {
elsif ($old && (my $new = $self->_upgrade($id))) {
if (my $jar = $self->cookie_jar) { $jar->extract($old) }
$old->client_close;
$self->_finish($new, $c->{cb});
$c->{cb}->($self, $new);
$new->client_read($old->res->content->leftovers);
}

Expand All @@ -327,11 +309,10 @@ sub _handle {
$self->_remove($id, $close);
return unless $old;
if (my $jar = $self->cookie_jar) { $jar->extract($old) }
$old->client_close;
$old->client_close($close);

# Handle redirects
$self->_finish($new || $old, $c->{cb}, $close)
unless $self->_redirect($c, $old);
$c->{cb}->($self, $new || $old) unless $self->_redirect($c, $old);
}
}

Expand Down

0 comments on commit 8107e06

Please sign in to comment.