Commit 4d41a11e authored by Alexandre Morin's avatar Alexandre Morin

Merge branch 'feat/11382_CCFN_API_ErrorCodes' into 'develop'

Feat/11382 ccfn api error codes

See merge request !326
parents 5594bd2c e4d8a959
Pipeline #4505 passed with stage
in 7 minutes and 58 seconds
......@@ -90,7 +90,7 @@ class ServiceKernel extends AbstractKernel
} catch (\Exception $exception) {
$handled = self::$instance->handleException($exception);
if (!$handled) {
self::$instance->response->setBody((string) $exception);
......@@ -316,6 +316,8 @@ class ServiceKernel extends AbstractKernel
if ($this->response->mode == 'http') {
$this->response->setHeader("X-Laabs-Exception", $exceptionClass . "; " . str_replace("\n", " ", $exception->getMessage()));
$this->response->setCode($exception->getCode());
}
// Try to find serializer output for the raised exception else send exception as string as response content
if (isset($this->outputRouter)) {
......
......@@ -48,6 +48,10 @@ class digitalSafe
$this->digitalResourceController = \laabs::newController('digitalResource/digitalResource');
$accountToken = \laabs::getToken('AUTH');
if (!$accountToken) {
throw $this->getThrowable("Missing authentication credential", 401);
}
$this->account = $this->accountController->get($accountToken->accountId);
}
......@@ -91,20 +95,13 @@ class digitalSafe
try {
$this->checkHash($resource->handler, $resource->hash, $resource->hashAlgorithm);
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
} elseif (!isset($resource->hash) && !isset($resource->hashAlgorithm)) {
continue;
} else {
$this->logEvent();
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Hash or hash algorithm missing";
return $replyMessage;
throw $this->getThrowable("Hash or hash algorithm missing", 401, $replyMessage);
}
}
......@@ -112,10 +109,7 @@ class digitalSafe
$archiveId = $this->archiveController->receive($archive, false);
$archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
$replyMessage->archiveId = $archiveId;
......@@ -156,10 +150,7 @@ class digitalSafe
$replyMessage->accountName = $this->account->accountName;
if (!$this->checkRight($originatorOwnerOrgRegNumber, $originatorOrgRegNumber, $archiveId)) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Permission denied";
return $replyMessage;
throw $this->getThrowable("Permission denied", 401, $replyMessage);
}
try {
......@@ -170,16 +161,11 @@ class digitalSafe
$res = $this->archiveController->destruct($archiveId);
}
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
if (count($res['error']) == 1) {
$replyMessage->operationResult = false;
return $replyMessage;
throw $this->getThrowable("The request could not be processed", 409, $replyMessage);
}
$replyMessage->originatorOwnerOrgRegNumber = $archive->originatorOwnerOrgRegNumber;
......@@ -210,10 +196,7 @@ class digitalSafe
$replyMessage->accountName = $this->account->accountName;
if (!$this->checkRight($originatorOwnerOrgRegNumber, $originatorOrgRegNumber, $archiveId)) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Permission denied";
return $replyMessage;
throw $this->getThrowable("Permission denied", 401, $replyMessage);
}
try {
......@@ -227,10 +210,7 @@ class digitalSafe
}
$archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
$replyMessage->originatorOwnerOrgRegNumber = $archive->originatorOwnerOrgRegNumber;
......@@ -262,19 +242,13 @@ class digitalSafe
$replyMessage->accountName = $this->account->accountName;
if (!$this->checkRight($originatorOwnerOrgRegNumber, $originatorOrgRegNumber, $archiveId)) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Permission denied";
return $replyMessage;
throw $this->getThrowable("Permission denied", 401, $replyMessage);
}
try {
$archive = $this->archiveController->retrieve($archiveId);
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable("archive " . $archiveId . " doesn't exist", 404, $replyMessage);
}
$replyMessage->originatorOwnerOrgRegNumber = $archive->originatorOwnerOrgRegNumber;
......@@ -313,20 +287,14 @@ class digitalSafe
$replyMessage->accountName = $this->account->accountName;
if (!$this->checkRight($originatorOwnerOrgRegNumber, $originatorOrgRegNumber, $archiveId)) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Permission denied";
return $replyMessage;
throw $this->getThrowable("Permission denied", 401, $replyMessage);
}
try {
$archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
$res = $this->archiveController->verifyIntegrity($archiveId);
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
$replyMessage->archiveId = $archive->archiveId;
......@@ -338,7 +306,6 @@ class digitalSafe
if (count($res['error']) > 1) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "No integrity";
return $replyMessage;
}
$replyMessage->operationResult = true;
......@@ -421,10 +388,7 @@ class digitalSafe
$replyMessage->lifeCycleEvents[] = $event;
}
} catch (\Exception $e) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
if ($originatorOrgRegNumber) {
......@@ -474,15 +438,11 @@ class digitalSafe
try {
$organization = $this->organizationController->getOrgByRegNumber($originatorOwnerOrgRegNumber);
} catch (\Exception $exception) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Organization " . $originatorOwnerOrgRegNumber . " doesn't exist";
return $replyMessage;
throw $this->getThrowable("Organization " . $originatorOwnerOrgRegNumber . " doesn't exist", 404, $replyMessage);
}
if ($organization->isOrgUnit) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "The organization must not be a organization unit";
return $replyMessage;
throw $this->getThrowable("The organization must not be a organization unit", 403, $replyMessage);
}
$userPositions = $this->userPositionController->listPositions($this->account->accountId);
......@@ -503,9 +463,7 @@ class digitalSafe
}
if (empty($userOrganisations)) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "The user is not positioned on the organization.";
return $replyMessage;
throw $this->getThrowable("The user is not positioned on the organization.", 403, $replyMessage);
}
$queryParts['organization'] = "(";
......@@ -523,9 +481,7 @@ class digitalSafe
$fromDate = $fromDate ? \laabs::newDatetime($fromDate, "UTC") : false;
$toDate = $toDate ? \laabs::newDatetime($toDate, "UTC") : false;
} catch (\Exception $exception) {
$replyMessage->operationResult = false;
$replyMessage->operationMessage = "Invalid format date";
return $replyMessage;
throw $this->getThrowable("Invalid format date", 400, $replyMessage);
}
if ($fromDate && $toDate) {
......@@ -632,10 +588,7 @@ class digitalSafe
$archive = $this->sdoFactory->read('recordsManagement/archive', $archiveId);
} catch (\Exception $e) {
$replyMessage = new \stdClass();
$replyMessage->operationResult = false;
$replyMessage->operationMessage = $e->getMessage();
return $replyMessage;
throw $this->getThrowable("archive " . $archiveId . " doesn't exist", 404, $replyMessage);
}
if ($archive->originatorOwnerOrgRegNumber == $originatorOwnerOrgRegNumber
......@@ -681,4 +634,24 @@ class digitalSafe
true
);
}
/**
* Prepare a throwable
* @param string The message
* @param int The code
* @param mixed The contextual data
*
* @return \Exception
*/
protected function getThrowable($message, $code, $context = [])
{
$exception = \laabs::newException('digitalSafe/Exception', $message, $code);
foreach ($context as $name => $value) {
$exception->{$name} = $value;
}
$exception->operationResult = false;
return $exception;
}
}
<?php
/*
* Copyright (C) 2019 Maarch
*
* This file is part of bundle digitalSafe.
*
* Bundle digitalSafe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle digitalSafe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle digitalSafe. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\digitalSafe\Exception;
class Exception extends \core\Exception
{
}
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