task(module-ejb,module-web): implement handling exception

This commit is contained in:
Damian Pierzchalski 2018-12-17 16:04:52 +01:00
parent 8476fa70a1
commit 2596a23d9a
6 changed files with 129 additions and 10 deletions

View File

@ -4,10 +4,10 @@ import java.util.HashSet;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pl.myboardgames.exception.DataNotFoundException;
import pl.myboardgames.model.BoardGameEntity;
@Stateless
@ -28,7 +28,7 @@ public class BoardGameDao {
public BoardGameEntity getById(Long aId) {
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
if (boardGame == null) {
throw new EntityNotFoundException("Can't find BoardGame for ID "
throw new DataNotFoundException("Can't find BoardGame for ID "
+ aId);
}
return boardGame;
@ -42,7 +42,7 @@ public class BoardGameDao {
public Boolean remove(Long aId) {
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
if (boardGame == null) {
throw new EntityNotFoundException("Can't find BoardGame for ID "
throw new DataNotFoundException("Can't find BoardGame for ID "
+ aId);
}
em.remove(boardGame);
@ -52,7 +52,7 @@ public class BoardGameDao {
public BoardGameEntity update(Long aId, BoardGameEntity aBoardGame){
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
if (aBoardGame == null) {
throw new EntityNotFoundException("Can't find BoardGame for ID "
throw new DataNotFoundException("Can't find BoardGame for ID "
+ aId);
}
em.merge(aBoardGame);

View File

@ -4,9 +4,9 @@ import java.util.HashSet;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pl.myboardgames.exception.DataNotFoundException;
import pl.myboardgames.model.BoardGameEntity;
import pl.myboardgames.model.UserEntity;
@ -29,7 +29,7 @@ public class UserDao {
public UserEntity getById(Long aId) {
UserEntity user = em.find(UserEntity.class, aId);
if (user == null) {
throw new EntityNotFoundException("Can't find User for ID "
throw new DataNotFoundException("Can't find User for ID "
+ aId);
}
return user;
@ -43,7 +43,7 @@ public class UserDao {
public Boolean remove(Long aId) {
UserEntity user = em.find(UserEntity.class, aId);
if (user == null) {
throw new EntityNotFoundException("Can't find User for ID "
throw new DataNotFoundException("Can't find User for ID "
+ aId);
}
em.remove(user);
@ -53,7 +53,7 @@ public class UserDao {
public UserEntity update(Long aId, UserEntity aUser){
UserEntity user = em.find(UserEntity.class, aId);
if (user == null) {
throw new EntityNotFoundException("Can't find User for ID "
throw new DataNotFoundException("Can't find User for ID "
+ aId);
}
em.merge(aUser);
@ -64,10 +64,10 @@ public class UserDao {
UserEntity user = em.find(UserEntity.class, aId);
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aBoardGameId);
if (user == null) {
throw new EntityNotFoundException("Can't find User for ID "
throw new DataNotFoundException("Can't find User for ID "
+ aId);
} else if(boardGame == null){
throw new EntityNotFoundException("Can't find BoardGame for ID "
throw new DataNotFoundException("Can't find BoardGame for ID "
+ aBoardGameId);
}
user.addBoardGame(boardGame);

View File

@ -0,0 +1,19 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames.exception;
/**
*
* @author dpierzchalski
*/
public class DataNotFoundException extends RuntimeException {
public DataNotFoundException(String message) {
super(message);
}
}

View File

@ -0,0 +1,28 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames.exception;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import pl.myboardgames.model.ErrorMessage;
/**
*
* @author dpierzchalski
*/
@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException> {
@Override
public Response toResponse(DataNotFoundException exception) {
ErrorMessage errorMessage = new ErrorMessage(exception.getMessage(),404);
return Response.status(404).entity(errorMessage).build();
}
}

View File

@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames.exception;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import pl.myboardgames.model.ErrorMessage;
/**
*
* @author dpierzchalski
*/
@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable exception) {
ErrorMessage errorMessage = new ErrorMessage(exception.getMessage(), 500);
return Response.status(500).entity(errorMessage).build();
}
}

View File

@ -0,0 +1,46 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames.model;
/**
*
* @author dpierzchalski
*/
public class ErrorMessage {
private String errorMessage;
private int errorCode;
public ErrorMessage() {
}
public ErrorMessage(String errorMessage, int errorCode) {
super();
this.errorMessage = errorMessage;
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}