improved logs

This commit is contained in:
Lincpay Tech
2025-11-10 10:05:43 +05:30
parent af7cdaf5d7
commit 43baedfecd
53 changed files with 662 additions and 572 deletions
@@ -1,61 +1,57 @@
package com.billpaymentservice.bbps.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.*;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.*;
import com.billpaymentservice.bbps.dto.ResponseModel;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobalExceptionHandler {
// ✅ Handle CustomException
@ExceptionHandler(CustomException.class)
public ResponseEntity<ResponseModel<?>> handleCustomException(CustomException ex) {
return new ResponseEntity<>(ex.getResponseModel(), HttpStatus.BAD_REQUEST);
}
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
// ✅ Handle validation errors
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ResponseModel<?>> handleValidationExceptions(MethodArgumentNotValidException ex) {
public ResponseEntity<ResponseModel<?>> handleValidation(MethodArgumentNotValidException ex) {
List<Map<String, String>> fieldErrors = ex.getBindingResult().getFieldErrors()
.stream()
.map(fe -> Map.of("field", fe.getField(), "message", fe.getDefaultMessage()))
.collect(Collectors.toList());
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
errors.put(error.getField(), error.getDefaultMessage());
});
ResponseModel<?> response = new ResponseModel<>(
"Validation Failed",
"FAILED",
HttpStatus.BAD_REQUEST.value(),
errors
logger.warn("Validation failed: {}", fieldErrors);
ResponseModel<List<Map<String, String>>> resp = new ResponseModel<>(
"Validation failed for request",
"FAILED",
HttpStatus.BAD_REQUEST.value(),
null
);
// put validation errors into data or duplicate field in ResponseModel as required
resp.setData(fieldErrors);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resp);
}
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
@ExceptionHandler(AppException.class)
public ResponseEntity<ResponseModel<?>> handleAppException(AppException ex) {
logger.warn("AppException: status={}, message={}", ex.getStatus(), ex.getMessage());
ResponseModel<?> resp = new ResponseModel<>(ex.getMessage(), ex.getStatus(), ex.getStatusCode(), ex.getData());
return ResponseEntity.status(HttpStatus.valueOf(ex.getStatusCode())).body(resp);
}
// ✅ NEW: Handle ALL other exceptions (Generic Handler)
@ExceptionHandler(Exception.class)
public ResponseEntity<ResponseModel<?>> handleAllExceptions(Exception ex) {
// ✅ Internal logging (tumhare system ko pata chale)
System.err.println("INTERNAL SERVER ERROR: " + ex.getMessage());
ex.printStackTrace(); // Stack trace for debugging
// ✅ Client ko generic message
public ResponseEntity<ResponseModel<?>> handleAll(Exception ex) {
logger.error("Unhandled exception", ex);
ResponseModel<?> response = new ResponseModel<>(
"Service temporarily unavailable. Please try again later.",
"FAILED",
HttpStatus.INTERNAL_SERVER_ERROR.value(),
null
);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
}