diff --git a/core/che-core-api-core/pom.xml b/core/che-core-api-core/pom.xml index 50182aa934f..315f518335a 100644 --- a/core/che-core-api-core/pom.xml +++ b/core/che-core-api-core/pom.xml @@ -134,6 +134,16 @@ everrest-assured test + + org.hamcrest + hamcrest-core + test + + + org.hamcrest + hamcrest-library + test + org.mockito mockito-all diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/CoreRestModule.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/CoreRestModule.java index 33d3a18c1b3..ddb8bbb21e9 100644 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/CoreRestModule.java +++ b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/CoreRestModule.java @@ -22,6 +22,7 @@ public class CoreRestModule extends AbstractModule { protected void configure() { bind(CheJsonProvider.class); bind(ApiExceptionMapper.class); + bind(RuntimeExceptionMapper.class); Multibinder.newSetBinder(binder(), Class.class, Names.named("che.json.ignored_classes")); } } diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapper.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapper.java new file mode 100644 index 00000000000..0e78d4c8e00 --- /dev/null +++ b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapper.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2012-2016 Codenvy, S.A. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Codenvy, S.A. - initial API and implementation + *******************************************************************************/ +package org.eclipse.che.api.core.rest; + +import org.eclipse.che.api.core.rest.shared.dto.ServiceError; +import org.eclipse.che.dto.server.DtoFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Singleton; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import static java.lang.String.format; + +/** + * Exception mapper that provide error message with error time. + * + * @author Roman Nikitenko + * @author Sergii Kabashniuk + */ +@Provider +@Singleton +public class RuntimeExceptionMapper implements ExceptionMapper { + private static final Logger LOG = LoggerFactory.getLogger(RuntimeExceptionMapper.class); + + @Override + public Response toResponse(RuntimeException exception) { + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + final String utcTime = dateFormat.format(new Date()); + final String errorMessage = format("Internal Server Error occurred, error time: %s", utcTime); + + LOG.error(errorMessage, exception); + + ServiceError serviceError = DtoFactory.newDto(ServiceError.class).withMessage(errorMessage); + return Response.serverError() + .entity(DtoFactory.getInstance().toJson(serviceError)) + .type(MediaType.APPLICATION_JSON) + .build(); + } +} diff --git a/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapperTest.java b/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapperTest.java new file mode 100644 index 00000000000..0461d096722 --- /dev/null +++ b/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapperTest.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2012-2016 Codenvy, S.A. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Codenvy, S.A. - initial API and implementation + *******************************************************************************/ +package org.eclipse.che.api.core.rest; + +import org.everrest.assured.EverrestJetty; +import org.hamcrest.Matchers; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +import static com.jayway.restassured.RestAssured.expect; + +@Listeners(value = {EverrestJetty.class}) +public class RuntimeExceptionMapperTest { + @Path("/runtime-exception") + public static class RuntimeExceptionService { + @GET + @Path("/re-empty-msg") + public String reWithEmptyMessage() { + throw new NullPointerException(); + } + } + + RuntimeExceptionService service; + + RuntimeExceptionMapper mapper; + + @Test + public void shouldHandleRuntimeException() { + final String expectedErrorMessage = "{\"message\":\"Internal Server Error occurred, error time:"; + + expect() + .statusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) + .body(Matchers.startsWith(expectedErrorMessage)) + .when().get("/runtime-exception/re-empty-msg"); + } +} diff --git a/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java b/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java index a887e8ae358..1505731e255 100644 --- a/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java +++ b/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java @@ -71,6 +71,8 @@ public void close() throws IOException { latch.await(15, TimeUnit.SECONDS); // should not stop here if process killed final long end = System.currentTimeMillis(); + Thread.sleep(200); + // System process sleeps 10 seconds. It is safety to check we done in less then 3 sec. Assert.assertFalse(ProcessUtil.isAlive(p)); Assert.assertTrue((end - start) < 3000, "Fail kill process"); diff --git a/core/che-core-api-core/src/test/resources/logback-test.xml b/core/che-core-api-core/src/test/resources/logback-test.xml index b269ea6aeaa..61b26b8193e 100644 --- a/core/che-core-api-core/src/test/resources/logback-test.xml +++ b/core/che-core-api-core/src/test/resources/logback-test.xml @@ -18,7 +18,7 @@ - +