Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #944 from zalando/move-problem-to-controller
Browse files Browse the repository at this point in the history
Move problem to controller
  • Loading branch information
lmontrieux authored Oct 11, 2018
2 parents 6a26342 + e06c995 commit 885a8c8
Show file tree
Hide file tree
Showing 31 changed files with 389 additions and 758 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Upgraded dependencies
- Refactored exceptions
- Moved Problem creation to controller

## [2.8.3] - 2018-08-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.ResponseEntity.status;
import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY;

@RestController
public class CursorOperationsController {
Expand Down Expand Up @@ -140,6 +141,13 @@ public ResponseEntity<?> invalidCursorOperation(final InvalidCursorOperation e,
clientErrorMessage(e.getReason())), request);
}

@ExceptionHandler(CursorConversionException.class)
public ResponseEntity<Problem> handleCursorConversionException(final CursorConversionException exception,
final NativeWebRequest request) {
LOG.error(exception.getMessage(), exception);
return Responses.create(UNPROCESSABLE_ENTITY, exception.getMessage(), request);
}

private String clientErrorMessage(final InvalidCursorOperation.Reason reason) {
switch (reason) {
case TIMELINE_NOT_FOUND: return "Timeline not found. It might happen in case the cursor refers to a " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,10 @@ public ResponseEntity<Problem> handleFeatureNotAllowed(final FeatureNotAvailable
return Responses.create(Problem.valueOf(Response.Status.NOT_IMPLEMENTED, "Feature is disabled"), request);
}

@ExceptionHandler(CursorsAreEmptyException.class)
public ResponseEntity<Problem> handleCursorsUnavailableException(final RuntimeException ex,
final NativeWebRequest request) {
LOG.debug(ex.getMessage(), ex);
return Responses.create(MoreStatus.UNPROCESSABLE_ENTITY, ex.getMessage(), request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,9 +18,13 @@
import org.zalando.nakadi.domain.EventPublishResult;
import org.zalando.nakadi.domain.EventPublishingStatus;
import org.zalando.nakadi.exceptions.runtime.AccessDeniedException;
import org.zalando.nakadi.exceptions.runtime.EnrichmentException;
import org.zalando.nakadi.exceptions.runtime.EventTypeTimeoutException;
import org.zalando.nakadi.exceptions.runtime.InternalNakadiException;
import org.zalando.nakadi.exceptions.runtime.InvalidPartitionKeyFieldsException;
import org.zalando.nakadi.exceptions.runtime.NakadiBaseException;
import org.zalando.nakadi.exceptions.runtime.NoSuchEventTypeException;
import org.zalando.nakadi.exceptions.runtime.PartitioningException;
import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException;
import org.zalando.nakadi.metrics.EventTypeMetricRegistry;
import org.zalando.nakadi.metrics.EventTypeMetrics;
Expand All @@ -40,6 +45,7 @@
import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE;
import static org.springframework.http.ResponseEntity.status;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY;
import static org.zalando.problem.spring.web.advice.Responses.create;

@RestController
Expand Down Expand Up @@ -71,7 +77,9 @@ public EventPublishingController(final EventPublisher publisher,
public ResponseEntity postEvent(@PathVariable final String eventTypeName,
@RequestBody final String eventsAsString,
final NativeWebRequest request,
final Client client) throws AccessDeniedException {
final Client client)
throws AccessDeniedException, EnrichmentException,
PartitioningException, ServiceTemporarilyUnavailableException {
LOG.trace("Received event {} for event type {}", eventsAsString, eventTypeName);
final EventTypeMetrics eventTypeMetrics = eventTypeMetricRegistry.metricsFor(eventTypeName);

Expand All @@ -91,12 +99,22 @@ public ResponseEntity postEvent(@PathVariable final String eventTypeName,
}
}

@ExceptionHandler({EnrichmentException.class,
PartitioningException.class,
InvalidPartitionKeyFieldsException.class})
public ResponseEntity<Problem> handleUnprocessableEntityResponses(final NakadiBaseException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage());
return Responses.create(UNPROCESSABLE_ENTITY, exception.getMessage(), request);
}

private ResponseEntity postEventInternal(final String eventTypeName,
final String eventsAsString,
final NativeWebRequest nativeWebRequest,
final EventTypeMetrics eventTypeMetrics,
final Client client)
throws AccessDeniedException, ServiceTemporarilyUnavailableException {
throws AccessDeniedException, EnrichmentException, PartitioningException,
ServiceTemporarilyUnavailableException {
final long startingNanos = System.nanoTime();
try {
final EventPublishResult result = publisher.publish(eventsAsString, eventTypeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.zalando.nakadi.exceptions.runtime.InconsistentStateException;
import org.zalando.nakadi.exceptions.runtime.InternalNakadiException;
import org.zalando.nakadi.exceptions.runtime.InvalidEventTypeException;
import org.zalando.nakadi.exceptions.runtime.NakadiBaseException;
import org.zalando.nakadi.exceptions.runtime.NakadiRuntimeException;
import org.zalando.nakadi.exceptions.runtime.NoSuchEventTypeException;
import org.zalando.nakadi.exceptions.runtime.NoSuchPartitionStrategyException;
Expand All @@ -41,7 +42,6 @@
import org.zalando.nakadi.service.AdminService;
import org.zalando.nakadi.service.EventTypeService;
import org.zalando.nakadi.service.FeatureToggleService;
import org.zalando.nakadi.service.Result;
import org.zalando.problem.Problem;
import org.zalando.problem.spring.web.advice.Responses;

Expand Down Expand Up @@ -145,12 +145,10 @@ public ResponseEntity<?> update(
}

@RequestMapping(value = "/{name:.+}", method = RequestMethod.GET)
public ResponseEntity<?> get(@PathVariable final String name, final NativeWebRequest request) {
final Result<EventType> result = eventTypeService.get(name);
if (!result.isSuccessful()) {
return Responses.create(result.getProblem(), request);
}
return status(HttpStatus.OK).body(result.getValue());
public ResponseEntity<?> get(@PathVariable final String name, final NativeWebRequest request)
throws NoSuchEventTypeException, InternalNakadiException{
final EventType eventType = eventTypeService.get(name);
return status(HttpStatus.OK).body(eventType);
}

private HttpHeaders generateWarningHeaders(final EventTypeBase eventType) {
Expand All @@ -173,58 +171,34 @@ private HttpHeaders generateWarningHeaders(final EventTypeBase eventType) {
return headers;
}

@ExceptionHandler({UnableProcessException.class,
NoSuchPartitionStrategyException.class,
InvalidEventTypeException.class,
EventTypeOptionsValidationException.class})
public ResponseEntity<Problem> handleUnprocessableEntityResponses(final NakadiBaseException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(UNPROCESSABLE_ENTITY, exception.getMessage(), request);
}

@ExceptionHandler(EventTypeDeletionException.class)
public ResponseEntity<Problem> deletion(final EventTypeDeletionException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), request);
}

@ExceptionHandler(UnableProcessException.class)
public ResponseEntity<Problem> unableProcess(final UnableProcessException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(UNPROCESSABLE_ENTITY, exception.getMessage(), request);
}

@ExceptionHandler(ConflictException.class)
public ResponseEntity<Problem> conflict(final ConflictException exception, final NativeWebRequest request) {
@ExceptionHandler({ConflictException.class, DuplicatedEventTypeNameException.class})
public ResponseEntity<Problem> handleConflictResponses(final NakadiBaseException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(Response.Status.CONFLICT, exception.getMessage(), request);
}

@ExceptionHandler(EventTypeUnavailableException.class)
public ResponseEntity<Problem> eventTypeUnavailable(final EventTypeUnavailableException exception,
final NativeWebRequest request) {
@ExceptionHandler({EventTypeUnavailableException.class, TopicCreationException.class})
public ResponseEntity<Problem> handleServiceUnavailableResponses(final NakadiBaseException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(Response.Status.SERVICE_UNAVAILABLE, exception.getMessage(), request);
}

@ExceptionHandler(NoSuchPartitionStrategyException.class)
public ResponseEntity<Problem> noSuchPartitionStrategyException(final NoSuchPartitionStrategyException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(Problem.valueOf(UNPROCESSABLE_ENTITY, exception.getMessage()), request);
}

@ExceptionHandler(DuplicatedEventTypeNameException.class)
public ResponseEntity<Problem> duplicatedEventTypeNameException(final DuplicatedEventTypeNameException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(Problem.valueOf(Response.Status.CONFLICT, exception.getMessage()), request);
}

@ExceptionHandler(InvalidEventTypeException.class)
public ResponseEntity<Problem> invalidEventTypeException(final InvalidEventTypeException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(Problem.valueOf(UNPROCESSABLE_ENTITY, exception.getMessage()), request);
}

@ExceptionHandler(EventTypeOptionsValidationException.class)
public ResponseEntity<Problem> unableProcess(final EventTypeOptionsValidationException exception,
final NativeWebRequest request) {
LOG.debug(exception.getMessage(), exception);
return Responses.create(UNPROCESSABLE_ENTITY, exception.getMessage(), request);
}
}
Loading

0 comments on commit 885a8c8

Please sign in to comment.