-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-1232. Add mapper for RuntimeException (#1547)
Signed-off-by: Roman Nikitenko <rnikitenko@codenvy.com>
- Loading branch information
1 parent
06ee352
commit 543eeb4
Showing
6 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...core-api-core/src/test/java/org/eclipse/che/api/core/rest/RuntimeExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters