mirror of
https://github.com/Icinga/icingadb-web.git
synced 2026-05-28 04:36:06 -04:00
MigrateController: Enhance error handling
This commit is contained in:
parent
5d2ed45e19
commit
e8e067ea34
1 changed files with 29 additions and 5 deletions
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Icinga\Module\Icingadb\Controllers;
|
||||
|
||||
use Exception;
|
||||
use Icinga\Exception\IcingaException;
|
||||
use Icinga\Module\Icingadb\Compat\UrlMigrator;
|
||||
use Icinga\Module\Icingadb\Web\Controller;
|
||||
use ipl\Web\Url;
|
||||
|
|
@ -15,22 +17,44 @@ class MigrateController extends Controller
|
|||
$this->httpBadRequest('No API request');
|
||||
}
|
||||
|
||||
// TODO: Also verify content-type!
|
||||
if (
|
||||
! preg_match('/([^;]*);?/', $this->getRequest()->getHeader('Content-Type'), $matches)
|
||||
|| $matches[1] !== 'application/json'
|
||||
) {
|
||||
$this->httpBadRequest('No JSON content');
|
||||
}
|
||||
|
||||
$urls = $this->getRequest()->getPost();
|
||||
|
||||
$result = [];
|
||||
$errors = [];
|
||||
foreach ($urls as $urlString) {
|
||||
$url = Url::fromPath($urlString);
|
||||
if (UrlMigrator::isSupportedUrl($url)) {
|
||||
$urlString = UrlMigrator::transformUrl($url)->getAbsoluteUrl();
|
||||
try {
|
||||
$urlString = UrlMigrator::transformUrl($url)->getAbsoluteUrl();
|
||||
} catch (Exception $e) {
|
||||
$errors[$urlString] = [
|
||||
IcingaException::describe($e),
|
||||
IcingaException::getConfidentialTraceAsString($e)
|
||||
];
|
||||
$urlString = false;
|
||||
}
|
||||
}
|
||||
|
||||
$result[] = $urlString;
|
||||
}
|
||||
|
||||
$this->getResponse()->json()
|
||||
->setSuccessData($result)
|
||||
->sendResponse();
|
||||
$response = $this->getResponse()->json();
|
||||
if (empty($errors)) {
|
||||
$response->setSuccessData($result);
|
||||
} else {
|
||||
$response->setFailData([
|
||||
'result' => $result,
|
||||
'errors' => $errors
|
||||
]);
|
||||
}
|
||||
|
||||
$response->sendResponse();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue