GlobalExceptionHandler.java
package com.ctrlbuy.webshop.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.http.ResponseEntity;
import java.net.URI;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(CustomerNotFoundException.class)
public ResponseEntity<ProblemDetail> handleCustomerNotFoundException(
CustomerNotFoundException ex, WebRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
problemDetail.setTitle("Kund hittades inte");
problemDetail.setDetail(ex.getMessage());
problemDetail.setInstance(URI.create(request.getDescription(false).replace("uri=", "")));
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(problemDetail);
}
}