|
17 | 17 | import com.datastrato.gravitino.exceptions.NoSuchPartitionException;
|
18 | 18 | import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
|
19 | 19 | import com.datastrato.gravitino.exceptions.NoSuchTableException;
|
| 20 | +import com.datastrato.gravitino.exceptions.NoSuchTopicException; |
20 | 21 | import com.datastrato.gravitino.exceptions.NonEmptySchemaException;
|
21 | 22 | import com.datastrato.gravitino.exceptions.NotFoundException;
|
22 | 23 | import com.datastrato.gravitino.exceptions.PartitionAlreadyExistsException;
|
23 | 24 | import com.datastrato.gravitino.exceptions.RESTException;
|
24 | 25 | import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
|
25 | 26 | import com.datastrato.gravitino.exceptions.TableAlreadyExistsException;
|
| 27 | +import com.datastrato.gravitino.exceptions.TopicAlreadyExistsException; |
26 | 28 | import com.datastrato.gravitino.exceptions.UnauthorizedException;
|
27 | 29 | import com.fasterxml.jackson.databind.ObjectMapper;
|
28 | 30 | import com.google.common.base.Joiner;
|
@@ -112,6 +114,15 @@ public static Consumer<ErrorResponse> filesetErrorHandler() {
|
112 | 114 | return FilesetErrorHandler.INSTANCE;
|
113 | 115 | }
|
114 | 116 |
|
| 117 | + /** |
| 118 | + * Creates an error handler specific to Topic operations. |
| 119 | + * |
| 120 | + * @return A Consumer representing the Topic error handler. |
| 121 | + */ |
| 122 | + public static Consumer<ErrorResponse> topicErrorHandler() { |
| 123 | + return TopicErrorHandler.INSTANCE; |
| 124 | + } |
| 125 | + |
115 | 126 | private ErrorHandlers() {}
|
116 | 127 |
|
117 | 128 | /**
|
@@ -410,6 +421,41 @@ public void accept(ErrorResponse errorResponse) {
|
410 | 421 | }
|
411 | 422 | }
|
412 | 423 |
|
| 424 | + /** Error handler specific to Topic operations. */ |
| 425 | + @SuppressWarnings("FormatStringAnnotation") |
| 426 | + private static class TopicErrorHandler extends RestErrorHandler { |
| 427 | + |
| 428 | + private static final TopicErrorHandler INSTANCE = new TopicErrorHandler(); |
| 429 | + |
| 430 | + @Override |
| 431 | + public void accept(ErrorResponse errorResponse) { |
| 432 | + String errorMessage = formatErrorMessage(errorResponse); |
| 433 | + |
| 434 | + switch (errorResponse.getCode()) { |
| 435 | + case ErrorConstants.ILLEGAL_ARGUMENTS_CODE: |
| 436 | + throw new IllegalArgumentException(errorMessage); |
| 437 | + |
| 438 | + case ErrorConstants.NOT_FOUND_CODE: |
| 439 | + if (errorResponse.getType().equals(NoSuchSchemaException.class.getSimpleName())) { |
| 440 | + throw new NoSuchSchemaException(errorMessage); |
| 441 | + } else if (errorResponse.getType().equals(NoSuchTopicException.class.getSimpleName())) { |
| 442 | + throw new NoSuchTopicException(errorMessage); |
| 443 | + } else { |
| 444 | + throw new NotFoundException(errorMessage); |
| 445 | + } |
| 446 | + |
| 447 | + case ErrorConstants.ALREADY_EXISTS_CODE: |
| 448 | + throw new TopicAlreadyExistsException(errorMessage); |
| 449 | + |
| 450 | + case ErrorConstants.INTERNAL_ERROR_CODE: |
| 451 | + throw new RuntimeException(errorMessage); |
| 452 | + |
| 453 | + default: |
| 454 | + super.accept(errorResponse); |
| 455 | + } |
| 456 | + } |
| 457 | + } |
| 458 | + |
413 | 459 | /** Generic error handler for REST requests. */
|
414 | 460 | private static class RestErrorHandler extends ErrorHandler {
|
415 | 461 | private static final ErrorHandler INSTANCE = new RestErrorHandler();
|
|
0 commit comments