Improve error handling in readFiles method for directory access and readability

This commit is contained in:
dhanabalan
2025-09-26 10:47:35 +05:30
parent 3d21975096
commit 64237c3da0

View File

@@ -62,30 +62,33 @@ class SapFileController extends Controller
$path = "/LaserPRD/{$plantCode}"; $path = "/LaserPRD/{$plantCode}";
$isDir = is_dir($path); // $isDir = is_dir($path);
$isReadable = is_readable($path); // $isReadable = is_readable($path);
if (!$isDir || !$isReadable) { // Check if folder exists
if (!is_dir($path)) {
return response()->json([ return response()->json([
'status' => 'error', 'status_code' => 'ERROR',
'message' => 'Folder not accessible', 'status_description' => "Folder for plant-code {$plantCode} not found!"
'debug' => [ ], 404);
'path_checked' => $path, }
'is_dir' => $isDir,
'is_readable' => $isReadable, if (!is_readable($path)) {
] return response()->json([
'status_code' => 'ERROR',
'status_description' => "Folder for plant-code {$plantCode} exists but is not readable!"
], 500); ], 500);
} }
// Scan folder // Scan folder
$allFiles = scandir($path); $allFiles = scandir($path);
if ($allFiles === false) { if ($allFiles === false) {
return response()->json([ return response()->json([
'status' => 'error', 'status' => 'error',
'message' => 'Failed to scan directory', 'message' => 'Failed to scan directory',
'debug' => ['path_checked' => $path] 'debug' => ['path_checked' => $path]
], 500); ], 500);
} }
$files = array_filter($allFiles, function($item) use ($path) { $files = array_filter($allFiles, function($item) use ($path) {
$fullPath = $path . '/' . $item; $fullPath = $path . '/' . $item;