MigrateController: Enhance error handling

This commit is contained in:
Johannes Meyer 2019-11-28 15:14:49 +01:00
parent 5d2ed45e19
commit e8e067ea34

View file

@ -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();
}
}