Skip to content

Commit 9c3fe72

Browse files
committed
Initial Commit
1 parent ee8c7e0 commit 9c3fe72

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

src/main/java/com/webservice/mobile/app/exceptions/AppExceptionsHandler.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
package com.webservice.mobile.app.exceptions;
22

3+
import com.webservice.mobile.app.ui.model.response.ErrorMessage;
34
import org.springframework.http.HttpHeaders;
45
import org.springframework.http.HttpStatus;
56
import org.springframework.http.ResponseEntity;
67
import org.springframework.web.bind.annotation.ControllerAdvice;
78
import org.springframework.web.bind.annotation.ExceptionHandler;
89
import org.springframework.web.context.request.WebRequest;
910

11+
import java.util.Date;
12+
1013
@ControllerAdvice
1114
public class AppExceptionsHandler {
1215

1316
@ExceptionHandler(value = {UserServiceException.class})
1417
public ResponseEntity<Object> handleUserServiceException(UserServiceException exception,
1518
WebRequest webRequest){
16-
return new ResponseEntity<>(exception.getMessage(),new HttpHeaders(),
19+
20+
ErrorMessage errorMessage = new ErrorMessage(new Date(),exception.getMessage());
21+
22+
23+
return new ResponseEntity<>(errorMessage,new HttpHeaders(),
24+
HttpStatus.INTERNAL_SERVER_ERROR);
25+
}
26+
27+
@ExceptionHandler(value = {Exception.class})
28+
public ResponseEntity<Object> handleOtherException(UserServiceException ex,
29+
WebRequest request){
30+
31+
ErrorMessage errorMessage = new ErrorMessage(new Date(),ex.getMessage());
32+
33+
34+
return new ResponseEntity<>(errorMessage,new HttpHeaders(),
1735
HttpStatus.INTERNAL_SERVER_ERROR);
1836
}
1937

src/main/java/com/webservice/mobile/app/service/impl/UserServiceImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.webservice.mobile.app.service.impl;
22

33

4+
import com.webservice.mobile.app.exceptions.UserServiceException;
45
import com.webservice.mobile.app.io.entity.UserEntity;
56
import com.webservice.mobile.app.io.repositories.UserRepository;
67
import com.webservice.mobile.app.service.UserService;
78
import com.webservice.mobile.app.shared.Utils;
89
import com.webservice.mobile.app.shared.dto.UserDTO;
10+
import com.webservice.mobile.app.ui.model.response.ErrorMessages;
911
import org.springframework.beans.BeanUtils;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.security.core.userdetails.User;
@@ -33,8 +35,11 @@ public class UserServiceImpl implements UserService {
3335
public UserDTO createUser(UserDTO userDTO) {
3436

3537

36-
if (userRepository.findUserByEmail(userDTO.getEmail()) !=null)
37-
throw new RuntimeException("Record Already Exists");
38+
if (userRepository != null){
39+
if (userRepository.findUserByEmail(userDTO.getEmail()) !=null)
40+
throw new UserServiceException(ErrorMessages.RECORD_ALREADY_EXISTS.getErrorMessage());
41+
}
42+
3843

3944
UserEntity userEntity = new UserEntity();
4045
BeanUtils.copyProperties(userDTO,userEntity);
@@ -53,7 +58,7 @@ public UserDTO createUser(UserDTO userDTO) {
5358
@Override
5459
public UserDTO getUser(String email) {
5560
UserEntity userEntity = userRepository.findUserByEmail(email);
56-
if (userEntity == null) throw new UsernameNotFoundException(email);
61+
if (userEntity == null) throw new UserServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());
5762
UserDTO returnValue = new UserDTO();
5863
BeanUtils.copyProperties(userEntity,returnValue);
5964
return returnValue;
@@ -65,7 +70,7 @@ public UserDTO getUserByUserId(String userId) {
6570

6671
UserDTO returnValue = new UserDTO();
6772
UserEntity userEntity = userRepository.findByUserId(userId);
68-
if (userEntity == null) throw new UsernameNotFoundException(userId);
73+
if (userEntity == null) throw new UserServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());
6974
BeanUtils.copyProperties(userEntity,returnValue);
7075

7176
return returnValue;

src/main/java/com/webservice/mobile/app/ui/controller/UserController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public UserRest createUser(@RequestBody UserDetailsRequestModel userDetails) thr
3737

3838
UserRest returnValue = new UserRest();
3939

40-
4140
if (userDetails.getFirstName().isEmpty()) throw new
4241
UserServiceException(ErrorMessages.MISSING_REQUIRED_FIELD.getErrorMessage());
4342

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.webservice.mobile.app.ui.model.response;
2+
3+
import java.util.Date;
4+
5+
public class ErrorMessage {
6+
private Date timeStamp;
7+
private String message;
8+
9+
public ErrorMessage() {
10+
11+
}
12+
13+
public ErrorMessage(Date timeStamp, String message) {
14+
this.timeStamp = timeStamp;
15+
this.message = message;
16+
}
17+
18+
public Date getTimeStamp() {
19+
return timeStamp;
20+
}
21+
22+
public void setTimeStamp(Date timeStamp) {
23+
this.timeStamp = timeStamp;
24+
}
25+
26+
public String getMessage() {
27+
return message;
28+
}
29+
30+
public void setMessage(String message) {
31+
this.message = message;
32+
}
33+
}

0 commit comments

Comments
 (0)