Skip to content

Commit

Permalink
Name exception uniformaly - instead of Wrong* to Invalid*
Browse files Browse the repository at this point in the history
  • Loading branch information
antban committed Jul 15, 2021
1 parent 1a47a7c commit 9788f21
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.zalando.nakadi.exceptions.runtime.NoStreamingSlotsAvailable;
import org.zalando.nakadi.exceptions.runtime.NoSuchSubscriptionException;
import org.zalando.nakadi.exceptions.runtime.SubscriptionPartitionConflictException;
import org.zalando.nakadi.exceptions.runtime.WrongStreamParametersException;
import org.zalando.nakadi.exceptions.runtime.InvalidStreamParametersException;
import org.zalando.nakadi.repository.db.SubscriptionDbRepository;
import org.zalando.nakadi.security.Client;
import org.zalando.nakadi.service.ClosedConnectionsCrutch;
Expand Down Expand Up @@ -113,7 +113,7 @@ private void assignExceptionProblem() {
(ex) -> Problem.valueOf(FORBIDDEN, ((AccessDeniedException) ex).explain()));
this.exceptionProblem.put(SubscriptionPartitionConflictException.class,
(ex) -> Problem.valueOf(CONFLICT, ex.getMessage()));
this.exceptionProblem.put(WrongStreamParametersException.class,
this.exceptionProblem.put(InvalidStreamParametersException.class,
(ex) -> Problem.valueOf(UNPROCESSABLE_ENTITY, ex.getMessage()));
this.exceptionProblem.put(NoSuchSubscriptionException.class,
(ex) -> Problem.valueOf(NOT_FOUND, ex.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.NativeWebRequest;
import org.zalando.nakadi.controller.SubscriptionStreamController;
import org.zalando.nakadi.exceptions.runtime.InvalidStreamParametersException;
import org.zalando.nakadi.exceptions.runtime.NoStreamingSlotsAvailable;
import org.zalando.nakadi.exceptions.runtime.WrongStreamParametersException;
import org.zalando.problem.Problem;
import org.zalando.problem.spring.web.advice.AdviceTrait;

Expand All @@ -19,9 +19,10 @@
@ControllerAdvice(assignableTypes = SubscriptionStreamController.class)
public class SubscriptionStreamExceptionHandler implements AdviceTrait {

@ExceptionHandler(WrongStreamParametersException.class)
public ResponseEntity<Problem> handleWrongStreamParametersException(final WrongStreamParametersException exception,
final NativeWebRequest request) {
@ExceptionHandler(InvalidStreamParametersException.class)
public ResponseEntity<Problem> handleWrongStreamParametersException(
final InvalidStreamParametersException exception,
final NativeWebRequest request) {
AdviceTrait.LOG.debug(exception.getMessage());
return create(Problem.valueOf(UNPROCESSABLE_ENTITY, exception.getMessage()), request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.zalando.nakadi.service.subscription;

import org.zalando.nakadi.domain.EventTypePartition;
import org.zalando.nakadi.exceptions.runtime.WrongStreamParametersException;
import org.zalando.nakadi.exceptions.runtime.InvalidStreamParametersException;
import org.zalando.nakadi.security.Client;
import org.zalando.nakadi.service.EventStreamConfig;
import org.zalando.nakadi.view.UserStreamParameters;
Expand Down Expand Up @@ -52,11 +52,11 @@ public class StreamParameters {
private StreamParameters(
final UserStreamParameters userParameters,
final long maxCommitTimeout,
final Client consumingClient) throws WrongStreamParametersException {
final Client consumingClient) throws InvalidStreamParametersException {

this.batchLimitEvents = userParameters.getBatchLimit().orElse(1);
if (batchLimitEvents <= 0) {
throw new WrongStreamParametersException("batch_limit can't be lower than 1");
throw new InvalidStreamParametersException("batch_limit can't be lower than 1");
}
this.streamLimitEvents = userParameters.getStreamLimit().filter(v -> v != 0);
this.batchTimespan = TimeUnit.SECONDS.toMillis(userParameters.getBatchTimespan().orElse(0L));
Expand All @@ -72,9 +72,9 @@ private StreamParameters(

final long commitTimeout = userParameters.getCommitTimeoutSeconds().orElse(maxCommitTimeout);
if (commitTimeout > maxCommitTimeout) {
throw new WrongStreamParametersException("commit_timeout can not be more than " + maxCommitTimeout);
throw new InvalidStreamParametersException("commit_timeout can not be more than " + maxCommitTimeout);
} else if (commitTimeout < 0) {
throw new WrongStreamParametersException("commit_timeout can not be less than 0");
throw new InvalidStreamParametersException("commit_timeout can not be less than 0");
}
this.commitTimeoutMillis = TimeUnit.SECONDS.toMillis(commitTimeout == 0 ? maxCommitTimeout : commitTimeout);
}
Expand All @@ -101,7 +101,7 @@ public List<EventTypePartition> getPartitions() {

public static StreamParameters of(final UserStreamParameters userStreamParameters,
final long maxCommitTimeoutSeconds,
final Client client) throws WrongStreamParametersException {
final Client client) throws InvalidStreamParametersException {
return new StreamParameters(userStreamParameters, maxCommitTimeoutSeconds, client);
}

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

import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.zalando.nakadi.exceptions.runtime.WrongStreamParametersException;
import org.zalando.nakadi.exceptions.runtime.InvalidStreamParametersException;
import org.zalando.nakadi.security.Client;
import org.zalando.nakadi.service.EventStreamConfig;
import org.zalando.nakadi.view.UserStreamParameters;
Expand All @@ -19,7 +19,7 @@

public class StreamParametersTest {

@Test(expected = WrongStreamParametersException.class)
@Test(expected = InvalidStreamParametersException.class)
public void whenBatchLimitLowerOrEqualToZeroTheException() throws Exception {
createStreamParameters(0, null, 0L, 0, null, null, 0, 0, mock(Client.class));
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public static StreamParameters createStreamParameters(final int batchLimitEvents
final Integer batchKeepAliveIterations,
final int maxUncommittedMessages,
final long commitTimeoutSeconds,
final Client client) throws WrongStreamParametersException {
final Client client) throws InvalidStreamParametersException {
final UserStreamParameters userParams = new UserStreamParameters(batchLimitEvents, streamLimitEvents,
batchTimespan, batchTimeoutSeconds, streamTimeoutSeconds, batchKeepAliveIterations,
maxUncommittedMessages, ImmutableList.of(), commitTimeoutSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException;
import org.zalando.nakadi.exceptions.runtime.UnableProcessException;
import org.zalando.nakadi.exceptions.runtime.UnprocessableSubscriptionException;
import org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidOwningApplicationException;
import org.zalando.problem.Problem;
import org.zalando.problem.Status;
Expand Down Expand Up @@ -45,7 +45,7 @@ public ResponseEntity<Problem> handleSubscriptionCreationDisabledException(
}

@ExceptionHandler({
WrongInitialCursorsException.class,
InvalidInitialCursorsException.class,
TooManyPartitionsException.class,
UnprocessableSubscriptionException.class,
UnableProcessException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.zalando.nakadi.exceptions.runtime.SubscriptionUpdateConflictException;
import org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException;
import org.zalando.nakadi.exceptions.runtime.UnableProcessException;
import org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidOwningApplicationException;
import org.zalando.nakadi.plugin.api.authz.AuthorizationAttribute;
import org.zalando.nakadi.repository.TopicRepository;
Expand Down Expand Up @@ -126,7 +126,7 @@ public SubscriptionService(final SubscriptionDbRepository subscriptionRepository

public Subscription createSubscription(final SubscriptionBase subscriptionBase)
throws TooManyPartitionsException, RepositoryProblemException, DuplicatedSubscriptionException,
NoSuchEventTypeException, InconsistentStateException, WrongInitialCursorsException,
NoSuchEventTypeException, InconsistentStateException, InvalidInitialCursorsException,
DbWriteOperationsBlockedException, UnableProcessException,
AuthorizationNotPresentException, ServiceTemporarilyUnavailableException,
InvalidOwningApplicationException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.zalando.nakadi.exceptions.runtime;

public class InvalidInitialCursorsException extends NakadiBaseException {

public InvalidInitialCursorsException(final String msg) {
super(msg);
}

public InvalidInitialCursorsException(final String msg, final Exception cause) {
super(msg, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.zalando.nakadi.exceptions.runtime;

public class InvalidStreamParametersException extends NakadiBaseException {

public InvalidStreamParametersException(final String message) {
super(message);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.zalando.nakadi.exceptions.runtime.SubscriptionUpdateConflictException;
import org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException;
import org.zalando.nakadi.exceptions.runtime.UnableProcessException;
import org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidOwningApplicationException;
import org.zalando.nakadi.exceptions.runtime.WrongStreamParametersException;
import org.zalando.nakadi.exceptions.runtime.InvalidStreamParametersException;
import org.zalando.nakadi.plugin.api.ApplicationService;
import org.zalando.nakadi.service.timeline.TimelineService;
import org.zalando.nakadi.validation.ResourceValidationHelperService;
Expand Down Expand Up @@ -75,7 +75,7 @@ public SubscriptionValidationService(final TimelineService timelineService,

public void validateSubscriptionOnCreate(final SubscriptionBase subscription)
throws TooManyPartitionsException, RepositoryProblemException, NoSuchEventTypeException,
InconsistentStateException, WrongInitialCursorsException, UnableProcessException,
InconsistentStateException, InvalidInitialCursorsException, UnableProcessException,
ServiceTemporarilyUnavailableException, InvalidOwningApplicationException {

// check that all event-types exist
Expand Down Expand Up @@ -143,7 +143,7 @@ public void validatePartitionsToStream(final Subscription subscription, final Li
// check for duplicated partitions
final long uniquePartitions = partitions.stream().distinct().count();
if (uniquePartitions < partitions.size()) {
throw new WrongStreamParametersException("Duplicated partition specified");
throw new InvalidStreamParametersException("Duplicated partition specified");
}
// check that partitions belong to subscription
final List<EventTypePartition> allPartitions = getAllPartitions(subscription.getEventTypes());
Expand All @@ -154,31 +154,31 @@ public void validatePartitionsToStream(final Subscription subscription, final Li
final String wrongPartitionsDesc = wrongPartitions.stream()
.map(EventTypePartition::toString)
.collect(Collectors.joining(", "));
throw new WrongStreamParametersException("Wrong partitions specified - some partitions don't belong to " +
throw new InvalidStreamParametersException("Wrong partitions specified - some partitions don't belong to " +
"subscription: " + wrongPartitionsDesc);
}
}

private void validateInitialCursors(final SubscriptionBase subscription,
final List<EventTypePartition> allPartitions)
throws WrongInitialCursorsException, RepositoryProblemException {
throws InvalidInitialCursorsException, RepositoryProblemException {

final boolean cursorsMissing = allPartitions.stream()
.anyMatch(p -> !subscription.getInitialCursors().stream().anyMatch(p::ownsCursor));
if (cursorsMissing) {
throw new WrongInitialCursorsException(
throw new InvalidInitialCursorsException(
"initial_cursors should contain cursors for all partitions of subscription");
}

final boolean hasCursorForWrongPartition = subscription.getInitialCursors().stream()
.anyMatch(c -> !allPartitions.contains(new EventTypePartition(c.getEventType(), c.getPartition())));
if (hasCursorForWrongPartition) {
throw new WrongInitialCursorsException(
throw new InvalidInitialCursorsException(
"initial_cursors should contain cursors only for partitions of this subscription");
}

if (subscription.getInitialCursors().size() > allPartitions.size()) {
throw new WrongInitialCursorsException(
throw new InvalidInitialCursorsException(
"there should be no more than 1 cursor for each partition in initial_cursors");
}

Expand All @@ -192,7 +192,7 @@ private void validateInitialCursors(final SubscriptionBase subscription,
Collections.singletonList(nakadiCursor));
}
} catch (final InvalidCursorException ex) {
throw new WrongInitialCursorsException(ex.getMessage(), ex);
throw new InvalidInitialCursorsException(ex.getMessage(), ex);
} catch (final InternalNakadiException | ServiceTemporarilyUnavailableException ex) {
throw new RepositoryProblemException("Topic repository problem occurred when validating cursors", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.zalando.nakadi.exceptions.runtime.RepositoryProblemException;
import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException;
import org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException;
import org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidInitialCursorsException;
import org.zalando.nakadi.exceptions.runtime.InvalidOwningApplicationException;
import org.zalando.nakadi.plugin.api.ApplicationService;
import org.zalando.nakadi.repository.TopicRepository;
Expand Down Expand Up @@ -180,7 +180,7 @@ public void whenCursorForSomePartitionIsMissingThenException() {
try {
subscriptionValidationService.validateSubscriptionOnCreate(subscriptionBase);
fail("WrongInitialCursorsException expected");
} catch (final WrongInitialCursorsException e) {
} catch (final InvalidInitialCursorsException e) {
assertThat(e.getMessage(),
equalTo("initial_cursors should contain cursors for all partitions of subscription"));
}
Expand All @@ -197,7 +197,7 @@ public void whenCursorForWrongPartitionThenException() {
try {
subscriptionValidationService.validateSubscriptionOnCreate(subscriptionBase);
fail("WrongInitialCursorsException expected");
} catch (final WrongInitialCursorsException e) {
} catch (final InvalidInitialCursorsException e) {
assertThat(e.getMessage(),
equalTo("initial_cursors should contain cursors only for partitions of this subscription"));
}
Expand All @@ -214,13 +214,13 @@ public void whenMoreThanOneCursorPerPartitionThenException() {
try {
subscriptionValidationService.validateSubscriptionOnCreate(subscriptionBase);
fail("WrongInitialCursorsException expected");
} catch (final WrongInitialCursorsException e) {
} catch (final InvalidInitialCursorsException e) {
assertThat(e.getMessage(),
equalTo("there should be no more than 1 cursor for each partition in initial_cursors"));
}
}

@Test(expected = WrongInitialCursorsException.class)
@Test(expected = InvalidInitialCursorsException.class)
public void whenInvalidCursorThenException() {
subscriptionBase.setInitialCursors(ImmutableList.of(
new SubscriptionCursorWithoutToken(ET1, P0, ""),
Expand Down

0 comments on commit 9788f21

Please sign in to comment.