1

add error handling (frontend) + exception changes

Co-authored-by: Christoph <tobi@urpost.de>
Co-authored-by: Mahgs <maxoerter@gmx.de>
This commit is contained in:
Christoph
2020-03-18 16:26:11 +01:00
parent 0ade9319fb
commit df7803f83d
20 changed files with 140 additions and 86 deletions

View File

@ -2,19 +2,16 @@ package mops.gruppen2.controller;
import mops.gruppen2.config.Gruppen2Config;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.GroupNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.*;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
@ -125,7 +122,7 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@GetMapping("/details")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "id") Long id) throws EventException, ResponseStatusException {
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "id") Long id) throws ResponseStatusException, EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(id);
Account account = keyCloakService.createAccountFromPrincipal(token);
@ -135,7 +132,7 @@ public class Gruppen2Controller {
model.addAttribute("role", group.getRoles().get(user.getUser_id()));
return "detailsMember";
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Group not found");
throw new GroupNotFoundException();
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@ -156,7 +153,7 @@ public class Gruppen2Controller {
model.addAttribute("group", group);
return "detailsNoMember";
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Group not found");
throw new GroupNotFoundException();
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@ -168,10 +165,10 @@ public class Gruppen2Controller {
model.addAttribute("group", group);
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Group not found");
throw new GroupNotFoundException();
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/leaveGroup")
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam(value = "group_id") Long id) {
Account account = keyCloakService.createAccountFromPrincipal(token);
@ -181,6 +178,6 @@ public class Gruppen2Controller {
@GetMapping("*")
public String defaultLink() {
return "errorRenameLater";
return "error";
}
}

View File

@ -1,10 +1,13 @@
package mops.gruppen2.domain.Exceptions;
public class EventException extends Exception {
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
public class EventException extends ResponseStatusException {
private String msg;
public EventException(String msg) {
this.msg = msg;
public EventException(String msg, HttpStatus status) {
super(status, msg);
}
@Override

View File

@ -0,0 +1,13 @@
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);
}
}

View File

@ -0,0 +1,13 @@
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);
}
}

View File

@ -1,7 +1,13 @@
package mops.gruppen2.domain.Exceptions;
import org.springframework.http.HttpStatus;
public class UserAlreadyExistsException extends EventException {
public UserAlreadyExistsException(String msg){
super(msg);
public UserAlreadyExistsException(String msg) {
super(msg, HttpStatus.INTERNAL_SERVER_ERROR);
}
public UserAlreadyExistsException() {
super("Der User existiert bereits.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@ -1,7 +1,13 @@
package mops.gruppen2.domain.Exceptions;
public class UserNotFoundException extends EventException{
public UserNotFoundException(String msg){
super(msg);
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);
}
}

View File

@ -1,12 +0,0 @@
package mops.gruppen2.domain;
import lombok.Value;
// @ApiModelProperty
@Value
public class ProductSwaggerExample {
// @ApiModelProperty
String name;
String description;
}

View File

@ -27,11 +27,12 @@ public class AddUserEvent extends Event {
this.email = email;
}
public void apply(Group group) throws EventException{
@Override
public void applyEvent(Group group) throws EventException {
User user = new User(this.user_id, this.givenname, this.familyname, this.email);
if (group.getMembers().contains(user)){
throw new UserAlreadyExistsException("Der User existiert bereits");
if (group.getMembers().contains(user)) {
throw new UserAlreadyExistsException();
}
group.getMembers().add(user);

View File

@ -15,8 +15,6 @@ public class CreateGroupEvent extends Event {
private Long groupParent;
private GroupType groupType;
public CreateGroupEvent(Long group_id, String user_id, Long parent, GroupType type, Visibility visibility) {
super(group_id, user_id);
this.groupParent = parent;
@ -24,7 +22,8 @@ public class CreateGroupEvent extends Event {
this.groupVisibility = visibility;
}
public void apply(Group group) {
@Override
public void applyEvent(Group group) {
group.setId(this.group_id);
group.setParent(this.groupParent);
group.setType(this.groupType);

View File

@ -13,7 +13,7 @@ public class DeleteGroupEvent extends Event {
}
@Override
public void apply(Group group) {
public void applyEvent(Group group) {
group.getRoles().clear();
group.getMembers().clear();
group.setTitle(null);

View File

@ -1,11 +1,11 @@
package mops.gruppen2.domain.event;
import lombok.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Group;
/**
* Entfernt ein einzelnes Mitglied einer Gruppe.
@ -17,7 +17,8 @@ public class DeleteUserEvent extends Event {
super(group_id, user_id);
}
public void apply(Group group) throws EventException {
@Override
public void applyEvent(Group group) throws EventException {
for (User user : group.getMembers()) {
if (user.getUser_id().equals(this.user_id)) {
group.getMembers().remove(user);

View File

@ -31,7 +31,22 @@ public class Event {
Long group_id;
String user_id;
public void apply(Group group) throws EventException {
checkGroupIdMatch(group.getId());
applyEvent(group);
}
protected void applyEvent(Group group) throws EventException {
}
private void checkGroupIdMatch(Long group_id) {
if (this.group_id.equals(group_id)) {
return;
}
//throw new GroupIdMismatchException(this.getClass().toString());
System.out.println(group_id);
System.out.println(this.group_id);
}
}

View File

@ -19,7 +19,8 @@ public class UpdateGroupDescriptionEvent extends Event {
this.newGroupDescription = newGroupDescription;
}
public void apply(Group group) {
@Override
public void applyEvent(Group group) {
group.setDescription(this.newGroupDescription);
}
}

View File

@ -19,7 +19,8 @@ public class UpdateGroupTitleEvent extends Event {
this.newGroupTitle = newGroupTitle;
}
public void apply(Group group) {
@Override
public void applyEvent(Group group) {
group.setTitle(this.newGroupTitle);
}

View File

@ -1,13 +1,11 @@
package mops.gruppen2.domain.event;
import lombok.*;
import mops.gruppen2.domain.Exceptions.EventException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import java.util.Optional;
/**
* Aktualisiert die Gruppenrolle eines Teilnehmers.
@ -18,15 +16,16 @@ import java.util.Optional;
public class UpdateRoleEvent extends Event {
Role newRole;
public UpdateRoleEvent(Long group_id, String user_id, Role newRole) {
super(group_id, user_id);
this.newRole = newRole;
}
public void apply(Group group) throws UserNotFoundException{
if (!group.getRoles().containsKey(user_id)){
throw new UserNotFoundException("Der User wurde nicht gefunden");
@Override
public void applyEvent(Group group) throws UserNotFoundException {
if (!group.getRoles().containsKey(user_id)) {
throw new UserNotFoundException();
}
group.getRoles().put(this.user_id, this.newRole);
}

View File

@ -6,11 +6,7 @@ import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticatio
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
import org.keycloak.representations.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;

View File

@ -1,6 +1,7 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.GroupNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.event.Event;
@ -39,7 +40,12 @@ public class UserService {
public Group getGroupById(Long group_id) throws EventException {
List<Long> group_ids = new ArrayList<>();
group_ids.add(group_id);
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events).get(0);
try {
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events).get(0);
} catch (IndexOutOfBoundsException e) {
throw new GroupNotFoundException("Gruppe nicht gefunden");
}
}
}