49 lines
2.1 KiB
Java
49 lines
2.1 KiB
Java
/*
|
|
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
|
|
* Your use of it is subject to your agreement with Google.
|
|
*/
|
|
|
|
package com.example.exception;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
@ControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
|
@ExceptionHandler(DialogflowClientException.class)
|
|
public ResponseEntity<Map<String, String>> handleDialogflowClientException(
|
|
DialogflowClientException ex) {
|
|
Map<String, String> error = new HashMap<>();
|
|
error.put("error", "Error communicating with Dialogflow");
|
|
error.put("message", ex.getMessage());
|
|
logger.error("DialogflowClientException: {}", ex.getMessage());
|
|
return new ResponseEntity<>(error, HttpStatus.SERVICE_UNAVAILABLE);
|
|
}
|
|
|
|
@ExceptionHandler(GeminiClientException.class)
|
|
public ResponseEntity<Map<String, String>> handleGeminiClientException(GeminiClientException ex) {
|
|
Map<String, String> error = new HashMap<>();
|
|
error.put("error", "Error communicating with Gemini");
|
|
error.put("message", ex.getMessage());
|
|
logger.error("GeminiClientException: {}", ex.getMessage());
|
|
return new ResponseEntity<>(error, HttpStatus.SERVICE_UNAVAILABLE);
|
|
}
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
public ResponseEntity<Map<String, String>> handleAllExceptions(Exception ex) {
|
|
Map<String, String> error = new HashMap<>();
|
|
error.put("error", "Internal Server Error");
|
|
error.put("message", ex.getMessage());
|
|
logger.error("An unexpected error occurred: {}", ex.getMessage(), ex);
|
|
return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
} |