Commit 3f1f9273 authored by Dietmar Maurer's avatar Dietmar Maurer

split out code to convert exceptions to apache handler results

parent 17dd123b
...@@ -271,6 +271,24 @@ sub proxy_handler { ...@@ -271,6 +271,24 @@ sub proxy_handler {
return OK; return OK;
} }
my $exc_to_res = sub {
my ($err, $status) = @_;
$status = $status || HTTP_INTERNAL_SERVER_ERROR;
my $resp = {};
if (ref($err) eq "PVE::Exception") {
$resp->{status} = $err->{code} || $status;
$resp->{errors} = $err->{errors} if $err->{errors};
$resp->{message} = $err->{msg};
} else {
$resp->{status} = $status;
$resp->{message} = $err;
}
return $resp;
};
sub rest_handler { sub rest_handler {
my ($rpcenv, $clientip, $method, $abs_uri, $rel_uri, $ticket, $token) = @_; my ($rpcenv, $clientip, $method, $abs_uri, $rel_uri, $ticket, $token) = @_;
...@@ -318,10 +336,7 @@ sub rest_handler { ...@@ -318,10 +336,7 @@ sub rest_handler {
if !$isUpload && ($euid != 0) && ($method ne 'GET'); if !$isUpload && ($euid != 0) && ($method ne 'GET');
}; };
if (my $err = $@) { if (my $err = $@) {
return { return &$exc_to_res($err, HTTP_UNAUTHORIZED);
status => HTTP_UNAUTHORIZED,
message => "$err", # always convert exception to string
};
} }
} }
...@@ -363,10 +378,7 @@ sub rest_handler { ...@@ -363,10 +378,7 @@ sub rest_handler {
# check access permissions # check access permissions
eval { $rpcenv->check_api2_permissions($info->{permissions}, $username, $uri_param); }; eval { $rpcenv->check_api2_permissions($info->{permissions}, $username, $uri_param); };
if (my $err = $@) { if (my $err = $@) {
return { return &$exc_to_res($err, HTTP_FORBIDDEN);
status => HTTP_FORBIDDEN,
message => "$err", # always convert exception to string
};
} }
if ($info->{proxyto}) { if ($info->{proxyto}) {
...@@ -382,10 +394,7 @@ sub rest_handler { ...@@ -382,10 +394,7 @@ sub rest_handler {
} }
}; };
if (my $err = $@) { if (my $err = $@) {
return { return &$exc_to_res($err);
status => HTTP_INTERNAL_SERVER_ERROR,
message => "$err", # always convert exception to string
};
} }
if ($remip) { if ($remip) {
return { proxy => $remip, proxy_params => $params }; return { proxy => $remip, proxy_params => $params };
...@@ -415,16 +424,8 @@ sub rest_handler { ...@@ -415,16 +424,8 @@ sub rest_handler {
$resp->{changes} = $diff; $resp->{changes} = $diff;
} }
}; };
my $err = $@; if (my $err = $@) {
if ($err) { return &$exc_to_res($err);
if (ref($err) eq "PVE::Exception") {
$resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
$resp->{errors} = $err->{errors} if $err->{errors};
$resp->{message} = $err->{msg};
} else {
$resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
$resp->{message} = "$err";
}
} }
$rpcenv->set_user(undef); $rpcenv->set_user(undef);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment