mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
693a92f9c9
commit
b81f39aee5
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user