API Handle uncaught ValidationException on CMS controller execution

This removes the need for a lot of boilerplate code
around DataObject->write() logic, and avoids generic 500 errors
on user-level failures. This should really be a per-project choice,
but at the moment request handling doesn't allow to configure
custom exception handling.
This commit is contained in:
Ingo Schommer 2013-03-08 12:50:27 +01:00
parent 693a92f9c9
commit b81f39aee5

View File

@ -354,7 +354,18 @@ class LeftAndMain extends Controller implements PermissionProvider {
}
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) {
$response = parent::handleRequest($request, $model);
try {
$response = parent::handleRequest($request, $model);
} catch(ValidationException $e) {
// Nicer presentation of model-level validation errors
$msgs = _t('LeftAndMain.ValidationError', 'Validation error') . ': '
. $e->getResult()->message();
$e = new SS_HTTPResponse_Exception($msgs, 403);
$e->getResponse()->addHeader('Content-Type', 'text/plain');
$e->getResponse()->addHeader('X-Status', rawurlencode($msgs));
throw $e;
}
$title = $this->Title();
if(!$response->getHeader('X-Controller')) $response->addHeader('X-Controller', $this->class);
if(!$response->getHeader('X-Title')) $response->addHeader('X-Title', $title);