Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aab7b43

Browse files
validation Error exception handling
1 parent e58110a commit aab7b43

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

‎app/ApiCode.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
class ApiCode {
66
public const SOMETHING_WENT_WRONG = 250;
77
public const INVALID_CREDENTIALS = 251;
8+
public const VALIDATION_ERROR = 252;
89
}

‎app/Exceptions/Handler.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\Exceptions;
44

5+
use App\ApiCode;
56
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7+
use Illuminate\Validation\ValidationException;
8+
use MarcinOrlowski\ResponseBuilder\ResponseBuilder;
69
use Throwable;
710

811
class Handler extends ExceptionHandler
@@ -49,7 +52,18 @@ public function report(Throwable $exception)
4952
* @throws \Throwable
5053
*/
5154
public function render($request, Throwable $exception)
52-
{;
55+
{
56+
if ($exception instanceof ValidationException) {
57+
return $this->respondWithValidationError($exception);
58+
}
59+
5360
return parent::render($request, $exception);
5461
}
62+
63+
private function respondWithValidationError($exception) {
64+
return ResponseBuilder::asError(ApiCode::VALIDATION_ERROR)
65+
->withData($exception->errors())
66+
->withHttpCode(422)
67+
->build();
68+
}
5569
}

‎app/Http/Controllers/AuthController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct()
1313
}
1414

1515
public function login() {
16-
$credentials = request(['email', 'password']);
16+
$credentials = request()->validate(['email' => 'required|email', 'password' => 'required|string|max:25']);
1717

1818
if (! $token = auth()->attempt($credentials)) {
1919
return $this->respondUnAuthorizedRequest(ApiCode::INVALID_CREDENTIALS);

‎config/response_builder.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
'map' => [
2222
ApiCode::SOMETHING_WENT_WRONG => 'api.something_went_wrong',
2323
ApiCode::INVALID_CREDENTIALS => 'api.invalid_credentials',
24+
ApiCode::VALIDATION_ERROR => 'api.validation_error',
2425
],
2526
];

‎resources/lang/en/api.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
'something_went_wrong' => 'Ops! Something went wrong.',
17-
'invalid_credentials' => 'Invalid email or password.'
17+
'invalid_credentials' => 'Invalid email or password.',
18+
'validation_error' => 'Validation Error'
1819

1920
];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /