|
6 | 6 | import com.mongodb.client.result.UpdateResult; |
7 | 7 | import com.mongodb.samplemflix.exception.DatabaseOperationException; |
8 | 8 | import com.mongodb.samplemflix.exception.ResourceNotFoundException; |
| 9 | +import com.mongodb.samplemflix.exception.ServiceUnavailableException; |
9 | 10 | import com.mongodb.samplemflix.exception.ValidationException; |
| 11 | +import com.mongodb.samplemflix.exception.VoyageAPIException; |
| 12 | +import com.mongodb.samplemflix.exception.VoyageAuthException; |
10 | 13 | import com.mongodb.samplemflix.model.Movie; |
11 | 14 | import com.mongodb.samplemflix.model.dto.*; |
12 | 15 | import com.mongodb.samplemflix.repository.MovieRepository; |
@@ -821,8 +824,8 @@ public List<VectorSearchResult> vectorSearchMovies(String query, Integer limit) |
821 | 824 | // Check if Voyage API key is configured |
822 | 825 | if (voyageApiKey == null || voyageApiKey.trim().isEmpty() || |
823 | 826 | voyageApiKey.equals("your_voyage_api_key")) { |
824 | | - throw new ValidationException( |
825 | | - "Vector search unavailable: VOYAGE_API_KEY not configured. Please add your Voyage AI API key to the .env file" |
| 827 | + throw new ServiceUnavailableException( |
| 828 | + "Vector search unavailable: VOYAGE_API_KEY not configured. Please add your API key to the .env file" |
826 | 829 | ); |
827 | 830 | } |
828 | 831 |
|
@@ -929,10 +932,16 @@ public List<VectorSearchResult> vectorSearchMovies(String query, Integer limit) |
929 | 932 |
|
930 | 933 | return results; |
931 | 934 |
|
| 935 | + } catch (VoyageAuthException e) { |
| 936 | + // Re-raise Voyage AI authentication errors to be handled by GlobalExceptionHandler |
| 937 | + throw e; |
| 938 | + } catch (VoyageAPIException e) { |
| 939 | + // Re-raise Voyage AI API errors to be handled by GlobalExceptionHandler |
| 940 | + throw e; |
932 | 941 | } catch (IOException e) { |
933 | | - // Handle Voyage AI API errors |
| 942 | + // Handle network errors calling Voyage AI API |
934 | 943 | String errorMsg = e.getMessage() != null ? e.getMessage() : "Network error calling Voyage AI API"; |
935 | | - throw new DatabaseOperationException("Error performing vector search: " + errorMsg); |
| 944 | + throw new VoyageAPIException("Error performing vector search: " + errorMsg); |
936 | 945 | } catch (InterruptedException e) { |
937 | 946 | Thread.currentThread().interrupt(); |
938 | 947 | throw new DatabaseOperationException("Vector search was interrupted"); |
@@ -981,9 +990,12 @@ private List<Double> generateVoyageEmbedding(String text, String apiKey) throws |
981 | 990 | if (response.statusCode() != 200) { |
982 | 991 | // Handle authentication errors specifically |
983 | 992 | if (response.statusCode() == 401) { |
984 | | - throw new IOException("Invalid Voyage AI API key. Please check your VOYAGE_API_KEY in the .env file"); |
| 993 | + throw new VoyageAuthException("Invalid Voyage AI API key. Please check your VOYAGE_API_KEY in the .env file"); |
985 | 994 | } |
986 | | - throw new IOException("Voyage AI API returned status code " + response.statusCode() + ": " + response.body()); |
| 995 | + throw new VoyageAPIException( |
| 996 | + "Voyage AI API returned status code " + response.statusCode() + ": " + response.body(), |
| 997 | + response.statusCode() |
| 998 | + ); |
987 | 999 | } |
988 | 1000 |
|
989 | 1001 | // Parse the JSON response to extract the embedding |
|
0 commit comments