mirror of
https://github.com/flarum/framework.git
synced 2025-04-27 07:04:03 +08:00
Improve error handling somewhat
- Fix composer crashing/not showing alert on error - Make a general ValidationException which takes an array of field ⇒ messages to be outputted nicely by the API
This commit is contained in:
parent
a14be00041
commit
5b3484d3c8
@ -108,7 +108,11 @@ export default class DiscussionComposer extends ComposerBody {
|
|||||||
app.cache.discussionList.addDiscussion(discussion);
|
app.cache.discussionList.addDiscussion(discussion);
|
||||||
m.route(app.route.discussion(discussion));
|
m.route(app.route.discussion(discussion));
|
||||||
},
|
},
|
||||||
() => this.loading = false
|
response => {
|
||||||
|
this.loading = false;
|
||||||
|
m.redraw();
|
||||||
|
app.alertErrors(response.errors);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,10 +92,10 @@ export default class ReplyComposer extends ComposerBody {
|
|||||||
|
|
||||||
app.composer.hide();
|
app.composer.hide();
|
||||||
},
|
},
|
||||||
errors => {
|
response => {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
m.redraw();
|
m.redraw();
|
||||||
app.alertErrors(errors);
|
app.alertErrors(response.errors);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -215,8 +215,8 @@ export default class App {
|
|||||||
alertErrors(errors) {
|
alertErrors(errors) {
|
||||||
errors.forEach(error => {
|
errors.forEach(error => {
|
||||||
this.alerts.show(new Alert({
|
this.alerts.show(new Alert({
|
||||||
type: 'warning',
|
type: 'error',
|
||||||
message: error.detail
|
children: error.detail
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ abstract class JsonApiAction implements Action
|
|||||||
*/
|
*/
|
||||||
public function handle(Request $request)
|
public function handle(Request $request)
|
||||||
{
|
{
|
||||||
// TODO: Move this error handling code to middleware?
|
// TODO: This is gross. Move this error handling code to middleware?
|
||||||
try {
|
try {
|
||||||
return $this->respond($request);
|
return $this->respond($request);
|
||||||
} catch (ValidationException $e) {
|
} catch (ValidationException $e) {
|
||||||
@ -30,6 +30,12 @@ abstract class JsonApiAction implements Action
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
return new JsonResponse(['errors' => $errors], 422);
|
return new JsonResponse(['errors' => $errors], 422);
|
||||||
|
} catch (\Flarum\Core\Exceptions\ValidationException $e) {
|
||||||
|
$errors = [];
|
||||||
|
foreach ($e->getMessages() as $path => $detail) {
|
||||||
|
$errors[] = compact('path', 'detail');
|
||||||
|
}
|
||||||
|
return new JsonResponse(['errors' => $errors], 422);
|
||||||
} catch (PermissionDeniedException $e) {
|
} catch (PermissionDeniedException $e) {
|
||||||
return new JsonResponse(null, 401);
|
return new JsonResponse(null, 401);
|
||||||
} catch (ModelNotFoundException $e) {
|
} catch (ModelNotFoundException $e) {
|
||||||
|
18
src/Core/Exceptions/ValidationException.php
Normal file
18
src/Core/Exceptions/ValidationException.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php namespace Flarum\Core\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class ValidationException extends Exception
|
||||||
|
{
|
||||||
|
protected $messages;
|
||||||
|
|
||||||
|
public function __construct(array $messages)
|
||||||
|
{
|
||||||
|
$this->messages = $messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMessages()
|
||||||
|
{
|
||||||
|
return $this->messages;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user