Skip to content

Commit

Permalink
Merge pull request #76 from zalando/feature/logging
Browse files Browse the repository at this point in the history
Added logging support for exceptions
  • Loading branch information
whiskeysierra authored Nov 4, 2016
2 parents 4a4dc90 + 32ccc31 commit c4d8326
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gag.annotation.remark.Hack;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -22,14 +24,14 @@
import org.zalando.problem.spring.web.advice.routing.RoutingAdviceTrait;
import org.zalando.problem.spring.web.advice.validation.ValidationAdviceTrait;

import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.StatusType;
import java.util.List;
import java.util.Optional;

import static com.google.common.base.MoreObjects.firstNonNull;
import static java.util.Arrays.asList;
import static javax.servlet.RequestDispatcher.ERROR_EXCEPTION;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
Expand Down Expand Up @@ -66,6 +68,8 @@
*/
public interface AdviceTrait {

Logger LOG = LoggerFactory.getLogger(AdviceTrait.class);

default ResponseEntity<Problem> create(final StatusType status, final Throwable throwable,
final NativeWebRequest request) {
return create(status, throwable, request, new HttpHeaders());
Expand Down Expand Up @@ -126,7 +130,9 @@ default ResponseEntity<Problem> create(final Throwable throwable, final Problem
final NativeWebRequest request, final HttpHeaders headers) {

final HttpStatus status = HttpStatus.valueOf(firstNonNull(
problem.getStatus(), INTERNAL_SERVER_ERROR).getStatusCode());
problem.getStatus(), Status.INTERNAL_SERVER_ERROR).getStatusCode());

log(throwable, problem, request, status);

if (status == HttpStatus.INTERNAL_SERVER_ERROR) {
request.setAttribute(ERROR_EXCEPTION, throwable, SCOPE_REQUEST);
Expand All @@ -140,6 +146,16 @@ default ResponseEntity<Problem> create(final Throwable throwable, final Problem
.orElseGet(() -> fallback(throwable, problem, request, headers)));
}

default void log(
@SuppressWarnings("UnusedParameters") final Throwable throwable,
@SuppressWarnings("UnusedParameters") final Problem problem,
@SuppressWarnings("UnusedParameters") final NativeWebRequest request,
final HttpStatus status) {
if (status == HttpStatus.INTERNAL_SERVER_ERROR) {
LOG.error("Internal Server Error", throwable);
}
}

default ResponseEntity<Problem> fallback(final Throwable throwable, final Problem problem,
final NativeWebRequest request, final HttpHeaders headers) {
return ResponseEntity.status(NOT_ACCEPTABLE).body(null);
Expand Down

0 comments on commit c4d8326

Please sign in to comment.