public function setError(Exception $error) { if ($error instanceof RestApi_TypeUtils_ParseException) { $error = RestApi_Make::error(400, "Invalid field " . $error->getFieldName() . ": " . $error->getMessage()); } else { if (!$error instanceof RestApi_ProcessingException) { $error = RestApi_Make::error(500, "Internal server error: " . $error->getMessage()); } } $this->setResult($error->asResult()); }
public function check($name, $required, $defaultValue, $type, array $allowedValues = null) { $this->defaultValues[$name] = $defaultValue; $value = $this->get($name); if ($allowedValues !== null && !in_array($value, $allowedValues)) { throw RestApi_Make::error(400, sprintf('Only following values are allowed for %s: %s', $name, implode(',', $allowedValues))); } if ($value == '') { if ($required) { throw RestApi_Make::error(400, sprintf('Param %s is required', $name)); } else { // not required, no need to type check return; } } try { RestApi_TypeUtils_Field::of($type, $this->get($name)); } catch (RestApi_TypeUtils_ParseException $e) { $e->setFieldName($name); throw $e; } }
/** * * @param string|array $scopes If array is given, method succeeds if user has at least one of the specified privileges * @throws RestApi_ProcessingException */ public function checkScope($scopes) { if (!is_array($scopes)) { $scopes = array($scopes); } if (empty($scopes)) { return; } foreach ($scopes as $scope) { if ($this->hasPrivilegePrivate($scope)) { return; } } throw RestApi_Make::error(403, 'You do not have sufficient privileges'); }