Skip to content

Commit

Permalink
CHE-1232. Add mapper for RuntimeException (#1547)
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <rnikitenko@codenvy.com>
  • Loading branch information
RomanNikitenko authored and Alexander Garagatyi committed Sep 12, 2016
1 parent 06ee352 commit 543eeb4
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 1 deletion.
10 changes: 10 additions & 0 deletions core/che-core-api-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@
<artifactId>everrest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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<RuntimeException> {
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();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion core/che-core-api-core/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</encoder>
</appender>

<root level="DEBUG">
<root level="INFO">
<appender-ref ref="stdout"/>
</root>

Expand Down

0 comments on commit 543eeb4

Please sign in to comment.