Skip to content

Commit c84bb52

Browse files
authored
Merge pull request #666 from ivanhrytsaim/13057-Improve-error-displaying-in-the-Blog-Api
13057-Improve-error-displaying-in-the-Blog-Api
2 parents a323133 + 0241263 commit c84bb52

5 files changed

+25
-13
lines changed

Model/AbstractManagement.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function create($data)
100100
$item->setData($data)->save();
101101
return json_encode($item->getData());
102102
} catch (\Exception $e) {
103-
return false;
103+
return $this->getError($e->getMessage());
104104
}
105105
}
106106

@@ -118,7 +118,7 @@ public function update($id, $data)
118118
$item->load($id);
119119

120120
if (!$item->getId()) {
121-
return false;
121+
return $this->getError('Item not found');
122122
}
123123
$data = json_decode($data, true);
124124
foreach ($this->_imagesMap as $key) {
@@ -133,7 +133,7 @@ public function update($id, $data)
133133
$item->addData($data)->save();
134134
return json_encode($item->getData());
135135
} catch (\Exception $e) {
136-
return false;
136+
return $this->getError($e->getMessage());
137137
}
138138
}
139139

@@ -152,9 +152,9 @@ public function delete($id)
152152
$item->delete();
153153
return true;
154154
}
155-
return false;
155+
return $this->getError('Something went wrong');
156156
} catch (\Exception $e) {
157-
return false;
157+
return $this->getError($e->getMessage());
158158
}
159159
}
160160

@@ -171,11 +171,11 @@ public function get($id)
171171
$item->load($id);
172172

173173
if (!$item->getId()) {
174-
return false;
174+
return $this->getError('Item not found');
175175
}
176176
return json_encode($item->getData());
177177
} catch (\Exception $e) {
178-
return false;
178+
return $this->getError($e->getMessage());
179179
}
180180
}
181181

@@ -193,12 +193,12 @@ public function view($id, $storeId)
193193
$item->getResource()->load($item, $id);
194194

195195
if (!$item->isVisibleOnStore($storeId)) {
196-
return false;
196+
return $this->getError('Item is not visible on this store.');
197197
}
198198

199199
return json_encode($this->getDynamicData($item));
200200
} catch (\Exception $e) {
201-
return false;
201+
return $this->getError($e->getMessage());
202202
}
203203
}
204204

@@ -207,4 +207,16 @@ public function view($id, $storeId)
207207
* @return mixed
208208
*/
209209
abstract protected function getDynamicData($item);
210+
211+
/**
212+
* @param $massage
213+
* @return false|string
214+
*/
215+
public function getError($massage) {
216+
$data = ['error' => 'true'];
217+
218+
$data['message'] = $massage ?? '';
219+
220+
return json_encode($data);
221+
}
210222
}

Model/CategoryManagement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function getList($type, $term, $storeId, $page, $limit)
7171

7272
return json_encode($result);
7373
} catch (\Exception $e) {
74-
return false;
74+
return $this->getError($e->getMessage());
7575
}
7676
}
7777

Model/CommentManagement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getList($type, $term, $storeId, $page, $limit)
7373

7474
return json_encode($result);
7575
} catch (\Exception $e) {
76-
return false;
76+
return $this->getError($e->getMessage());
7777
}
7878
}
7979

Model/PostManagement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function getList($type, $term, $storeId, $page, $limit)
101101

102102
return json_encode($result);
103103
} catch (\Exception $e) {
104-
return false;
104+
return $this->getError($e->getMessage());
105105
}
106106
}
107107

Model/TagManagement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getList($type, $term, $storeId, $page, $limit)
7373

7474
return json_encode($result);
7575
} catch (\Exception $e) {
76-
return false;
76+
return $this->getError($e->getMessage());
7777
}
7878
}
7979

0 commit comments

Comments
 (0)