Skip to content

Commit 41fbeef

Browse files
author
christophe-compere
committed
Rewrite exception treatment.
1 parent 3ef918b commit 41fbeef

File tree

2 files changed

+103
-31
lines changed

2 files changed

+103
-31
lines changed

Controller/ContentManagement/CrudController.php

Lines changed: 100 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ private function dataService() {
2323
return $this->get('ems.service.data');
2424
}
2525

26-
2726
/**
2827
* @Route("/api/{name}/create/{ouuid}", defaults={"ouuid": null, "_format": "json"})
2928
* @Route("/api/{name}/draft/{ouuid}", defaults={"ouuid": null, "_format": "json"})
@@ -47,8 +46,17 @@ public function createAction($ouuid, ContentType $contentType, Request $request)
4746

4847
} catch (\Exception $e) {
4948

50-
$this->addFlash('error', 'The revision ' . $newRevision . ' can not be created');
49+
if (($e instanceof NotFoundHttpException) OR ($e instanceof BadRequestHttpException)) {
50+
throw $e;
51+
} else {
52+
$this->addFlash('error', 'The revision for contenttype '. $contentType->getName() .' can not be created. Reason:'.$e->getMessage());
53+
}
5154
$isCreated = false;
55+
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
56+
'success' => $isCreated,
57+
'ouuid' => $ouuid,
58+
'type' => $contentType->getName(),
59+
]);
5260
}
5361

5462
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
@@ -58,7 +66,6 @@ public function createAction($ouuid, ContentType $contentType, Request $request)
5866
]);
5967
}
6068

61-
6269
/**
6370
* @Route("/api/{name}/{ouuid}", defaults={"ouuid": null, "_format": "json"})
6471
* @Route("/api/{name}/get/{ouuid}", defaults={"ouuid": null, "_format": "json"})
@@ -75,17 +82,26 @@ public function getAction($ouuid, ContentType $contentType) {
7582
} catch (\Exception $e) {
7683

7784
$isFound = false;
78-
throw $e;
85+
if (($e instanceof NotFoundHttpException) OR ($e instanceof BadRequestHttpException)) {
86+
throw $e;
87+
} else {
88+
$this->addFlash('error', 'The revision for contenttype '. $contentType->getName() .' can not be found. Reason: '.$e->getMessage());
89+
}
90+
return $this->render( 'EMSCoreBundle:ajax:revision.json.twig', [
91+
'success' => $isFound,
92+
'ouuid' => $ouuid,
93+
'type' => $contentType->getName(),
94+
]);
7995
}
8096

8197
return $this->render( 'EMSCoreBundle:ajax:revision.json.twig', [
8298
'success' => $isFound,
8399
'revision' => $revision->getRawData(),
84100
'ouuid' => $revision->getOuuid(),
101+
'id' => $revision->getId(),
85102
]);
86103
}
87104

88-
89105
/**
90106
* @Route("/api/{name}/finalize/{id}", defaults={"_format": "json"})
91107
* @ParamConverter("contentType", options={"mapping": {"name": "name", "deleted": 0, "active": 1}})
@@ -94,7 +110,7 @@ public function getAction($ouuid, ContentType $contentType) {
94110
public function finalizeAction($id, ContentType $contentType, Request $request) {
95111

96112
if(!$contentType->getEnvironment()->getManaged()){
97-
throw new BadRequestHttpException('You can not create content for a managed content type');
113+
throw new BadRequestHttpException('You can not finalize content for a managed content type');
98114
}
99115

100116
$out = [
@@ -109,12 +125,11 @@ public function finalizeAction($id, ContentType $contentType, Request $request)
109125
} catch (\Exception $e) {
110126

111127
if (($e instanceof NotFoundHttpException) OR ($e instanceof DataStateException)) {
112-
$this->addFlash('error', $e->getMessage());
128+
throw $e;
113129
} else {
114-
$this->addFlash('error', 'The revision ' . $id . ' can not be finalized: '.$e->getMessage());
130+
$this->addFlash('error', 'The revision ' . $id . ' for contenttype '. $contentType->getName() .' can not be finalized. Reason: '.$e->getMessage());
115131
}
116132
$out['success'] = false;
117-
118133
}
119134
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', $out);
120135
}
@@ -127,7 +142,7 @@ public function finalizeAction($id, ContentType $contentType, Request $request)
127142
public function discardAction($id, ContentType $contentType, Request $request) {
128143

129144
if(!$contentType->getEnvironment()->getManaged()){
130-
throw new BadRequestHttpException('You can not create content for a managed content type');
145+
throw new BadRequestHttpException('You can not discard content for a managed content type');
131146
}
132147

133148
try {
@@ -138,15 +153,57 @@ public function discardAction($id, ContentType $contentType, Request $request) {
138153
} catch (\Exception $e) {
139154
$isDiscard = false;
140155
if (($e instanceof NotFoundHttpException) OR ($e instanceof BadRequestHttpException)) {
141-
$this->addFlash('error', $e->getMessage());
156+
throw $e;
142157
} else {
143-
$this->addFlash('error', 'The revision ' . $id . ' can not be discarded. Reason: '.$e->getMessage());
158+
$this->addFlash('error', 'The revision ' . $id . ' for contenttype '. $contentType->getName() .' can not be discarded. Reason: '.$e->getMessage());
144159
}
145-
throw $e;
146-
160+
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
161+
'success' => $isDiscard,
162+
'type' => $contentType->getName(),
163+
'revision_id' => $id,
164+
]);
147165
}
148166
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
149167
'success' => $isDiscard,
168+
'type' => $contentType->getName(),
169+
'revision_id' => $revision->getId(),
170+
]);
171+
}
172+
173+
/**
174+
* @Route("/api/{name}/delete/{ouuid}", defaults={"_format": "json"})
175+
* @ParamConverter("contentType", options={"mapping": {"name": "name", "deleted": 0, "active": 1}})
176+
* @Method({"POST"})
177+
*/
178+
public function deleteAction($ouuid, ContentType $contentType, Request $request) {
179+
180+
$isDeleted = false;
181+
if(!$contentType->getEnvironment()->getManaged()){
182+
throw new BadRequestHttpException('You can not delete content for a managed content type');
183+
}
184+
185+
try {
186+
$this->dataService()->delete($contentType->getName(), $ouuid);
187+
try {
188+
$revision = $this->dataService()->getNewestRevision($contentType->getName(), $ouuid);
189+
} catch (\Exception $exception){
190+
if ($exception instanceof NotFoundHttpException) {
191+
$isDeleted = true;
192+
}
193+
}
194+
195+
} catch (\Exception $e) {
196+
$isDeleted = false;
197+
if (($e instanceof NotFoundHttpException) OR ($e instanceof BadRequestHttpException)) {
198+
throw $e;
199+
} else {
200+
$this->addFlash('error', 'The revision ' . $id . ' can not be deleted. Reason: '.$e->getMessage());
201+
}
202+
}
203+
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
204+
'success' => $isDeleted,
205+
'ouuid' => $ouuid,
206+
'type' => $contentType->getName(),
150207
]);
151208
}
152209

