I am continuing a project that I have not started and I have to follow some guidelines. I'm having problems with exception handling, I put the code and then I ask:
The project consists of several "projects in Java", I will name only those that I think are necessary for my question: gps-EJB, gps-persistence, gps-common, gps-war.
Within the gps-wat project I have this class, it is responsible for collecting the requests that come to the API:
@Path("user")
//@Stateless
@javax.enterprise.context.RequestScoped
public class UserResource {
private static final Logger LOGGER = LoggerFactory.getLogger (Constants.LOGGER_NAME);
@Context
private UriInfo context;
@Inject
private UserEJBLocal userEJB;
/**
* Creates a new instance of LocationResource
*/
public UserResource() {
}
@Path("/new")
@PUT
@Produces("application/json")
@Consumes("application/json")
public Response createUser(DBUser user) {
System.out.println("createuser");
try {
userEJB.createUser(user);
DBUser userinsert = userEJB.getUser (String.valueOf(user.getUserId()));
DBUserResponse response = new DBUserResponse(userinsert);
return Response.ok(response).build();
} catch(Throwable ex) {
LOGGER.error(ex.getMessage(), ex);
//TODO manejar excepciones del ejb, no Throwable; usar codigos de estado http
DBUserResponse response = new DBUserResponse(ErrorCode.E001.toString());
return Response.status(Response.Status.CONFLICT).entity(response).build();
}
//TODO manejar excepciones
}
Mainly the problem I have here, what I want is that when there is an error in the "try" between the catch and I have the correct information the exception.
The corresponding EJB class is the following:
@Stateless(name=UserEJBLocal.EJB_NAME)
public class UserEJB implements UserEJBLocal{
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(UserEJB.class);
@PostConstruct
public void init() {
String description = "You are in UserEJB class";
LOGGER.info(description);
}
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void createUser(DBUser user) {
LOGGER.info("createUser(AN_USER)");
LOGGER.info("The user passed by param has the following id: " + user.getUserId());
UserMapper mapper = new UserMapper();
try {
mapper.createUser(user);
} catch (DatabaseException ex) {
String description = "createUser(AN_USER) call has an ERROR: ";
LOGGER.error(description, ex);
String sqlcod = ex.getSQLState();
if(sqlcod.equals("23505")){
try {
throw new Throwable("error", ex.getCause());
} catch (Throwable ex1) {
Logger.getLogger(UserEJB.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
}
As you can see in this last class I am collecting a BD error (for the case that it is a duplicate entry). I have been debugging, and enters the "catch" of the UserResource class of the corresponding method, but the exception comes to me without information, that is, its empty or null fields: -S
Why is this?