Skip to content

Commit

Permalink
Updated the Http Response code and added logging for debugging purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
vikhyat187 committed Dec 25, 2023
1 parent c0f8d4a commit 07ed4ef
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@
import com.RDS.skilltree.Common.Response.GenericResponse;
import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException;
import com.RDS.skilltree.Exceptions.NoEntityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler({NoEntityException.class})
public ResponseEntity<GenericResponse<Object>> handleNoEntityException(NoEntityException ex){
log.error("NoEntityException - Error : {}", ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new GenericResponse<>(null, ex.getMessage()));
}

@ExceptionHandler({EntityAlreadyExistsException.class})
public ResponseEntity<GenericResponse<Object>> handleEntityAlreadyExistsException(EntityAlreadyExistsException ex){
log.error("EntityAlreadyExistsException - Error : {}", ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.status(HttpStatus.CONFLICT)
.body(new GenericResponse<>(null, ex.getMessage()));
}

@ExceptionHandler({RuntimeException.class})
public ResponseEntity<GenericResponse<Object>> handleRuntimException(RuntimeException ex){
log.error("Runtime Exception - Error : {}", ex.getMessage(), ex);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new GenericResponse<>(null, ex.getMessage()));
.body(new GenericResponse<>(null, "Something went wrong, please try communicating on `#wg-skill-tree discord` channel" ));
}

}

0 comments on commit 07ed4ef

Please sign in to comment.