8000 Better error handling · Lucky-Loek/Symfony-API-Example@b006afc · GitHub
[go: up one dir, main page]

Skip 8000 to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit b006afc

Browse files
author
Loek van der Linde
committed
Better error handling
1 parent db05ae7 commit b006afc

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/AppBundle/Controller/TokenController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace AppBundle\Controller;
44

5-
use AppBundle\Exception\InvalidRequestArgumentException;
65
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
76
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
87
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
98
use Symfony\Component\HttpFoundation\JsonResponse;
109
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1111

1212
class TokenController extends Controller
1313
{
@@ -24,14 +24,14 @@ public function newTokenAction(Request $request)
2424
$user = $repository->findOneBy(['username' => $username]);
2525

2626
if (is_null($user)) {
27-
throw new InvalidRequestArgumentException('User not found', 401);
27+
throw new BadCredentialsException('User not found', 401);
2828
}
2929

3030
$encoder = $this->get('security.password_encoder');
3131
$passwordValid = $encoder->isPasswordValid($user, $password);
3232

3333
if (!$passwordValid) {
34-
throw new InvalidRequestArgumentException('Password invalid', 401);
34+
throw new BadCredentialsException('Password invalid', 401);
3535
}
3636

3737
$tokenEncoder = $this->get('lexik_jwt_authentication.encoder');

src/AppBundle/Security/JwtTokenAuthenticator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ public function __construct(JWTEncoderInterface $encoder, EntityManager $em)
5353
*/
5454
public function start(Request $request, AuthenticationException $authException = null)
5555
{
56-
return new JsonResponse([
57-
'error' => 'Auth required',
58-
], 401);
56+
$errorMessage = [
57+
'error' => [
58+
'code' => 401,
59+
'message' => $authException->getMessageKey()
60+
]
61+
];
62+
63+
return new JsonResponse($errorMessage, 401);
5964
}
6065

6166
/**

0 commit comments

Comments
 (0)
0