|
5 | 5 | use EasyRdf_Exception; |
6 | 6 | use EasyRdf_Graph as Graph; |
7 | 7 | use Laminas\Diactoros\ServerRequest; |
| 8 | +use League\Flysystem\FileExistsException; |
8 | 9 | use League\Flysystem\FilesystemInterface as Filesystem; |
9 | 10 | use Psr\Http\Message\ResponseInterface as Response; |
10 | 11 | use Psr\Http\Message\ServerRequestInterface as Request; |
@@ -355,8 +356,29 @@ private function handleCreateRequest(Response $response, string $path, $contents |
355 | 356 | $response->getBody()->write($message); |
356 | 357 | $response = $response->withStatus(400); |
357 | 358 | } 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 | + |
360 | 382 | if ($success) { |
361 | 383 | $response = $response->withHeader("Location", $this->baseUrl . $path); |
362 | 384 | $response = $response->withStatus(201); |
|
0 commit comments