@@ -158,7 +215,7 @@ public function discardAction($id, ContentType $contentType, Request $request) {
158215
public function replaceAction($ouuid, ContentType $contentType, Request $request) {
159216

160217
if(!$contentType->getEnvironment()->getManaged()){
161-
throw new BadRequestHttpException('You can not create content for a managed content type');
218+
throw new BadRequestHttpException('You can not replace content for a managed content type');
162219
}
163220

164221
$rawdata = json_decode($request->getContent(), true);
@@ -169,24 +226,30 @@ public function replaceAction($ouuid, ContentType $contentType, Request $request
169226
try {
170227
$revision = $this->dataService()->getNewestRevision($contentType->getName(), $ouuid);
171228
$newDraft = $this->dataService()->replaceData($revision, $rawdata);
172-
$isReplace = ($revision->getId() != $newDraft->getId()) ? true : false;
229+
$isReplaced = ($revision->getId() != $newDraft->getId()) ? true : false;
173230

174231
} catch (\Exception $e) {
232+
$isReplaced = false;
175233
if ($e instanceof NotFoundHttpException) {
176-
$this->addFlash('error', $e->getMessage());
234+
throw $e;
177235
} else {
178-
$this->addFlash('error', 'The revision ' . $ouuid . ' can not be replaced');
236+
$this->addFlash('error', 'The revision ' . $ouuid . ' for contenttype '. $contentType->getName() .' can not be replaced. Reason: '.$e->getMessage());
179237
}
180-
$isReplace = false;
181-
238+
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
239+
'success' => $isReplaced,
240+
'ouuid' => $ouuid,
241+
'type' => $contentType->getName(),
242+
'revision_id' => null,
243+
]);
182244
}
183245
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
184-
'success' => $isReplace,
246+
'success' => $isReplaced,
247+
'ouuid' => $ouuid,
248+
'type' => $contentType->getName(),
185249
'revision_id' => $newDraft->getId(),
186250
]);
187251
}
188252

