瀏覽代碼

Use same json error format as Mastodon

As documented in the Mastodon API ( https://docs.joinmastodon.org/entities/error/ ),   error responses use "error" as the key for the value, instead of Laravel's default (which is "message")
Wv5twkFEKh54vo4tta9yu7dHa3 4 年之前
父節點
當前提交
a19d4d5418
共有 1 個文件被更改,包括 5 次插入0 次删除
  1. 5 0
      app/Exceptions/Handler.php

+ 5 - 0
app/Exceptions/Handler.php

@@ -48,6 +48,11 @@ class Handler extends ExceptionHandler
      */
     public function render($request, Throwable $exception)
     {
+        if ($request->wantsJson())
+            return response()->json(
+                ['error' => $exception->getMessage()],
+                method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
+            );
         return parent::render($request, $exception);
     }
 }