Skip to content

Commit 70354c8

Browse files
committed
Try to show an more detailed error message, if digikey needs oauth reconnection
1 parent 43601e0 commit 70354c8

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

src/Controller/InfoProviderController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use App\Entity\Parts\Manufacturer;
2727
use App\Entity\Parts\Part;
28+
use App\Exceptions\OAuthReconnectRequiredException;
2829
use App\Form\InfoProviderSystem\PartSearchType;
2930
use App\Services\InfoProviderSystem\ExistingPartFinder;
3031
use App\Services\InfoProviderSystem\PartInfoRetriever;
@@ -175,8 +176,11 @@ public function search(Request $request, #[MapEntity(id: 'target')] ?Part $updat
175176
$this->addFlash('error',$e->getMessage());
176177
//Log the exception
177178
$exceptionLogger->error('Error during info provider search: ' . $e->getMessage(), ['exception' => $e]);
179+
} catch (OAuthReconnectRequiredException $e) {
180+
$this->addFlash('error', t('info_providers.search.error.oauth_reconnect', ['%provider%' => $e->getProviderName()]));
178181
}
179182

183+
180184
// modify the array to an array of arrays that has a field for a matching local Part
181185
// the advantage to use that format even when we don't look for local parts is that we
182186
// always work with the same interface
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Exceptions;
25+
26+
use Throwable;
27+
28+
class OAuthReconnectRequiredException extends \RuntimeException
29+
{
30+
private string $providerName;
31+
32+
public function __construct(string $message = "You need to reconnect the OAuth connection for this provider!", int $code = 0, ?Throwable $previous = null)
33+
{
34+
parent::__construct($message, $code, $previous);
35+
}
36+
37+
public static function forProvider(string $providerName): self
38+
{
39+
$exception = new self("You need to reconnect the OAuth connection for the provider '$providerName'!");
40+
$exception->providerName = $providerName;
41+
return $exception;
42+
}
43+
44+
public function getProviderName(): string
45+
{
46+
return $this->providerName;
47+
}
48+
}

src/Services/InfoProviderSystem/Providers/DigikeyProvider.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace App\Services\InfoProviderSystem\Providers;
2525

2626
use App\Entity\Parts\ManufacturingStatus;
27+
use App\Exceptions\OAuthReconnectRequiredException;
2728
use App\Services\InfoProviderSystem\DTOs\FileDTO;
2829
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
2930
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
@@ -117,12 +118,22 @@ public function searchByKeyword(string $keyword): array
117118
];
118119

119120
//$response = $this->digikeyClient->request('POST', '/Search/v3/Products/Keyword', [
120-
$response = $this->digikeyClient->request('POST', '/products/v4/search/keyword', [
121-
'json' => $request,
122-
'auth_bearer' => $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME)
123-
]);
121+
try {
122+
$response = $this->digikeyClient->request('POST', '/products/v4/search/keyword', [
123+
'json' => $request,
124+
'auth_bearer' => $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME)
125+
]);
126+
127+
$response_array = $response->toArray();
128+
} catch (\InvalidArgumentException $exception) {
129+
//Check if the exception was caused by an invalid or expired token
130+
if (str_contains($exception->getMessage(), 'access_token')) {
131+
throw OAuthReconnectRequiredException::forProvider($this->getProviderKey());
132+
}
133+
134+
throw $exception;
135+
}
124136

125-
$response_array = $response->toArray();
126137

127138

128139
$result = [];
@@ -150,9 +161,18 @@ public function searchByKeyword(string $keyword): array
150161

151162
public function getDetails(string $id): PartDetailDTO
152163
{
153-
$response = $this->digikeyClient->request('GET', '/products/v4/search/' . urlencode($id) . '/productdetails', [
154-
'auth_bearer' => $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME)
155-
]);
164+
try {
165+
$response = $this->digikeyClient->request('GET', '/products/v4/search/' . urlencode($id) . '/productdetails', [
166+
'auth_bearer' => $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME)
167+
]);
168+
} catch (\InvalidArgumentException $exception) {
169+
//Check if the exception was caused by an invalid or expired token
170+
if (str_contains($exception->getMessage(), 'access_token')) {
171+
throw OAuthReconnectRequiredException::forProvider($this->getProviderKey());
172+
}
173+
174+
throw $exception;
175+
}
156176

157177
$response_array = $response->toArray();
158178
$product = $response_array['Product'];

translations/messages.en.xlf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14244,5 +14244,12 @@ Please note, that you can not impersonate a disabled user. If you try you will g
1424414244
<target>If checked, the part tables for categories, footprints, etc. should include all parts of child categories. If not checked, only parts that strictly belong to the clicked node are shown.</target>
1424514245
</segment>
1424614246
</unit>
14247+
<unit id="Gdlnmav" name="info_providers.search.error.oauth_reconnect">
14248+
<segment>
14249+
<source>info_providers.search.error.oauth_reconnect</source>
14250+
<target>You need to reconnect OAuth for following providers: %provider%
14251+
You can do this in the provider info list.</target>
14252+
</segment>
14253+
</unit>
1424714254
</file>
1424814255
</xliff>

0 commit comments

Comments
 (0)