Skip to content

Commit

Permalink
fix: Log and handle errors
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
  • Loading branch information
elzody committed Jul 25, 2024
1 parent f63d502 commit 6f85ff4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 8 additions & 5 deletions lib/Controller/TemplateFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException;
use OCP\IRequest;

class TemplateFieldController extends OCSController {
Expand Down Expand Up @@ -50,8 +49,8 @@ public function extractFields(int $fileId): DataResponse {
$fields = $this->templateFieldService->extractFields($fileId);

return new DataResponse($fields, Http::STATUS_OK);
} catch (NotFoundException $e) {
return new DataResponse([$e->getMessage()], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
return new DataResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -62,8 +61,12 @@ public function extractFields(int $fileId): DataResponse {
*/
#[NoAdminRequired]
public function fillFields(int $fileId, array $fields): DataResponse {
$this->templateFieldService->fillFields($fileId, $fields);
try {
$this->templateFieldService->fillFields($fileId, $fields);

return new DataResponse([], Http::STATUS_OK);
return new DataResponse([], Http::STATUS_OK);
} catch (\Exception $e) {
return new DataResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}
11 changes: 7 additions & 4 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;

class TemplateFieldService {
public function __construct(
private IClientService $clientService,
private AppConfig $appConfig,
private IRootFolder $rootFolder
private IRootFolder $rootFolder,
private LoggerInterface $logger
) {
}

Expand Down Expand Up @@ -71,8 +73,8 @@ public function extractFields(Node|int $file) {

return array_merge([], ...$fields);
} catch (\Exception $e) {
// handle exception
return $e->getMessage();
$this->logger->error($e->getMessage());
return [];
}
}

Expand Down Expand Up @@ -116,7 +118,8 @@ public function fillFields(Node|int $file, array $fields = []) {

return $response->getBody();
} catch (\Exception $e) {
return $e->getMessage();
$this->logger->error($e->getMessage());
throw $e;
}
}
}

0 comments on commit 6f85ff4

Please sign in to comment.