added exception handling mistakes of project

This commit is contained in:
Daniel Sommer 2019-03-03 22:27:28 +01:00
parent 14e4ad948c
commit 6c7fd837ad
3 changed files with 208 additions and 32 deletions

View File

@ -86,6 +86,24 @@
\end{itemize}
\end{itemize}
\subsection{Annotationen - Details}
\begin{minted}[linenos,breaklines=true]{java}
CascadeType anschauen
@Entity
@Table(name = "user")
public class User extends AbstractEntity {
private static final long serialVersionUID = -7060150053795176748L;
@OneToMany(
mappedBy = "communityAdminUser",
cascade = {CascadeType.PERSIST, CascadeType.REFRESH}, // Persist Operation des User werden auch auf dessen communityAdminUser persistiert - und auch update
orphanRemoval = true //
)
\end{minted}
\section{Konfigurationsdateien}
\subsection{standalone-psoe.xml}
@ -335,3 +353,161 @@ Alle Patterns, die vorkommen praktische Beispiele aus dem Code
Was sind JavaBeans? Wie funktioniert das Konzept? Wie wird es genau implementiert?
NamedBean, TypedBean etc.
\section{Fehler im Projekt}
\subsection{Return null}
Anstatt von Null einfach eine Leere Liste bzw. ein default Objekt (oder new <Object>) zurückgeben
\subsection{Exception nicht gefangen}
\begin{minted}[linenos,breaklines=true]{java}
package at.fhj.swd.psoe.service.impl;
...
@Override
public void removeCommunityByAdmin(CommunityDTO communityDTO) {
Community community = communityDAO.findById(communityDTO.getId());
String errorText;
try {
communityDAO.removeCommunityByAdmin(community);
} catch (DaoException e) {
errorText = "Error removing community";
logger.error(errorText, e);
throw new ServiceException(errorText);
}
}
\end{minted}
\subsection{Destructive Wrapping}
\begin{minted}[linenos,breaklines=true]{java}
package at.fhj.swd.psoe.service.impl;
...
@Override
public CommunityDTO updateCommunityEnabled(String adminUserId, String communityName, boolean isEnabled) {
String errorText = "";
try {
boolean hasPermission = false;
User adminUser = userDao.getByUserId(adminUserId);
Community community = communityDAO.getByName(communityName);
for (Role r : adminUser.getRoles()) {
if (r.getname().equals("ADMIN") || r.getname().equals("PORTALADMIN")) {
hasPermission = true;
}
}
if (hasPermission || adminUser.getUserId().equals(community.getCommunityAdminUser().getUserId())) {
community.setIsEnabled(isEnabled);
communityDAO.update(community);
} else {
errorText = "No Permission to update community";
throw new AuthenticationException(errorText);
}
return CommunityMapper.toDTO(community);
} catch (DaoException e) {
errorText = "Error updating community enabled";
throw new ServiceException(errorText);
} catch (AuthenticationException e) {
throw new ServiceException(errorText);
} catch (Throwable e) {
errorText = "Unknown error updating community enabled";
throw new ServiceException(errorText);
}
}
\end{minted}
\subsection{Logger mit falschem Parameter}
\begin{minted}[linenos,breaklines=true]{java}
package at.fhj.swd.psoe.service.impl;
...
@Stateless
public class DepartmentHierarchyServiceImpl implements DepartmentHierarchyService, Serializable {
private static final long serialVersionUID = -2467949382018996094L;
private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
...
\end{minted}
\subsection{Fehlendes Exception Handling}
\begin{minted}[linenos,breaklines=true]{java}
package at.fhj.swd.psoe.service.impl;
...
@Local(MessageService.class)
@Remote(MessageServiceRemote.class)
@Stateless
public class MessageServiceImpl implements MessageService, MessageServiceRemote, Serializable {
...
@Override
public MessageDTO getByMessageId(long id) {
Message message = messageDAO.getById(id);
if (message == null) {
return null;
}
return MessageMapper.toDTO(message);
}
@Override
public MessageDTO updateByAdmin(long id, String content, Date changed) {
Message message = messageDAO.getById(id);
message.setContent(content);
message.setEditedByAdmin(changed);
return MessageMapper.toDTO(messageDAO.update(message));
}
@Override
public MessageDTO updateByUser(long messageId, String content, Date changed) {
Message message = messageDAO.getById(messageId);
message.setContent(content);
message.setEditedByUser(changed);
return MessageMapper.toDTO(messageDAO.update(message));
}
\end{minted}
\subsection{Exception werfen und gleich wieder fangen}
\begin{minted}[linenos,breaklines=true]{java}
...
public class MessageServiceImpl implements MessageService, MessageServiceRemote, Serializable {
private static final long serialVersionUID = 6768291437557855130L;
...
// nicht optimal, da die IllegalArgumentException gleich wieder gefangen wird
// überdies wird alles andere nicht gefangen
@Override
public void deleteMessage(long id) {
try {
Message message = messageDAO.getById(id);
if (message == null) {
throw new IllegalArgumentException("Message cannot be empty");
}
messageDAO.delete(message);
logger.info("Message deleted successfully");
}
catch (IllegalArgumentException ex) {
String errorMsg = "Could not delete the message (illegal argument)";
logger.error(errorMsg, ex);
throw new ServiceException(errorMsg);
}
}
//---------- besser wäre
package at.fhj.swd.psoe.service.impl;
...
// erst loggen, dass man in die Methode gekommen ist
// wenn userDTO null ist wird IllegalArgument geworfen und das außerhalb des try catch blocks
// erst wird die DaoException gefangen und anschließend alle anderen
// Stacktrace wird geloggt und jeweils die ServiceException weitergegeben
@Override
public void saveUser(UserDTO userDTO) {
logger.debug("UserService saveUser() called with parameter: '{}'", userDTO);
if (userDTO == null) {
throw new IllegalArgumentException("userDTO is null");
}
try {
User user = (userDTO.getId() == null) ? new User() : userDao.getById(userDTO.getId());
userDao.update(UserMapper.toEntity(userDTO, user));
} catch (DaoException e) {
logger.error("Error saving user", e);
throw new ServiceException("Error saving user");
} catch (Throwable e) {
logger.error("Unknown error saving user", e);
throw new ServiceException("Unknown error saving user");
}
}
\end{minted}

BIN
pse.pdf

Binary file not shown.

View File

@ -9,9 +9,9 @@
\usepackage[hidelinks]{hyperref}
\usepackage{multicol}
\usepackage{graphicx}
%\usepackage{minted}
\usepackage{minted}
\usepackage[outputdir=../auxil]{minted}
% \usepackage[outputdir=../auxil]{minted}
\setminted{fontsize=\small}
\usepackage{caption}