Permalink
Browse files

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.
  • Loading branch information...
1 parent 693a92f commit b81f39aee596c660efc9fbbf87d2247aee485d95 @chillu chillu committed Mar 8, 2013
Showing with 12 additions and 1 deletion.
  1. +12 −1 admin/code/LeftAndMain.php
View
@@ -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);

0 comments on commit b81f39a

Please sign in to comment.