Exception rework: clearer message + origin
Co-authored-by: [Mahgs] <maxoerter@gmx.de> Co-authored-by: Christoph <tobi@urpost.de>
This commit is contained in:
@ -3,15 +3,10 @@ package mops.gruppen2.domain.Exceptions;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
public class EventException extends ResponseStatusException {
|
||||
private String msg;
|
||||
public abstract class EventException extends ResponseStatusException {
|
||||
|
||||
public EventException(String msg, HttpStatus status) {
|
||||
super(status, msg);
|
||||
public EventException(HttpStatus status, String msg, String info) {
|
||||
super(status, msg + " (" + info + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,11 +3,8 @@ package mops.gruppen2.domain.Exceptions;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class GroupIdMismatchException extends EventException {
|
||||
public GroupIdMismatchException(String msg) {
|
||||
super("Falsche Gruppe für Event." + " (" + msg + ")", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
public GroupIdMismatchException() {
|
||||
super("", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
public GroupIdMismatchException(String info) {
|
||||
super(HttpStatus.INTERNAL_SERVER_ERROR, "Falsche Gruppe für Event.", info);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,11 +3,8 @@ package mops.gruppen2.domain.Exceptions;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class GroupNotFoundException extends EventException {
|
||||
public GroupNotFoundException(String msg) {
|
||||
super(msg, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
public GroupNotFoundException() {
|
||||
super("Gruppe nicht gefunden.", HttpStatus.NOT_FOUND);
|
||||
public GroupNotFoundException(String info) {
|
||||
super(HttpStatus.NOT_FOUND, "Gruppe wurde nicht gefunden.", info);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
package mops.gruppen2.domain.Exceptions;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class NoValueException extends EventException {
|
||||
|
||||
public NoValueException(String info) {
|
||||
super(HttpStatus.NO_CONTENT, "Eine Information fehlt.", info);
|
||||
}
|
||||
}
|
||||
@ -3,11 +3,8 @@ package mops.gruppen2.domain.Exceptions;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class UserAlreadyExistsException extends EventException {
|
||||
public UserAlreadyExistsException(String msg) {
|
||||
super(msg, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
public UserAlreadyExistsException() {
|
||||
super("Der User existiert bereits.", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
public UserAlreadyExistsException(String info) {
|
||||
super(HttpStatus.INTERNAL_SERVER_ERROR, "Der User existiert bereits.", info);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,11 +3,8 @@ package mops.gruppen2.domain.Exceptions;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class UserNotFoundException extends EventException {
|
||||
public UserNotFoundException(String msg) {
|
||||
super(msg, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
public UserNotFoundException() {
|
||||
super("Der User wurde nicht gefunden.", HttpStatus.NOT_FOUND);
|
||||
public UserNotFoundException(String info) {
|
||||
super(HttpStatus.NOT_FOUND, "Der User wurde nicht gefunden.", info);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,11 +4,11 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
|
||||
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
|
||||
import mops.gruppen2.domain.event.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Repräsentiert den aggregierten Zustand einer Gruppe.
|
||||
@ -18,7 +18,7 @@ import java.util.*;
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class Group {
|
||||
private long id;
|
||||
private Long id;
|
||||
private String title;
|
||||
private String description;
|
||||
private final List<User> members;
|
||||
|
||||
@ -16,9 +16,10 @@ import mops.gruppen2.domain.User;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AddUserEvent extends Event {
|
||||
String givenname;
|
||||
String familyname;
|
||||
String email;
|
||||
|
||||
private String givenname;
|
||||
private String familyname;
|
||||
private String email;
|
||||
|
||||
public AddUserEvent(Long group_id, String user_id, String givenname, String familyname, String email) {
|
||||
super(group_id, user_id);
|
||||
@ -32,7 +33,7 @@ public class AddUserEvent extends Event {
|
||||
User user = new User(this.user_id, this.givenname, this.familyname, this.email);
|
||||
|
||||
if (group.getMembers().contains(user)) {
|
||||
throw new UserAlreadyExistsException();
|
||||
throw new UserAlreadyExistsException(this.getClass().toString());
|
||||
}
|
||||
|
||||
group.getMembers().add(user);
|
||||
|
||||
@ -11,6 +11,7 @@ import mops.gruppen2.domain.Visibility;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class CreateGroupEvent extends Event {
|
||||
|
||||
private Visibility groupVisibility;
|
||||
private Long groupParent;
|
||||
private GroupType groupType;
|
||||
|
||||
@ -13,6 +13,7 @@ import mops.gruppen2.domain.User;
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public class DeleteUserEvent extends Event {
|
||||
|
||||
public DeleteUserEvent(Long group_id, String user_id) {
|
||||
super(group_id, user_id);
|
||||
}
|
||||
@ -26,6 +27,6 @@ public class DeleteUserEvent extends Event {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new UserNotFoundException("Der User existiert nicht");
|
||||
throw new UserNotFoundException(this.getClass().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,12 +7,9 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import mops.gruppen2.domain.Exceptions.EventException;
|
||||
import mops.gruppen2.domain.Exceptions.GroupIdMismatchException;
|
||||
import mops.gruppen2.domain.Group;
|
||||
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonTypeInfo(
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
@ -26,27 +23,27 @@ import mops.gruppen2.domain.Group;
|
||||
@JsonSubTypes.Type(value = UpdateGroupTitleEvent.class, name = "UpdateGroupTitleEvent"),
|
||||
@JsonSubTypes.Type(value = UpdateRoleEvent.class, name = "UpdateRoleEvent"),
|
||||
})
|
||||
@Getter
|
||||
@Setter
|
||||
public class Event {
|
||||
Long group_id;
|
||||
String user_id;
|
||||
@NoArgsConstructor // Needed by Lombok in Subclasses
|
||||
@AllArgsConstructor
|
||||
public abstract class Event {
|
||||
|
||||
protected Long group_id;
|
||||
protected String user_id;
|
||||
|
||||
public void apply(Group group) throws EventException {
|
||||
checkGroupIdMatch(group.getId());
|
||||
applyEvent(group);
|
||||
}
|
||||
|
||||
protected void applyEvent(Group group) throws EventException {
|
||||
|
||||
}
|
||||
protected abstract void applyEvent(Group group) throws EventException;
|
||||
|
||||
private void checkGroupIdMatch(Long group_id) {
|
||||
if (this.group_id.equals(group_id)) {
|
||||
if (group_id == null || this.group_id.equals(group_id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//throw new GroupIdMismatchException(this.getClass().toString());
|
||||
System.out.println(group_id);
|
||||
System.out.println(this.group_id);
|
||||
throw new GroupIdMismatchException(this.getClass().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package mops.gruppen2.domain.event;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import mops.gruppen2.domain.Exceptions.NoValueException;
|
||||
import mops.gruppen2.domain.Group;
|
||||
|
||||
/**
|
||||
@ -12,7 +13,8 @@ import mops.gruppen2.domain.Group;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UpdateGroupDescriptionEvent extends Event {
|
||||
String newGroupDescription;
|
||||
|
||||
private String newGroupDescription;
|
||||
|
||||
public UpdateGroupDescriptionEvent(Long group_id, String user_id, String newGroupDescription) {
|
||||
super(group_id, user_id);
|
||||
@ -21,6 +23,10 @@ public class UpdateGroupDescriptionEvent extends Event {
|
||||
|
||||
@Override
|
||||
public void applyEvent(Group group) {
|
||||
if (this.newGroupDescription.isEmpty()) {
|
||||
throw new NoValueException(this.getClass().toString());
|
||||
}
|
||||
|
||||
group.setDescription(this.newGroupDescription);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package mops.gruppen2.domain.event;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import mops.gruppen2.domain.Exceptions.NoValueException;
|
||||
import mops.gruppen2.domain.Group;
|
||||
|
||||
/**
|
||||
@ -12,7 +13,8 @@ import mops.gruppen2.domain.Group;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UpdateGroupTitleEvent extends Event {
|
||||
String newGroupTitle;
|
||||
|
||||
private String newGroupTitle;
|
||||
|
||||
public UpdateGroupTitleEvent(Long group_id, String user_id, String newGroupTitle) {
|
||||
super(group_id, user_id);
|
||||
@ -21,6 +23,10 @@ public class UpdateGroupTitleEvent extends Event {
|
||||
|
||||
@Override
|
||||
public void applyEvent(Group group) {
|
||||
if (this.getNewGroupTitle().isEmpty()) {
|
||||
throw new NoValueException(this.getClass().toString());
|
||||
}
|
||||
|
||||
group.setTitle(this.newGroupTitle);
|
||||
}
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ import mops.gruppen2.domain.Role;
|
||||
@NoArgsConstructor
|
||||
public class UpdateRoleEvent extends Event {
|
||||
|
||||
Role newRole;
|
||||
private Role newRole;
|
||||
|
||||
public UpdateRoleEvent(Long group_id, String user_id, Role newRole) {
|
||||
super(group_id, user_id);
|
||||
@ -24,10 +24,11 @@ public class UpdateRoleEvent extends Event {
|
||||
|
||||
@Override
|
||||
public void applyEvent(Group group) throws UserNotFoundException {
|
||||
if (!group.getRoles().containsKey(user_id)) {
|
||||
throw new UserNotFoundException();
|
||||
if (group.getRoles().containsKey(user_id)) {
|
||||
group.getRoles().put(this.user_id, this.newRole);
|
||||
}
|
||||
group.getRoles().put(this.user_id, this.newRole);
|
||||
|
||||
throw new UserNotFoundException(this.getClass().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user