Skip to content

Commit 6449cac

Browse files
committed
Stop throwing 404's when another exception occurred
1 parent 11d769d commit 6449cac

2 files changed

Lines changed: 4 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixed
66
- Fixed an “Header may not contain more than a single header” error that could occur if a different exception had occurred. ([#115](https://github.com/craftcms/element-api/issues/115))
7+
- Fixed a bug where most exceptions were resulting in 404 responses rather than 500s.
78

89
## 2.8.0 - 2021-08-31
910

src/controllers/DefaultController.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use yii\base\UserException;
2828
use yii\web\HttpException;
2929
use yii\web\JsonResponseFormatter;
30-
use yii\web\NotFoundHttpException;
3130
use yii\web\Response;
3231

3332
/**
@@ -54,7 +53,6 @@ class DefaultController extends Controller
5453
* @param string $pattern The endpoint URL pattern that was matched
5554
* @return Response
5655
* @throws InvalidConfigException
57-
* @throws NotFoundHttpException
5856
*/
5957
public function actionIndex(string $pattern): Response
6058
{
@@ -78,13 +76,7 @@ public function actionIndex(string $pattern): Response
7876
if (is_callable($config)) {
7977
/** @phpstan-ignore-next-line */
8078
$params = Craft::$app->getUrlManager()->getRouteParams();
81-
try {
82-
$config = $this->_callWithParams($config, $params);
83-
} catch (InvalidConfigException $e) {
84-
Craft::warning("Unable to resolve Element API route: {$e->getMessage()}", __METHOD__);
85-
Craft::$app->getErrorHandler()->logException($e);
86-
throw new NotFoundHttpException('Page not found', 0, $e);
87-
}
79+
$config = $this->_callWithParams($config, $params);
8880
}
8981

9082
if (is_array($config)) {
@@ -100,7 +92,7 @@ public function actionIndex(string $pattern): Response
10092
ArrayHelper::remove($config, 'cache', true) &&
10193
!($this->request->getIsPreview() || $this->request->getIsLivePreview())
10294
);
103-
95+
10496
$cacheKey = ArrayHelper::remove($config, 'cacheKey')
10597
?? implode(':', [
10698
'elementapi',
@@ -145,11 +137,7 @@ public function actionIndex(string $pattern): Response
145137
Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad = true;
146138

147139
// Get the data resource
148-
try {
149-
$resource = $plugin->createResource($config);
150-
} catch (\Throwable $e) {
151-
throw new NotFoundHttpException($e->getMessage() ?: Craft::t('element-api', 'Resource not found'), 0, $e);
152-
}
140+
$resource = $plugin->createResource($config);
153141

154142
// Load Fractal
155143
$fractal = new Manager();

0 commit comments

Comments
 (0)