mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 04:23:53 +08:00
Move some API error handling code around. It still sucks though
This commit is contained in:
parent
a5b2a3da03
commit
b5169512cb
@ -3,8 +3,12 @@
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Flarum\Api\JsonApiResponse;
|
||||
use Flarum\Core\Exceptions\ValidationFailureException;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Tobscure\JsonApi\SerializerInterface;
|
||||
use Tobscure\JsonApi\Criteria;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
abstract class SerializeAction implements ActionInterface
|
||||
{
|
||||
@ -68,7 +72,20 @@ abstract class SerializeAction implements ActionInterface
|
||||
{
|
||||
$request = static::buildJsonApiRequest($request);
|
||||
|
||||
$data = $this->data($request, $response = new JsonApiResponse);
|
||||
try {
|
||||
$data = $this->data($request, $response = new JsonApiResponse);
|
||||
} catch (ValidationFailureException $e) {
|
||||
$errors = [];
|
||||
foreach ($e->getErrors()->getMessages() as $field => $messages) {
|
||||
$errors[] = [
|
||||
'detail' => implode("\n", $messages),
|
||||
'path' => $field
|
||||
];
|
||||
}
|
||||
return new JsonResponse(['errors' => $errors], 422);
|
||||
} catch (PermissionDeniedException $e) {
|
||||
return new JsonResponse(null, 401);
|
||||
}
|
||||
|
||||
$serializer = new static::$serializer($request->actor, $request->include, $request->link);
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Core\Commands\GenerateAccessTokenCommand;
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
|
||||
@ -32,9 +33,8 @@ class TokenAction implements ActionInterface
|
||||
$user = $this->users->findByIdentification($identification);
|
||||
|
||||
if (! $user || ! $user->checkPassword($password)) {
|
||||
return;
|
||||
// throw an exception
|
||||
// return $this->respondWithError('invalidCredentials', 401);
|
||||
// throw new PermissionDeniedException;
|
||||
return new JsonResponse(null, 401);
|
||||
}
|
||||
|
||||
$token = $this->bus->dispatch(
|
||||
|
@ -5,8 +5,6 @@ use Illuminate\Foundation\Exceptions\Handler;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Flarum\Core\Exceptions\ValidationFailureException;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Config;
|
||||
|
||||
@ -31,13 +29,6 @@ class ExceptionHandler extends Handler
|
||||
public function render($request, Exception $e)
|
||||
{
|
||||
if ($request->is('api/*')) {
|
||||
if ($e instanceof ValidationFailureException) {
|
||||
return $this->renderValidationException($e);
|
||||
}
|
||||
if ($e instanceof PermissionDeniedException) {
|
||||
return new Response(null, 401);
|
||||
}
|
||||
|
||||
$error = [];
|
||||
if (Config::get('app.debug')) {
|
||||
$error['code'] = (new \ReflectionClass($e))->getShortName();
|
||||
@ -60,16 +51,4 @@ class ExceptionHandler extends Handler
|
||||
{
|
||||
return new JsonResponse(['errors' => $errors], $httpCode);
|
||||
}
|
||||
|
||||
protected function renderValidationException(ValidationFailureException $e)
|
||||
{
|
||||
$errors = [];
|
||||
foreach ($e->getErrors()->getMessages() as $field => $messages) {
|
||||
$errors[] = [
|
||||
'detail' => implode("\n", $messages),
|
||||
'path' => $field
|
||||
];
|
||||
}
|
||||
return $this->renderErrors($errors, 422);
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,7 @@ class LoginAction extends BaseAction
|
||||
$response = app('Flarum\Api\Actions\TokenAction')
|
||||
->handle(new ApiRequest($request->only('identification', 'password')));
|
||||
|
||||
$data = $response->getData();
|
||||
if (! empty($data->token)) {
|
||||
if (($data = $response->getData()) && ! empty($data->token)) {
|
||||
$response->withCookie($this->makeRememberCookie($data->token));
|
||||
|
||||
event(new UserLoggedIn($this->users->findOrFail($data->userId), $data->token));
|
||||
|
Loading…
x
Reference in New Issue
Block a user