Skip to content

Commit

Permalink
API Handle uncaught ValidationException on CMS controller execution
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chillu committed Mar 8, 2013
1 parent 693a92f commit b81f39a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion admin/code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,18 @@ public function init() {
}

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);
Expand Down

0 comments on commit b81f39a

Please sign in to comment.