mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 22:53:37 +08:00
Better API error handling
This commit is contained in:
parent
3441002a5b
commit
00be36ad16
|
@ -93,9 +93,10 @@ export default class ComposerReply extends ComposerBody {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}, (response) => {
|
}, errors => {
|
||||||
this.loading(false);
|
this.loading(false);
|
||||||
m.redraw();
|
m.redraw();
|
||||||
|
app.handleApiErrors(errors);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Component from 'flarum/component';
|
import Component from 'flarum/component';
|
||||||
import LoadingIndicator from 'flarum/components/loading-indicator';
|
import LoadingIndicator from 'flarum/components/loading-indicator';
|
||||||
import SignupModal from 'flarum/components/signup-modal';
|
import SignupModal from 'flarum/components/signup-modal';
|
||||||
|
import Alert from 'flarum/components/alert';
|
||||||
import icon from 'flarum/helpers/icon';
|
import icon from 'flarum/helpers/icon';
|
||||||
|
|
||||||
export default class LoginModal extends Component {
|
export default class LoginModal extends Component {
|
||||||
|
@ -15,7 +16,7 @@ export default class LoginModal extends Component {
|
||||||
view() {
|
view() {
|
||||||
return m('div.modal-dialog.modal-sm.modal-login', [
|
return m('div.modal-dialog.modal-sm.modal-login', [
|
||||||
m('div.modal-content', [
|
m('div.modal-content', [
|
||||||
m('button.btn.btn-icon.btn-link.close.back-control', {onclick: app.modal.close.bind(app.modal)}, icon('times')),
|
m('button.btn.btn-icon.btn-link.close.back-control', {onclick: this.hide.bind(this)}, icon('times')),
|
||||||
m('form', {onsubmit: this.login.bind(this)}, [
|
m('form', {onsubmit: this.login.bind(this)}, [
|
||||||
m('div.modal-header', m('h3.title-control', 'Log In')),
|
m('div.modal-header', m('h3.title-control', 'Log In')),
|
||||||
this.props.message ? m('div.modal-alert.alert', this.props.message) : '',
|
this.props.message ? m('div.modal-alert.alert', this.props.message) : '',
|
||||||
|
@ -49,15 +50,22 @@ export default class LoginModal extends Component {
|
||||||
$modal.find('[name=email]').focus();
|
$modal.find('[name=email]').focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hide() {
|
||||||
|
app.modal.close();
|
||||||
|
app.alerts.dismiss(this.errorAlert);
|
||||||
|
}
|
||||||
|
|
||||||
login(e) {
|
login(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.loading(true);
|
this.loading(true);
|
||||||
app.session.login(this.email(), this.password()).then(() => {
|
app.session.login(this.email(), this.password()).then(() => {
|
||||||
app.modal.close();
|
this.hide();
|
||||||
this.props.callback && this.props.callback();
|
this.props.callback && this.props.callback();
|
||||||
}, (response) => {
|
}, response => {
|
||||||
this.loading(false);
|
this.loading(false);
|
||||||
m.redraw();
|
m.redraw();
|
||||||
|
app.alerts.dismiss(this.errorAlert);
|
||||||
|
app.alerts.show(this.errorAlert = new Alert({ type: 'warning', message: 'Invalid credentials.' }))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ export default class Alert extends Component {
|
||||||
var message = attrs.message;
|
var message = attrs.message;
|
||||||
delete attrs.message;
|
delete attrs.message;
|
||||||
|
|
||||||
var controlItems = attrs.controls.slice() || [];
|
var controlItems = attrs.controls ? attrs.controls.slice() : [];
|
||||||
delete attrs.controls;
|
delete attrs.controls;
|
||||||
|
|
||||||
if (attrs.dismissible || attrs.dismissible === undefined) {
|
if (attrs.dismissible || attrs.dismissible === undefined) {
|
||||||
|
|
|
@ -23,9 +23,9 @@ export default class Alerts extends Component {
|
||||||
var index = this.components.indexOf(component);
|
var index = this.components.indexOf(component);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.components.splice(index, 1);
|
this.components.splice(index, 1);
|
||||||
}
|
|
||||||
m.redraw();
|
m.redraw();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
this.components = [];
|
this.components = [];
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import ItemList from 'flarum/utils/item-list';
|
import ItemList from 'flarum/utils/item-list';
|
||||||
|
import Alert from 'flarum/components/alert';
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -14,6 +15,14 @@ class App {
|
||||||
document.title = (title ? title+' - ' : '')+this.config['forum_title'];
|
document.title = (title ? title+' - ' : '')+this.config['forum_title'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleApiErrors(response) {
|
||||||
|
this.alerts.clear();
|
||||||
|
|
||||||
|
response.errors.forEach(error =>
|
||||||
|
this.alerts.show(new Alert({ type: 'warning', message: error.detail }))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
route(name, params) {
|
route(name, params) {
|
||||||
var url = this.routes[name][0].replace(/:([^\/]+)/g, function(m, t) {
|
var url = this.routes[name][0].replace(/:([^\/]+)/g, function(m, t) {
|
||||||
var value = params[t];
|
var value = params[t];
|
||||||
|
|
|
@ -12,7 +12,7 @@ abstract class DeleteAction implements ActionInterface
|
||||||
* @param \Flarum\Api\Request $request
|
* @param \Flarum\Api\Request $request
|
||||||
* @return \Flarum\Api\Response
|
* @return \Flarum\Api\Response
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request)
|
public function respond(Request $request)
|
||||||
{
|
{
|
||||||
$this->delete($request, $response = new Response('', 204));
|
$this->delete($request, $response = new Response('', 204));
|
||||||
|
|
||||||
|
|
43
framework/core/src/Api/Actions/JsonApiAction.php
Normal file
43
framework/core/src/Api/Actions/JsonApiAction.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php namespace Flarum\Api\Actions;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Flarum\Api\Request;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Flarum\Core\Exceptions\ValidationFailureException;
|
||||||
|
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||||
|
|
||||||
|
abstract class JsonApiAction implements ActionInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an API request and return an API response, handling any relevant
|
||||||
|
* (API-related) exceptions that are thrown.
|
||||||
|
*
|
||||||
|
* @param \Flarum\Api\Request $request
|
||||||
|
* @return \Flarum\Api\Response
|
||||||
|
*/
|
||||||
|
public function handle(Request $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return $this->respond($request);
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle an API request and return an API response.
|
||||||
|
*
|
||||||
|
* @param \Flarum\Api\Request $request
|
||||||
|
* @return \Flarum\Api\Response
|
||||||
|
*/
|
||||||
|
abstract protected function respond(Request $request);
|
||||||
|
}
|
|
@ -3,14 +3,11 @@
|
||||||
use Flarum\Api\Request;
|
use Flarum\Api\Request;
|
||||||
use Flarum\Api\JsonApiRequest;
|
use Flarum\Api\JsonApiRequest;
|
||||||
use Flarum\Api\JsonApiResponse;
|
use Flarum\Api\JsonApiResponse;
|
||||||
use Flarum\Core\Exceptions\ValidationFailureException;
|
|
||||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
|
||||||
use Tobscure\JsonApi\SerializerInterface;
|
use Tobscure\JsonApi\SerializerInterface;
|
||||||
use Tobscure\JsonApi\Criteria;
|
use Tobscure\JsonApi\Criteria;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Http\JsonResponse;
|
|
||||||
|
|
||||||
abstract class SerializeAction implements ActionInterface
|
abstract class SerializeAction extends JsonApiAction
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The name of the serializer class to output results with.
|
* The name of the serializer class to output results with.
|
||||||
|
@ -68,24 +65,11 @@ abstract class SerializeAction implements ActionInterface
|
||||||
* @param \Flarum\Api\Request $request
|
* @param \Flarum\Api\Request $request
|
||||||
* @return \Flarum\Api\Response
|
* @return \Flarum\Api\Response
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request)
|
public function respond(Request $request)
|
||||||
{
|
{
|
||||||
$request = static::buildJsonApiRequest($request);
|
$request = static::buildJsonApiRequest($request);
|
||||||
|
|
||||||
try {
|
|
||||||
$data = $this->data($request, $response = new JsonApiResponse);
|
$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);
|
$serializer = new static::$serializer($request->actor, $request->include, $request->link);
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Contracts\Bus\Dispatcher;
|
use Illuminate\Contracts\Bus\Dispatcher;
|
||||||
|
|
||||||
class TokenAction implements ActionInterface
|
class TokenAction extends JsonApiAction
|
||||||
{
|
{
|
||||||
protected $users;
|
protected $users;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ class TokenAction implements ActionInterface
|
||||||
* @param \Flarum\Api\Request $request
|
* @param \Flarum\Api\Request $request
|
||||||
* @return \Flarum\Api\Response
|
* @return \Flarum\Api\Response
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request)
|
public function respond(Request $request)
|
||||||
{
|
{
|
||||||
$identification = $request->get('identification');
|
$identification = $request->get('identification');
|
||||||
$password = $request->get('password');
|
$password = $request->get('password');
|
||||||
|
@ -33,8 +33,7 @@ class TokenAction implements ActionInterface
|
||||||
$user = $this->users->findByIdentification($identification);
|
$user = $this->users->findByIdentification($identification);
|
||||||
|
|
||||||
if (! $user || ! $user->checkPassword($password)) {
|
if (! $user || ! $user->checkPassword($password)) {
|
||||||
// throw new PermissionDeniedException;
|
throw new PermissionDeniedException;
|
||||||
return new JsonResponse(null, 401);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = $this->bus->dispatch(
|
$token = $this->bus->dispatch(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user