Skip to content

Commit 2edf947

Browse files
committed
fix: handle LibresignException
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 00f5737 commit 2edf947

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

lib/Controller/RequestSignatureController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\Libresign\Controller;
1010

1111
use OCA\Libresign\AppInfo\Application;
12+
use OCA\Libresign\Exception\LibresignException;
1213
use OCA\Libresign\Helper\ValidateHelper;
1314
use OCA\Libresign\Middleware\Attribute\RequireManager;
1415
use OCA\Libresign\ResponseDefinitions;
@@ -91,10 +92,23 @@ public function request(array $file, array $users, string $name, ?string $callba
9192
],
9293
Http::STATUS_OK
9394
);
95+
} catch (LibresignException $e) {
96+
$errorMessage = $e->getMessage();
97+
$decoded = json_decode($errorMessage, true);
98+
if (json_last_error() === JSON_ERROR_NONE && isset($decoded['errors'])) {
99+
$errorMessage = $decoded['errors'][0]['message'] ?? $errorMessage;
100+
}
101+
return new DataResponse(
102+
[
103+
'message' => $errorMessage,
104+
],
105+
Http::STATUS_UNPROCESSABLE_ENTITY
106+
);
94107
} catch (\Throwable $th) {
108+
$errorMessage = $th->getMessage();
95109
return new DataResponse(
96110
[
97-
'message' => $th->getMessage(),
111+
'message' => $errorMessage,
98112
],
99113
Http::STATUS_UNPROCESSABLE_ENTITY
100114
);

tests/integration/features/sign/request.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ Feature: request-signature
222222
| users | [{"identify":{"account":"signer2"}}] |
223223
| name | document |
224224
Then the response should be a JSON array with the following mandatory values
225-
| key | value |
226-
| (jq).ocs.data.message | User not found. |
225+
| key | value |
226+
| (jq).ocs.data.message | Invalid user |
227227

228228
Scenario: Request to sign with success using account as identifier
229229
Given as user "admin"
@@ -273,8 +273,8 @@ Feature: request-signature
273273
| name | document |
274274
Then the response should have a status code 422
275275
Then the response should be a JSON array with the following mandatory values
276-
| key | value |
277-
| (jq).ocs.data.message | User not found. |
276+
| key | value |
277+
| (jq).ocs.data.message | Invalid user |
278278

279279
Scenario: Request to sign with error using email as account identifier
280280
Given as user "admin"
@@ -285,8 +285,8 @@ Feature: request-signature
285285
| name | document |
286286
Then the response should have a status code 422
287287
Then the response should be a JSON array with the following mandatory values
288-
| key | value |
289-
| (jq).ocs.data.message | User not found. |
288+
| key | value |
289+
| (jq).ocs.data.message | Invalid user |
290290

291291
Scenario: Request to sign with success using email as identifier and URL as file
292292
Given as user "admin"

0 commit comments

Comments
 (0)