189-
190253
/**
191254
* @Route("/api/{name}/merge/{ouuid}", defaults={"_format": "json"})
192255
* @ParamConverter("contentType", options={"mapping": {"name": "name", "deleted": 0, "active": 1}})
@@ -195,35 +258,41 @@ public function replaceAction($ouuid, ContentType $contentType, Request $request
195258
public function mergeAction($ouuid, ContentType $contentType, Request $request) {
196259

197260
if(!$contentType->getEnvironment()->getManaged()){
198-
throw new BadRequestHttpException('You can not create content for a managed content type');
261+
throw new BadRequestHttpException('You can not merge content for a managed content type');
199262
}
200263

201264
$rawdata = json_decode($request->getContent(), true);
202265
if (empty($rawdata)){
203-
throw new BadRequestHttpException('Not a valid JSON message');
266+
throw new BadRequestHttpException('Not a valid JSON message for revision ' . $ouuid . ' and contenttype '. $contentType->getName());
204267
}
205268

206269
try {
207270
$revision = $this->dataService()->getNewestRevision($contentType->getName(), $ouuid);
208271
$newDraft = $this->dataService()->replaceData($revision, $rawdata, "merge");
209-
$isMerge = ($revision->getId() != $newDraft->getId()) ? true : false;
272+
$isMerged = ($revision->getId() != $newDraft->getId()) ? true : false;
210273

211274
} catch (\Exception $e) {
212275
if ($e instanceof NotFoundHttpException) {
213-
$this->addFlash('error', $e->getMessage());
276+
throw $e;
214277
} else {
215-
$this->addFlash('error', 'The revision ' . $ouuid . ' can not be replaced');
278+
$this->addFlash('error', 'The revision ' . $ouuid . ' for contenttype '. $contentType->getName() .' can not be merged. Reason: '.$e->getMessage());
216279
}
217-
$isMerge = false;
218-
280+
$isMerged = false;
281+
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
282+
'success' => $isMerged,
283+
'ouuid' => $ouuid,
284+
'type' => $contentType->getName(),
285+
'revision_id' => null,
286+
]);
219287
}
220288
return $this->render( 'EMSCoreBundle:ajax:notification.json.twig', [
221-
'success' => $isMerge,
289+
'success' => $isMerged,
290+
'ouuid' => $ouuid,
291+
'type' => $contentType->getName(),
222292
'revision_id' => $newDraft->getId(),
223293
]);
224294
}
225295

226-
227296
/**
228297
* @Route("/api/test", defaults={"_format": "json"}, name="api.test")
229298
* @Method({"GET"})

Resources/views/ajax/notification.json.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
{% endif %}
2121
{% if uuid is defined %}
2222
"uuid": {{ uuid|json_encode|raw }},
23+
{% endif %}
24+
{% if type is defined %}
25+
"type": {{ type|json_encode|raw }},
2326
{% endif %}
2427
{% block body %}{% endblock %}
2528
"success": {{ success|json_encode|raw }}

0 commit comments

Comments
 (0)