/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ public function afterException($controller, $methodName, \Exception $exception) { // If there's no proper status code associated, set it to 500. $response = new JSONResponse(); if ($exception->getCode() < 100) { $response->setStatus(500); } else { $response->setStatus($exception->getCode()); } $response->setErrorMessage($exception->getMessage()); //$this->api->log(get_class($controller) . '->' . $methodName . ': ' . $exception->getMessage()); return $response; }
/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ public function afterException($controller, $methodName, \Exception $exception) { \OCP\Util::writeLog('contacts', __METHOD__ . ' method: ' . $methodName, \OCP\Util::DEBUG); // If there's no proper status code associated, set it to 500. $response = new JSONResponse(); if ($exception->getCode() < 100) { $response->setStatus(HttpStatus::STATUS_INTERNAL_SERVER_ERROR); } else { $response->setStatus($exception->getCode()); } $response->setErrorMessage($exception->getMessage()); \OCP\Util::logException('contacts', $exception); return $response; }
/** * @NoAdminRequired */ public function deleteGroup() { $id = $this->request->post['id']; $name = $this->request->post['name']; $response = new JSONResponse(); if (empty($id)) { $response->bailOut(App::$l10n->t('No group ID given.')); return $response; } try { $ids = $this->tags->getIdsForTag($id); } catch (\Exception $e) { $response->setErrorMessage($e->getMessage()); \OCP\Util::writeLog('contacts', __METHOD__ . ', ' . $e->getMessage(), \OCP\Util::ERROR); return $response; } $tagId = $id; if ($ids !== false) { $backend = $this->app->getBackend('local'); foreach ($ids as $id) { $contact = $backend->getContact(null, $id, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read($contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); if ($obj) { if (!$obj->inGroup($name)) { continue; } if ($obj->removeFromGroup($name)) { $backend->updateContact(null, $id, $obj, array('noCollection' => true, 'isBatch' => true)); } } else { \OCP\Util::writeLog('contacts', __METHOD__ . ', could not parse card ' . $id, \OCP\Util::DEBUG); } } } try { $this->tags->delete($tagId); } catch (\Exception $e) { $response->setErrorMessage($e->getMessage()); \OCP\Util::writeLog('contacts', __METHOD__ . ', ' . $e->getMessage(), \OCP\Util::ERROR); } return $response; }
/** * @NoAdminRequired */ public function deleteGroup() { $name = $this->request->post['name']; $response = new JSONResponse(); if (is_null($name) || $name === '') { $response->bailOut(App::$l10n->t('No group name given.')); return $response; } $tagMgr = $this->server->getTagManager()->load('contact'); try { $ids = $tagMgr->getIdsForTag($name); } catch (\Exception $e) { $response->setErrorMessage($e->getMessage()); return $response; } if ($ids !== false) { $backend = $this->app->getBackend('local'); foreach ($ids as $id) { $contact = $backend->getContact(null, $id, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read($contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); if ($obj) { if (!isset($obj->CATEGORIES)) { continue; } if ($obj->CATEGORIES->removeGroup($name)) { $backend->updateContact(null, $id, $obj, array('noCollection' => true, 'isBatch' => true)); } } else { \OCP\Util::writeLog('contacts', __METHOD__ . ', could not parse card ' . $id, \OCP\Util::DEBUG); } } } try { $tagMgr->delete($name); } catch (\Exception $e) { $response->setErrorMessage($e->getMessage()); } return $response; }