05.
Modeling the Error: ApiProblem Class
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
7 Comments
While I try to run the server this error shows: "Cannot autowire service "App\Controller\Api\ApiProblem": argument "$statusCode" of method "__construct()" has no type-hint, you should configure its value explicitly. "
Hey @Majkell!
Hmm. So I know what's happening here, but I don't exactly know the cause :). This is an error because Symfony is trying to "autowire" your ApiProblem class. This means that either:
(A) You've added an ApiProblem argument to a constructor of a service somewhere or
(B) You've added an ApiProblem argument to one of your controllers.
If you can find that code, that's your problem :). In both cases, having an argument there doesn't make sense: you don't want Symfony to instantiate an ApiProblem for you - it's a simple "model" object that you should instantiate yourself when you need it.
Let me know if that makes sense and if you can find the bad code!
Cheers!
I ve used as an argument in the JwtTokenAuthenticator
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$apiProblem = new ApiProblem(401);
$apiProblem->set('detail', $exception->getMessageKey());
return $this->responseFactory->createResponse($apiProblem);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
}
public function supportsRememberMe()
{
return false;
}
public function start(Request $request, AuthenticationException $authException = null)
{
$apiProblem = new ApiProblem(401);
$message = $authException ? $authException->getMessageKey() : 'Missing credentials';
$apiProblem->set('detail', $message);
return new JsonResponse($apiProblem->toArray(), 401);
}
fixed it. thanks
Um, I don't see this class in the resources directory. :( Only ApiTestCase and ResponseAsserter
Ah, you're right! I've just fixed this - try downloading the code again. Thanks for letting me know!
Loving it :)
"Houston: no signs of life"
Start the conversation!