update backend
This commit is contained in:
+30
-14
@@ -1,27 +1,43 @@
|
||||
package com.walkguide.exception;
|
||||
|
||||
import com.walkguide.dto.ApiResponse;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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);
|
||||
@ExceptionHandler(ResourceNotFoundException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handleNotFound(ResourceNotFoundException ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.body(ApiResponse.error("NOT_FOUND", ex.getMessage()));
|
||||
}
|
||||
|
||||
// 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);
|
||||
@ExceptionHandler(PairingException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handlePairing(PairingException ex) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
.body(ApiResponse.error("PAIRING_ERROR", ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handleValidation(MethodArgumentNotValidException ex) {
|
||||
String msg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
.body(ApiResponse.error("VALIDATION_ERROR", msg));
|
||||
}
|
||||
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handleRuntime(RuntimeException ex) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.body(ApiResponse.error("AUTH_ERROR", ex.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ApiResponse<Object>> handleGeneric(Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(ApiResponse.error("INTERNAL_ERROR", "Terjadi kesalahan internal"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.walkguide.exception;
|
||||
|
||||
public class PairingException extends RuntimeException {
|
||||
public PairingException(String message) { super(message); }
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.walkguide.exception;
|
||||
|
||||
public class ResourceNotFoundException extends RuntimeException {
|
||||
public ResourceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user