/** * Tests PFXUtils::nestedArrayKeyExists(). */ public function testNestedArrayKeyExists() { $structure = array('foo' => array('foo' => 'bar', 'bar' => array(0, 1, 3), 'baz' => array('abc' => 'def', 'ghi' => 'jkl')), 'bar' => array('foo' => null, 'baz' => false, 'borg' => array('foo' => array('foo' => 'bar')))); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('foo', 'foo'), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('foo', 'bar', 0), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('foo', 'baz', 'ghi'), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('bar', 'foo'), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('bar', 'baz'), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('bar', 'borg'), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('bar', 'borg', 'foo'), $structure)); $this->assertTrue(PFXUtils::nestedArrayKeyExists(array('bar', 'borg', 'foo', 'foo'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('baz'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('foo', 'borg'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('foo', 'foo', 'bar'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('foo', 'bar', 3), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('foo', 'baz', 'jkl'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('bar', 'bar'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('bar', 'baz', 'borf'), $structure)); $this->assertFalse(PFXUtils::nestedArrayKeyExists(array('bar', 'borg', 'foo', 'baz'), $structure)); }
/** * Reacts to an error condition reported by the API. */ protected function _handleError() { if ($this->_responseCode < 400 && !\PFXUtils::nestedArrayKeyExists(array('error', 'errors'), $this->_responseParsed)) { return; } $this->_activeQuery = null; $reason = null; $message = null; $responseError = null; $responseID = uniqid(); if (!\PFXUtils::nestedArrayKeyExists(array('error', 'errors'), $this->_responseParsed) || !$this->_responseParsed['error']['errors']) { $responseError = 'Could not find error detail in API response ' . '(response code was ' . $this->_responseCode . ', response ID is ' . $responseID . ').'; } elseif (array_key_exists('reason', $this->_responseParsed['error']['errors'][0])) { $reason = $this->_responseParsed['error']['errors'][0]['reason']; } else { $responseError = 'Could not find reason code in API response ' . '(response code was ' . $this->_responseCode . ', response ID is ' . $responseID . ').'; } if (\PFXUtils::nestedArrayKeyExists(array('error', 'message'), $this->_responseParsed)) { $message = $this->_responseParsed['error']['message']; } else { $message = 'Could not find error message in API response ' . '(response code was ' . $this->_responseCode . ').'; } switch ($this->_responseCode) { case 400: if ($reason == 'invalidParameter') { throw new InvalidParameterException($message); } if ($reason == 'badRequest') { throw new BadRequestException($message); } break; case 401: throw new InvalidCredentialsException($message); break; case 403: if ($reason == 'insufficientPermissions') { throw new InsufficientPermissionsException($message); } if ($reason == 'dailyLimitExceeded') { throw new DailyLimitExceededException($message); } if ($reason == 'userRateLimitExceeded') { throw new UserRateLimitExceededException($message); } if ($reason == 'quotaExceeded') { throw new QuotaExceededException($message); } break; case 503: throw new RemoteException($message); break; } if ($responseError) { /* The API response didn't contain the error messaging it should have, so log it. */ $this->_logger->log('Raw content of response ID ' . $responseID . ': ' . $this->_responseRaw, false); if ($message === null) { $message = $responseError; } } throw new RuntimeException($message); }
protected function _handleError() { if (!$this->_responseCode < 400 && !isset($this->_responseParsed['error'])) { return; } $responseID = null; if (\PFXUtils::nestedArrayKeyExists(array('error', 'message'), $this->_responseParsed)) { $message = $this->_responseParsed['error']['message']; // Why is this buried so deep? if (\PFXUtils::nestedArrayKeyExists(array('error', 'details', 0, 'errorDetails', 0, 'message'), $this->_responseParsed)) { $message .= ' (' . $this->_responseParsed['error']['details'][0]['errorDetails'][0]['message'] . ')'; } } else { $responseID = uniqid(); $message = 'Could not find error message in API response. ' . 'Response code was ' . $this->_responseCode . '; response ID is ' . $responseID . '.'; $this->_logger->log('Raw content of response ID ' . $responseID . ': ' . $this->_responseRaw, false); } switch ($this->_responseCode) { case 400: throw new BadRequestException($message); case 404: throw new NotFoundException($message); default: throw new RemoteException($message); } }