* error messages may be localised, thus digging out the code
* when request Name attribute from arbitrary name, FileNotFound will not
  be returned. This caused folder creation to fail.

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2019-04-05 17:10:47 +02:00
Родитель 9ddd07bcaa
Коммит 7836c7b177
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7424F1874854DF23
3 изменённых файлов: 27 добавлений и 11 удалений

Просмотреть файл

@ -23,6 +23,8 @@
namespace OCA\SharePoint;
use function explode;
use function json_decode;
use OCA\SharePoint\Storage\Storage;
use Office365\PHP\Client\Runtime\Auth\AuthenticationContext;
use Office365\PHP\Client\Runtime\ClientObject;
@ -110,6 +112,7 @@ class Client {
if(preg_match('/^The file \/.* does not exist\.$/', $e->getMessage()) !== 1
&& $e->getMessage() !== 'Unknown Error'
&& $e->getMessage() !== 'File Not Found.'
&& !$this->isErrorDoesNotExist($e)
) {
# Unexpected Exception, pass it on
throw $e;
@ -121,6 +124,20 @@ class Client {
throw new NotFoundException('File or Folder not found');
}
private function isErrorDoesNotExist(\Exception $e): bool {
$trace = $e->getTrace()[0];
if($trace['function'] !== 'validateResponse' || !isset($trace['args'][0])) {
return false;
}
$error = json_decode($trace['args'][0], true)['error'];
$errorCode = (int)explode(',', $error['code'])[0];
return in_array($errorCode, [
-2146232832, # Microsoft.SharePoint.SPException (unclear)
-2147024894, # File cannot be found
-1, # unknown error
]);
}
/**
* returns a File instance for the provided path
*

Просмотреть файл

@ -32,6 +32,7 @@ use OCA\SharePoint\NotFoundException;
use OCA\SharePoint\Client;
use OCA\SharePoint\ClientFactory;
use OCP\Files\FileInfo;
use OCP\ILogger;
use OCP\ITempManager;
use Office365\PHP\Client\Runtime\ClientObjectCollection;
use Office365\PHP\Client\SharePoint\File;
@ -126,6 +127,12 @@ class Storage extends Common {
return true;
} catch (\Exception $e) {
$this->fileCache->remove($serverUrl);
\OC::$server->getLogger()->logException($e,
[
'app' => 'sharepoint',
'level' => ILogger::INFO
]
);
return false;
}
}
@ -607,11 +614,7 @@ class Storage extends Common {
throw new NotFoundException('File or Folder not found');
} else if($entry === null || !isset($entry['instance'])) {
try {
$file = $this->spClient->fetchFileOrFolder($serverUrl, [
self::SP_PROPERTY_SIZE,
self::SP_PROPERTY_MTIME,
self::SP_PROPERTY_NAME,
]);
$file = $this->spClient->fetchFileOrFolder($serverUrl);
} catch (NotFoundException $e) {
$this->fileCache->set($serverUrl, false);
throw $e;

Просмотреть файл

@ -153,9 +153,7 @@ class SharePointTest extends TestCase {
$this->client->expects($this->once())
->method('fetchFileOrFolder')
->with($serverPath, [
Storage::SP_PROPERTY_SIZE, Storage::SP_PROPERTY_MTIME, Storage::SP_PROPERTY_NAME
])
->with($serverPath)
->willReturn($folderMock);
$data = $this->storage->stat($path);
@ -171,9 +169,7 @@ class SharePointTest extends TestCase {
$this->client->expects($this->once())
->method('fetchFileOrFolder')
->with($serverPath, [
Storage::SP_PROPERTY_SIZE, Storage::SP_PROPERTY_MTIME, Storage::SP_PROPERTY_NAME
])
->with($serverPath)
->willThrowException(new NotFoundException());
$this->assertFalse($this->storage->stat($path));