wrap get item with try catch

This commit is contained in:
Rev 2021-10-10 13:10:35 +09:00
parent 826bc13956
commit 7cdd4645bd

View File

@ -5,6 +5,9 @@ namespace App\Http\Controllers;
use App\Data\ItemsCollection;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Throwable;
class ItemController extends Controller
{
@ -35,8 +38,16 @@ class ItemController extends Controller
*/
public function getItem(string $id): JsonResponse
{
try {
return response()->json([
'item' => $this->itemsCollection->getItemById($id),
]);
} catch (Throwable $exception) {
Log::error($exception->getMessage());
Log::error($exception->getTraceAsString());
return response()->json([
'item' => [],
], Response::HTTP_NOT_FOUND);
}
}
}