I’m attempting to throw a 404 with an error message if a resource object does not exist. However, I can’t set a custom JSON error message. I’ve tried with non 2xx statuses and was unsuccessful in setting the body.
public function getById($req, $res, $args)
{
$question = $this->questionRepo->getById($args['questionId']);
if ($question) {
return $res->withJson($question->toArray(), 200);
}
return $res->withJson(['error' => 'Question not found'], 404);
}
If I change the 404 to 200 status then the error will be passed to the body. When I set it back to 404, the body is empty.