first week launch
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
package com.walkguide.exception;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
import com.walkguide.dto.ApiResponse;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
// Nangkep error kalau login gagal / user gak ketemu
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handleRuntimeException(RuntimeException ex) {
|
||||
ApiResponse<Object> response = new ApiResponse<>(false, "AUTH_ERROR", ex.getMessage());
|
||||
return ResponseEntity.status(401).body(response);
|
||||
}
|
||||
|
||||
// Nangkep error dari Validasi (misal email format salah)
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handleValidationException(MethodArgumentNotValidException ex) {
|
||||
String errorMsg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
|
||||
ApiResponse<Object> response = new ApiResponse<>(false, "VALIDATION_ERROR", errorMsg);
|
||||
return ResponseEntity.status(400).body(response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user