-
Throughout Craft's controllers, anywhere that returns JSON with errors, the response is a 200. It really should be 4xx or 5xx, so it can be properly handled. A good solution might be to add this to I'm thinking something like this might be ideal:
Or perhaps something else to unify all JSON responses (success, error, errors, message, etc: #2495) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Agree this would be better. However it would be a breaking change for anyone creating these requests using jQuery Ajax methods, as the |
Beta Was this translation helpful? Give feedback.
-
Thanks @brandonkelly. In the meantime I wonder if I can duck-punch |
Beta Was this translation helpful? Give feedback.
-
You could hack it in by creating a module with this: use yii\base\Event;
use yii\web\Response;
Event::on(Response::class, Response::EVENT_BEFORE_SEND, function(Event $e) {
/** @var Response $response */
$response = $e->sender;
if (
$response->format === Response::FORMAT_JSON &&
!empty($response->data['error']) &&
$response->getStatusCode() === 200 &&
\Craft::$app->getRequest()->getIsSiteRequest()
) {
$response->setStatusCode(400);
}
}); |
Beta Was this translation helpful? Give feedback.
-
Hey @timkelty, it's you from the future! |
Beta Was this translation helpful? Give feedback.
-
I just stumbled upon this and I actually disagree with this change. If you look at the docs it clearly says:
Doing something like I feel this implementation, as a breaking change, is not a step forward. When I get a 400 I expect my client code to be wrong, my posted data to be wrong (e.g. bad CSRF), but not that the server did not complete my request. |
Beta Was this translation helpful? Give feedback.
Hey @timkelty, it's you from the future!
We did it, buddy!
#10605