Skip to content

Commit 40e412b

Browse files
author
Denis Cornehl
committed
begin of handling new error-format
1 parent bff1703 commit 40e412b

1 file changed

Lines changed: 51 additions & 21 deletions

File tree

phpcclib.php

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
set_include_path('.:/Users/denis/pear/share/pear');
4+
25
/**
36
* require Pear::HTTP_Request2, Pear::Net_URL2 packages
47
*/
@@ -522,7 +525,7 @@ public function deployment_addAddon($applicationName, $deploymentName, $addonNam
522525
$data = array('addon' => $addonName);
523526
return $this->_executePost($resource, $data);
524527
}
525-
528+
526529

527530
/**
528531
* get sso login data
@@ -976,7 +979,7 @@ public function billingAccount_getVoucherList($userName, $billingName)
976979
public function addon_getList() {
977980
return $this->_executeGet('/addon/', $requiresToken=false);
978981
}
979-
982+
980983
/**
981984
* return list of all support plans
982985
*
@@ -993,12 +996,12 @@ public function addon_getList() {
993996
public function support_getList() {
994997
return $this->_executeGet('/support/', $requiresToken=false);
995998
}
996-
999+
9971000
/**
9981001
* return a single support plan
9991002
*
10001003
* @param string $planName name of the support plan
1001-
*
1004+
*
10021005
* @throws BadRequestError
10031006
* @throws ForbiddenError
10041007
* @throws GoneError
@@ -1181,6 +1184,44 @@ private function _jsonDecode($content) {
11811184
*/
11821185

11831186
class CCException extends Exception {
1187+
public function __construct($message, $status)
1188+
{
1189+
$this->message = $message;
1190+
1191+
1192+
$msgs = array();
1193+
$obj = json_decode($message);
1194+
if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) {
1195+
$rc = "";
1196+
$errors = array();
1197+
foreach ($obj as $k => $v) {
1198+
if ($k == "rc")
1199+
$rc = $v;
1200+
else if ($k == "error")
1201+
$errors = $v;
1202+
}
1203+
1204+
if (count($errors) > 0)
1205+
$msgs = $errors;
1206+
else if (strlen($rc) > 0)
1207+
$this->message = $rc;
1208+
else
1209+
$msgs = $obj;
1210+
}
1211+
1212+
if (count($msgs) > 0) {
1213+
$this->message = '';
1214+
1215+
1216+
if (is_array($msgs) || $msgs instanceof Traversable || $msgs instanceof stdClass) {
1217+
foreach ($msgs as $k => $v) {
1218+
$this->message .= sprintf("%s: %s\n", $k, $v);
1219+
}
1220+
}else
1221+
$this->message = $msgs;
1222+
1223+
}
1224+
}
11841225
}
11851226

11861227
/*
@@ -1210,24 +1251,12 @@ public function __toString()
12101251
*/
12111252

12121253
class BadRequestError extends CCException {
1213-
private $_msgs = array();
1214-
1215-
public function __construct($message)
1254+
public function __construct($message, $status)
12161255
{
1217-
$this->message = 'BadRequest';
1218-
/*
1219-
* You will get a string like this
1220-
* Bad Request {"lastname": "This field is required.", "firstname": "This field is required."}
1221-
* therefore we cut the first 12 chars from errorMessage
1222-
*/
1223-
$obj = json_decode(substr($message, 12));
1224-
1225-
if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) {
1226-
$this->message = '';
1227-
foreach ($obj as $k => $v) {
1228-
$this->message .= sprintf("%s: %s\n", $k, $v);
1229-
}
1230-
}
1256+
if (substr($message, 0, 11) == "Bad Request")
1257+
parent::__construct(substr($message, 12), $status);
1258+
else
1259+
parent::__construct($message, $status);
12311260
}
12321261
}
12331262

@@ -1499,6 +1528,7 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar
14991528
#
15001529
//$headers['Content-Length'] = strlen($body);
15011530
$headers['Accept-Encoding'] = 'compress, gzip';
1531+
$headers['Accept'] = 'application/json';
15021532
#
15031533
# Finally we fire the actual request.
15041534
#

0 commit comments

Comments
 (0)