Skip to content

Commit

Permalink
#30 updating readme, adding stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Aug 9, 2024
1 parent 2e895c5 commit c0f1335
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/main/java/io/kerosenelabs/atc/server/CentralRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
import io.kerosenelabs.kindling.constant.HttpStatus;
import io.kerosenelabs.kindling.exception.KindlingException;
import io.kerosenelabs.kindling.handler.RequestHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.ObjectInputFilter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class CentralRequestHandler extends RequestHandler {
private final ConfigurationHandler configurationHandler;
private final List<Configuration.Service> services;
private final ObjectMapper objectMapper = new ObjectMapper();

public CentralRequestHandler() throws IOException {
configurationHandler = ConfigurationHandler.getInstance();
services = configurationHandler.getConfiguration().getServices();;
services = configurationHandler.getConfiguration().getServices();
;
log.info("Central request handler initialized");
}

Expand Down Expand Up @@ -94,31 +95,27 @@ public boolean accepts(HttpRequest httpRequest) throws KindlingException {
return true;
}

@SneakyThrows
@Override
public HttpResponse handleError(Throwable t) {
ObjectMapper objectMapper = new ObjectMapper();

// if a kindling exception was thrown OR we're in allowExceptionsInErrorResponse mode
if (t instanceof KindlingException || System.getProperty("atc.http.allowExceptionsInErrorResponse").equals("true")) {
try {
return new HttpResponse.Builder()
.status(HttpStatus.BAD_REQUEST)
.headers(new HashMap<>() {
{
put("Content-Type", "application/json");
}
})
.content(objectMapper.writeValueAsString(
new ExceptionErrorResponse(
t.getMessage(),
Arrays.stream(t.getStackTrace())
.map(Object::toString)
.toList())
)
)
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return new HttpResponse.Builder()
.status(HttpStatus.BAD_REQUEST)
.headers(new HashMap<>() {
{
put("Content-Type", "application/json");
}
})
.content(objectMapper.writeValueAsString(
new ExceptionErrorResponse(
t.getMessage(),
Arrays.stream(t.getStackTrace())
.map(Object::toString)
.toList())
)
)
.build();
}

return new HttpResponse.Builder()
Expand Down

0 comments on commit c0f1335

Please sign in to comment.