1

I have a Spring Boot 3 application with a global exception handler that works perfectly when I run it locally, but when I deploy the same code to Render, the exception handling stops working — I only get a generic 500 Internal Server Error response.

Here’s my global exception handler:

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(DataConflictException.class)
    public ResponseEntity<Object> handleDataConflict(DataConflictException ex) {
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("timestamp", LocalDateTime.now());
        body.put("status", HttpStatus.CONFLICT.value());
        body.put("error", "Data Conflict");
        body.put("message", ex.getMessage());
        return new ResponseEntity<>(body, HttpStatus.CONFLICT);
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex) {
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("timestamp", LocalDateTime.now());
        body.put("status", HttpStatus.CONFLICT.value());
        body.put("error", "Data Conflict");
        body.put("message", ex.getMostSpecificCause().getMessage());
        return new ResponseEntity<>(body, HttpStatus.CONFLICT);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleGenericException(Exception ex) {
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("timestamp", LocalDateTime.now());
        body.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
        body.put("error", "Internal Error");
        body.put("message", ex.getMessage());
        return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

When I run the application locally (using IntelliJ), if a duplicate key is inserted into the database (PostgreSQL), I get a proper JSON response like this:

{
  "timestamp": "...",
  "status": 409,
  "error": "Data Conflict",
  "message": "This email has already been registered"
}

However, when I deploy the exact same code to Render, I only get this response instead:

{
  "timestamp": "...",
  "status": 500,
  "error": "Internal Error",
  "message": "could not execute statement [ERROR: duplicate key value violates unique constraint ...]"
}

It looks like my @RestControllerAdvice isn’t being triggered

My project structure is like this:

com.salus.agenda
 ├── SalusApplication.java  (@SpringBootApplication)
 ├── Configuration
 │    ├── SecurityConfiguration.java
 │    └── GlobalExceptionHandler.java
 ├── Controllers
 ├── Services
 └── Repositories

I’ve confirmed that everything works locally.
Is there any reason why the @RestControllerAdvice might not be registered or triggered when running on Render?
Could it be related to Spring profiles, filters (like my JWT filter), or database-level exceptions happening before the controller layer?

Any insights or troubleshooting tips would be appreciated.

4
  • Is this error occurring in a @Controller or a @RestController? Commented Nov 25 at 2:38
  • 1
    Without knowning how you are deploying this this will be impossible to answer. How are you building the artifact, what does your @SpringBootApplication annotated class look like? There is too little information here. Commented Nov 25 at 6:56
  • > It looks like my @RestControllerAdvice isn’t being triggered - according to 500 payload advice is actually being triggered, however that is the general one. Looks like the actual exception differs from your expectations, just add extra information to payload. Commented Nov 25 at 8:10
  • enbale debug logs and show us the startup logs so we can see that it gets registered and then show us the logs when the exception is thrown so we can see what is thrown and way it takes out of the application. Without logs, this is impossible. Commented Nov 26 at 2:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.