Skip to content

Commit e4c8885

Browse files
committed
Add handling for filesystem errors in Server.
1 parent 70bfa2e commit e4c8885

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

src/Server.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use EasyRdf_Exception;
66
use EasyRdf_Graph as Graph;
77
use Laminas\Diactoros\ServerRequest;
8+
use League\Flysystem\FileExistsException;
89
use League\Flysystem\FilesystemInterface as Filesystem;
910
use Psr\Http\Message\ResponseInterface as Response;
1011
use Psr\Http\Message\ServerRequestInterface as Request;
@@ -355,8 +356,29 @@ private function handleCreateRequest(Response $response, string $path, $contents
355356
$response->getBody()->write($message);
356357
$response = $response->withStatus(400);
357358
} else {
358-
// @FIXME: Handle error scenarios correctly (for instance trying to create a file underneath another file)
359-
$success = $filesystem->write($path, $contents);
359+
$success = false;
360+
361+
set_error_handler(static function($severity, $message, $filename, $line) {
362+
throw new \ErrorException($message, 0, $severity, $filename, $line);
363+
});
364+
365+
try {
366+
$success = $filesystem->write($path, $contents);
367+
} catch (FileExistsException $e) {
368+
$message = vsprintf(self::ERROR_PUT_EXISTING_RESOURCE, [$path]);
369+
$response->getBody()->write($message);
370+
371+
return $response->withStatus(400);
372+
} catch (Throwable $exception) {
373+
/*/ An error occurred in the underlying flysystem adapter /*/
374+
$message = vsprintf('Could not write to path %s: %s', [$path, $exception->getMessage()]);
375+
$response->getBody()->write($message);
376+
377+
return $response->withStatus(400);
378+
} finally {
379+
restore_error_handler();
380+
}
381+
360382
if ($success) {
361383
$response = $response->withHeader("Location", $this->baseUrl . $path);
362384
$response = $response->withStatus(201);

0 commit comments

Comments
 (0)