Skip to content

Commit

Permalink
Catching invalid namespace error and converting to 400 Bad Request in…
Browse files Browse the repository at this point in the history
…stead of 500 Internal Server Error
  • Loading branch information
escowles committed Apr 19, 2014
1 parent 1025757 commit c790eab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Expand Up @@ -18,6 +18,8 @@

import static com.google.common.base.Throwables.getStackTraceAsString;
import static javax.ws.rs.core.Response.serverError;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.RepositoryException;
Expand All @@ -43,6 +45,10 @@ public Response toResponse(final RepositoryException e) {

LOGGER.warn("Caught repository exception: {}", e);

if ( e.getMessage().matches("Error converting \".+\" from String to a Name")) {
return status(BAD_REQUEST).entity(e.getMessage()).build();
}

return serverError().entity(
showStackTrace ? getStackTraceAsString(e) : null).build();
}
Expand Down
@@ -0,0 +1,56 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.http.commons.exceptionhandlers;

import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.ws.rs.core.Response;

import javax.jcr.RepositoryException;

import org.junit.Before;
import org.junit.Test;

public class RepositoryExceptionMapperTest {

private RepositoryExceptionMapper testObj;

@Before
public void setUp() {
testObj = new RepositoryExceptionMapper();
}

@Test
public void testInvalidNamespace() {
RepositoryException input = new RepositoryException("Error converting \"abc:123\" from String to a Name");
Response actual = testObj.toResponse(input);
assertEquals(BAD_REQUEST.getStatusCode(), actual.getStatus());
assertEquals(actual.getEntity(), input.getMessage());
}

@Test
public void testToResponse() {
RepositoryException input = new RepositoryException("An error occurred");
Response actual = testObj.toResponse(input);
assertEquals(INTERNAL_SERVER_ERROR.getStatusCode(), actual.getStatus());
assertNotNull(actual.getEntity());
}
}

0 comments on commit c790eab

Please sign in to comment.