-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server] refactor exceptions handling (#1749)
- Loading branch information
1 parent
541fb96
commit a75697c
Showing
15 changed files
with
156 additions
and
216 deletions.
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
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
132 changes: 0 additions & 132 deletions
132
thirdeye-server/src/main/java/ai/startree/thirdeye/exception/ExceptionHandler.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
thirdeye-server/src/main/java/ai/startree/thirdeye/exception/ExceptionUtils.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,46 @@ | ||
/* | ||
* Copyright 2024 StarTree Inc | ||
* | ||
* Licensed under the StarTree Community License (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.startree.ai/legal/startree-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OF ANY KIND, | ||
* either express or implied. | ||
* See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package ai.startree.thirdeye.exception; | ||
|
||
import ai.startree.thirdeye.spi.api.ExceptionApi; | ||
import ai.startree.thirdeye.spi.api.StackTraceElementApi; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ExceptionUtils { | ||
|
||
public static ExceptionApi toExceptionApi(final Throwable t) { | ||
if (t == null) { | ||
return null; | ||
} | ||
|
||
final List<StackTraceElementApi> stackTrace = Arrays.stream(t.getStackTrace()) | ||
.map(ExceptionUtils::stackTraceElementApi) | ||
.collect(Collectors.toList()); | ||
|
||
return new ExceptionApi() | ||
.setMessage(t.getMessage()) | ||
.setCause(toExceptionApi(t.getCause())) | ||
.setStackTrace(stackTrace); | ||
} | ||
|
||
private static StackTraceElementApi stackTraceElementApi(final StackTraceElement ste) { | ||
return new StackTraceElementApi() | ||
.setClassName(ste.getClassName()) | ||
.setMethodName(ste.getMethodName()) | ||
.setFileName(ste.getFileName()) | ||
.setLineNumber(ste.getLineNumber()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
thirdeye-server/src/main/java/ai/startree/thirdeye/exception/GenericExceptionMapper.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,70 @@ | ||
/* | ||
* Copyright 2024 StarTree Inc | ||
* | ||
* Licensed under the StarTree Community License (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.startree.ai/legal/startree-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OF ANY KIND, | ||
* either express or implied. | ||
* See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package ai.startree.thirdeye.exception; | ||
|
||
import static ai.startree.thirdeye.exception.ExceptionUtils.toExceptionApi; | ||
|
||
import ai.startree.thirdeye.spi.ThirdEyeStatus; | ||
import ai.startree.thirdeye.spi.api.StatusApi; | ||
import ai.startree.thirdeye.spi.api.StatusListApi; | ||
import io.dropwizard.jersey.errors.LoggingExceptionMapper; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.Provider; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This mapper has lesser priority than all others. | ||
* Most Exceptions thrown implemented in this code base should be wrapped inside a ThirdEyeException | ||
* and will benefit from the ThirdEyeExceptionMapper | ||
* Complete this class only for exceptions that are hard to wrap with a ThirdEyeException. | ||
*/ | ||
@Provider | ||
public class GenericExceptionMapper extends LoggingExceptionMapper<Throwable> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(GenericExceptionMapper.class); | ||
|
||
public GenericExceptionMapper() { | ||
} | ||
|
||
@Override | ||
public Response toResponse(final Throwable exception) { | ||
final ThirdEyeStatus status = statusFor(exception); | ||
LOG.debug( | ||
"Request failed because of a {}. Returning error code {}", | ||
exception.getClass().getSimpleName(), | ||
status.getRecommendedStatusCode()); | ||
final StatusApi statusApi = new StatusApi() | ||
.setCode(status) | ||
.setMsg(exception.getMessage()) | ||
// TODO cyril put this behind a boolean - in some environments we should not return this | ||
.setException(toExceptionApi(exception)); | ||
final StatusListApi statusList = new StatusListApi().setList(List.of(statusApi)); | ||
return Response.status(status.getRecommendedStatusCode()) | ||
.type(MediaType.APPLICATION_JSON_TYPE) | ||
.entity(statusList) | ||
.build(); | ||
} | ||
|
||
private static ThirdEyeStatus statusFor(final Throwable exception) { | ||
if (exception instanceof TimeoutException) { | ||
return ThirdEyeStatus.ERR_TIMEOUT; | ||
} else { | ||
return ThirdEyeStatus.ERR_UNKNOWN; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.