From 85da187f85c9c289f36a9553947305a053c2b8f2 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 8 Mar 2013 12:35:27 +0100 Subject: [PATCH] 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. --- code/LeftAndMain.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/code/LeftAndMain.php b/code/LeftAndMain.php index 96a2b394..f0c6050a 100644 --- a/code/LeftAndMain.php +++ b/code/LeftAndMain.php @@ -314,6 +314,15 @@ class LeftAndMain extends Controller { SSViewer::set_theme(null); } + public function handleRequest(SS_HTTPRequest $request) { + try { + return parent::handleRequest($request); + } catch(ValidationException $e) { + // Nicer presentation of model-level validation errors + return $this->httpError(403, $e->getResult()->message()); + } + } + /** * If this is set to true, the "switchView" context in the