Skip to content

Commit

Permalink
Improve error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jun 24, 2020
1 parent b131119 commit 94f0759
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Added protocol package and custom HttpHeaders implementation.**


[b131119887b8d43](https://github.com/noboomu/proteus/commit/b131119887b8d43) Joshua Bauer *2020-06-16 21:39:52*

**Break out XmlModule**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,18 @@ public void failure(Service service)

}, MoreExecutors.directExecutor());

final Thread mainThread = Thread.currentThread();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
shutdown();
} catch (TimeoutException timeout) {
log.error(timeout.getMessage(), timeout);
mainThread.join();
}
catch (InterruptedException ex) {
log.error("Shutdown was interrupted", ex);
}
catch (TimeoutException timeout) {
log.error("Shutdown timed out", timeout);
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package io.sinistral.proteus.server.handlers;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
Expand Down Expand Up @@ -39,8 +40,12 @@ public class ServerDefaultResponseListener implements DefaultResponseListener
@Inject
protected XmlMapper xmlMapper;

@Inject
protected ObjectMapper objectMapper;
protected ObjectMapper objectMapper = new ObjectMapper();

public ServerDefaultResponseListener()
{
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

@Override
public boolean handleDefaultResponse(HttpServerExchange exchange)
Expand All @@ -60,7 +65,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)

if (statusCode >= 400 || throwable != null) {

final Map<String, String> errorMap = new HashMap<>();
final Map<String, Object> errorMap = new HashMap<>();

final String path = exchange.getRelativePath();

Expand All @@ -85,6 +90,20 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)
errorMap.put("path", path);
errorMap.put("code", Integer.toString(exchange.getStatusCode()));

if(throwable.getCause() != null)
{
try
{

errorMap.put("cause",objectMapper.valueToTree(throwable.getCause()));

} catch( Exception e )
{
errorMap.put("cause",throwable.getCause().getMessage());

}
}


if (throwable.getStackTrace() != null && exchange.getStatusCode() >= 500 ) {

Expand All @@ -94,18 +113,9 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)
errorMap.put("className", throwable.getStackTrace()[0].getClassName());
}

StringWriter sw = new StringWriter();

throwable.printStackTrace(new PrintWriter(sw));

String exceptionAsString = sw.toString();
List<String> stringList = Arrays.stream(exceptionAsString.split("\n")).collect(Collectors.toList());
errorMap.put("stackTrace", throwable.getStackTrace());

try {
errorMap.put("stackTrace", objectMapper.writeValueAsString(stringList));
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void configure(Binder binder)
protected void shutDown() throws Exception
{
log.info("Stopping " + this.getClass().getSimpleName());

}

/*
Expand All @@ -54,6 +55,7 @@ protected void shutDown() throws Exception
protected void startUp() throws Exception
{
log.info("Starting " + this.getClass().getSimpleName());

}


Expand Down

0 comments on commit 94f0759

Please sign in to comment.