fix: add error handling to SearchProviderRegistry search method
Wrap individual provider search calls in try-catch so a single provider failure no longer breaks the entire search. Failures are logged as warnings and the remaining providers continue normally. Fixes #11 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f90cd2c3ec
commit
9f7e5783ba
1 changed files with 48 additions and 37 deletions
|
|
@ -13,6 +13,7 @@ namespace Core\Admin\Search;
|
|||
|
||||
use Core\Admin\Search\Contracts\SearchProvider;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
|
|
@ -111,46 +112,56 @@ class SearchProviderRegistry
|
|||
$grouped = [];
|
||||
|
||||
foreach ($this->availableProviders($user, $workspace) as $provider) {
|
||||
$type = $provider->searchType();
|
||||
$results = $provider->search($query, $limitPerProvider);
|
||||
try {
|
||||
$type = $provider->searchType();
|
||||
$results = $provider->search($query, $limitPerProvider);
|
||||
|
||||
// Convert results to array format with type/icon
|
||||
$formattedResults = $results->map(function ($result) use ($provider) {
|
||||
if ($result instanceof SearchResult) {
|
||||
return $result->withTypeAndIcon(
|
||||
$provider->searchType(),
|
||||
$provider->searchIcon()
|
||||
)->toArray();
|
||||
// Convert results to array format with type/icon
|
||||
$formattedResults = $results->map(function ($result) use ($provider) {
|
||||
if ($result instanceof SearchResult) {
|
||||
return $result->withTypeAndIcon(
|
||||
$provider->searchType(),
|
||||
$provider->searchIcon()
|
||||
)->toArray();
|
||||
}
|
||||
|
||||
// Handle array results
|
||||
if (is_array($result)) {
|
||||
$searchResult = SearchResult::fromArray($result);
|
||||
|
||||
return $searchResult->withTypeAndIcon(
|
||||
$provider->searchType(),
|
||||
$provider->searchIcon()
|
||||
)->toArray();
|
||||
}
|
||||
|
||||
// Handle model objects with getUrl
|
||||
return [
|
||||
'id' => (string) ($result->id ?? uniqid()),
|
||||
'title' => (string) ($result->title ?? $result->name ?? ''),
|
||||
'subtitle' => (string) ($result->subtitle ?? $result->description ?? ''),
|
||||
'url' => $provider->getUrl($result),
|
||||
'type' => $provider->searchType(),
|
||||
'icon' => $provider->searchIcon(),
|
||||
'meta' => [],
|
||||
];
|
||||
})->toArray();
|
||||
|
||||
if (! empty($formattedResults)) {
|
||||
$grouped[$type] = [
|
||||
'label' => $provider->searchLabel(),
|
||||
'icon' => $provider->searchIcon(),
|
||||
'results' => $formattedResults,
|
||||
];
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Search provider failed', [
|
||||
'provider' => $provider::class,
|
||||
'query' => $query,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
// Handle array results
|
||||
if (is_array($result)) {
|
||||
$searchResult = SearchResult::fromArray($result);
|
||||
|
||||
return $searchResult->withTypeAndIcon(
|
||||
$provider->searchType(),
|
||||
$provider->searchIcon()
|
||||
)->toArray();
|
||||
}
|
||||
|
||||
// Handle model objects with getUrl
|
||||
return [
|
||||
'id' => (string) ($result->id ?? uniqid()),
|
||||
'title' => (string) ($result->title ?? $result->name ?? ''),
|
||||
'subtitle' => (string) ($result->subtitle ?? $result->description ?? ''),
|
||||
'url' => $provider->getUrl($result),
|
||||
'type' => $provider->searchType(),
|
||||
'icon' => $provider->searchIcon(),
|
||||
'meta' => [],
|
||||
];
|
||||
})->toArray();
|
||||
|
||||
if (! empty($formattedResults)) {
|
||||
$grouped[$type] = [
|
||||
'label' => $provider->searchLabel(),
|
||||
'icon' => $provider->searchIcon(),
|
||||
'results' => $formattedResults,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue