From afd7cad8b8bac4b4fdb711d5754f7fae13de1f95 Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Wed, 30 Aug 2023 09:35:08 +0300 Subject: [PATCH] Set original message and stack trace to a Runtime Exception DTO (#550) In order to extend the predefined error message in the dashboard, pass original message and stack trace to a Runtime Exception DTO --- .../api/core/rest/RuntimeExceptionMapper.java | 17 ++++++++++++++--- .../api/core/rest/shared/dto/ServiceError.java | 7 ++++++- 2 files changed, 20 insertions(+), 4 deletions(-) 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 index d6ec045c4e..49dd5b7ecd 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2021 Red Hat, Inc. + * Copyright (c) 2012-2023 Red Hat, Inc. * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ @@ -11,7 +11,10 @@ */ package org.eclipse.che.api.core.rest; +import static com.google.common.base.Strings.isNullOrEmpty; import static java.lang.String.format; +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.toList; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; @@ -19,6 +22,7 @@ import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.TimeZone; import javax.inject.Singleton; import org.eclipse.che.api.core.rest.shared.dto.ServiceError; @@ -42,11 +46,18 @@ public class RuntimeExceptionMapper implements ExceptionMapper 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); + String message = exception.getMessage(); + final String errorMessage = + isNullOrEmpty(message) + ? format("Internal Server Error occurred, error time: %s", utcTime) + : message; LOG.error(errorMessage, exception); - ServiceError serviceError = DtoFactory.newDto(ServiceError.class).withMessage(errorMessage); + List trace = + stream(exception.getStackTrace()).map(StackTraceElement::toString).collect(toList()); + ServiceError serviceError = + DtoFactory.newDto(ServiceError.class).withMessage(errorMessage).withTrace(trace); return Response.serverError() .entity(DtoFactory.getInstance().toJson(serviceError)) .type(MediaType.APPLICATION_JSON) diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/shared/dto/ServiceError.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/shared/dto/ServiceError.java index e1ff4d6d13..17f5879018 100644 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/shared/dto/ServiceError.java +++ b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/rest/shared/dto/ServiceError.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2018 Red Hat, Inc. + * Copyright (c) 2012-2023 Red Hat, Inc. * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ @@ -11,6 +11,7 @@ */ package org.eclipse.che.api.core.rest.shared.dto; +import java.util.List; import org.eclipse.che.dto.shared.DTO; /** @@ -30,8 +31,12 @@ public interface ServiceError { */ String getMessage(); + List getTrace(); + ServiceError withMessage(String message); + ServiceError withTrace(List trace); + /** * Set error message. *