Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
catch APIException if creating a role fails due to duplicate names
Browse files Browse the repository at this point in the history
 - bubble the error text all the way to the browser so we can display a more helpful error message

 fixes #1578
  • Loading branch information
kroepke committed Aug 28, 2015
1 parent 958051e commit 310b89d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
21 changes: 19 additions & 2 deletions app/controllers/api/RolesApiController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package controllers.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.net.MediaType;
import controllers.AuthenticatedController;
import lib.ApiErrorMessage;
import lib.json.Json;
import lib.security.RestPermissions;
import org.graylog2.rest.models.roles.responses.RoleMembershipResponse;
import org.graylog2.rest.models.roles.responses.RoleResponse;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.models.RolesService;
import play.mvc.Result;

import javax.inject.Inject;
import java.io.IOException;
import java.util.Set;

import static com.google.common.base.Strings.isNullOrEmpty;
import static views.helpers.Permissions.isPermitted;

public class RolesApiController extends AuthenticatedController {

private final ObjectMapper mapper;
private final RolesService rolesService;

@Inject
public RolesApiController(RolesService rolesService) {
public RolesApiController(ObjectMapper mapper, RolesService rolesService) {
this.mapper = mapper;
this.rolesService = rolesService;
}

Expand Down Expand Up @@ -55,7 +61,17 @@ public Result createRole() {
if (isNullOrEmpty(newRole.name()) || newRole.permissions() == null || newRole.permissions().isEmpty()) {
return badRequest("Missing fields");
}
final RoleResponse role = rolesService.create(newRole);
final RoleResponse role;
try {
role = rolesService.create(newRole);
} catch (APIException e) {
try {
final ApiErrorMessage apiErrorMessage = mapper.readValue(e.getResponseBody(), ApiErrorMessage.class);
return badRequest(apiErrorMessage.message);
} catch (IOException e1) {
return internalServerError();
}
}
if (role == null) {
return internalServerError();
}
Expand Down Expand Up @@ -124,4 +140,5 @@ public Result deleteMember(String rolename, String username) {
private static Status jsonOk(Object returnValue) {
return ok(Json.toJsonString(returnValue)).as(MediaType.JSON_UTF_8.toString());
}

}
9 changes: 9 additions & 0 deletions app/lib/ApiErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lib;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

@JsonAutoDetect
public class ApiErrorMessage {
public String type;
public String message;
}
4 changes: 2 additions & 2 deletions javascript/src/stores/users/RolesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export var RolesStore = {
UserNotification.success("Role \"" + newRole.name + "\" was created successfully");
});

promise.fail((jqXHR, textStatus, errorThrown) => {
UserNotification.error("Creating role \"" + role.name + "\" failed with status: " + errorThrown,
promise.fail((jqXHR) => {
UserNotification.error("Creating role \"" + role.name + "\" failed with status: " + jqXHR.responseText,
"Could not create role");
});

Expand Down

0 comments on commit 310b89d

Please sign in to comment.