Return 404 if user not found

This commit is contained in:
T'JAMPENS QUENTIN p2406187
2025-12-17 15:16:03 +01:00
parent dfb70cc5ce
commit 4c907c8ba5
2 changed files with 6 additions and 3 deletions
+4 -2
View File
@@ -3,11 +3,11 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Services\GetRole;
use App\Services\UserService; use App\Services\UserService;
use Propaganistas\LaravelPhone\PhoneNumber; use Propaganistas\LaravelPhone\PhoneNumber;
@@ -83,7 +83,9 @@ class UserController extends Controller
try { try {
return response()->json(UserService::getData($id)); return response()->json(UserService::getData($id));
} catch(\Exception $e) { } catch (ModelNotFoundException $e) {
return response()->json(['message' => 'Utilisateur non trouvé'], 404);
} catch (\Exception $e) {
Log::info($e->getMessage()); Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500); return response()->json(['message' => "Server error"], 500);
} }
+2 -1
View File
@@ -7,7 +7,8 @@ class UserService
{ {
public static function getData(int $id) : array { public static function getData(int $id) : array {
$user = User::find($id); $user = User::findOrFail($id);
return [ return [
"id" => $user->id, "id" => $user->id,
"name" => $user->name, "name" => $user->name,