Skip to content

Commit

Permalink
added log statements for troubleshooting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwani-cgi committed Oct 2, 2024
1 parent fe0399a commit 38aa548
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.moh.phlat.backend.advice;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -10,10 +13,13 @@
import com.moh.phlat.backend.exception.HandleNotFoundException;

@RestControllerAdvice
public class ApplicationExceptionHanler {
public class ApplicationExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(ApplicationExceptionHandler.class);

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleInvalidArgument(MethodArgumentNotValidException ex) {
logger.error("Invalid Method Argument", ex);
Map<String, String> errorMap = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
errorMap.put(error.getField(), error.getDefaultMessage());
Expand All @@ -24,6 +30,7 @@ public Map<String, String> handleInvalidArgument(MethodArgumentNotValidException
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(HandleInternalException.class)
public Map<String, String> handleBusinessException(HandleInternalException ex) {
logger.error("Internal error occurred", ex);
Map<String, String> errorMap = new HashMap<>();
errorMap.put("Message", ex.getMessage());
return errorMap;
Expand All @@ -32,6 +39,7 @@ public Map<String, String> handleBusinessException(HandleInternalException ex) {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(HandleNotFoundException.class)
public Map<String, String> handleBusinessException(HandleNotFoundException ex) {
logger.error("Resource not found", ex);
Map<String, String> errorMap = new HashMap<>();
errorMap.put("Message", ex.getMessage());
return errorMap;
Expand All @@ -40,6 +48,7 @@ public Map<String, String> handleBusinessException(HandleNotFoundException ex) {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
public Map<String, String> handleOtherException(Exception ex) {
logger.error("An error occurred", ex);
Map<String, String> errorMap = new HashMap<>();
errorMap.put("Message", "The server could not process the request. Please contact customer support");
return errorMap;
Expand Down

0 comments on commit 38aa548

Please sign in to comment.