Skip to content

Commit

Permalink
Add error codes to error messages (#1493)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Wong <[email protected]>
  • Loading branch information
brfrn169 and josh-wong authored Feb 16, 2024
1 parent 0c23ecb commit c49d064
Show file tree
Hide file tree
Showing 86 changed files with 1,654 additions and 508 deletions.
34 changes: 23 additions & 11 deletions core/src/main/java/com/scalar/db/api/AuthAdmin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scalar.db.api;

import com.scalar.db.common.error.CoreError;
import com.scalar.db.exception.storage.ExecutionException;
import java.util.List;
import java.util.Optional;
Expand All @@ -24,7 +25,8 @@ public interface AuthAdmin {
*/
default void createUser(String username, @Nullable String password, UserOption... userOptions)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -40,7 +42,8 @@ default void createUser(String username, @Nullable String password, UserOption..
*/
default void alterUser(String username, @Nullable String password, UserOption... userOptions)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -51,7 +54,8 @@ default void alterUser(String username, @Nullable String password, UserOption...
* @throws ExecutionException if the operation fails
*/
default void dropUser(String username) throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -67,7 +71,8 @@ default void dropUser(String username) throws ExecutionException {
default void grant(
String username, String namespaceName, String tableName, Privilege... privileges)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -81,7 +86,8 @@ default void grant(
*/
default void grant(String username, String namespaceName, Privilege... privileges)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -97,7 +103,8 @@ default void grant(String username, String namespaceName, Privilege... privilege
default void revoke(
String username, String namespaceName, String tableName, Privilege... privileges)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -111,7 +118,8 @@ default void revoke(
*/
default void revoke(String username, String namespaceName, Privilege... privileges)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -122,7 +130,8 @@ default void revoke(String username, String namespaceName, Privilege... privileg
* @throws ExecutionException if the operation fails
*/
default Optional<User> getUser(String username) throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -132,7 +141,8 @@ default Optional<User> getUser(String username) throws ExecutionException {
* @throws ExecutionException if the operation fails
*/
default List<User> getUsers() throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -147,7 +157,8 @@ default List<User> getUsers() throws ExecutionException {
*/
default Set<Privilege> getPrivileges(String username, String namespaceName, String tableName)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

/**
Expand All @@ -161,7 +172,8 @@ default Set<Privilege> getPrivileges(String username, String namespaceName, Stri
*/
default Set<Privilege> getPrivileges(String username, String namespaceName)
throws ExecutionException {
throw new UnsupportedOperationException("Not supported in the community edition");
throw new UnsupportedOperationException(
CoreError.NOT_SUPPORTED_IN_COMMUNITY_EDITION.buildMessage());
}

interface User {
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/com/scalar/db/api/ConditionBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.scalar.db.api;

import com.scalar.db.api.ConditionalExpression.Operator;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.BigIntColumn;
import com.scalar.db.io.BlobColumn;
import com.scalar.db.io.BooleanColumn;
Expand Down Expand Up @@ -802,8 +803,8 @@ private void check(ConditionalExpression conditionalExpression) {
if (conditionalExpression.getOperator().equals(Operator.LIKE)
|| conditionalExpression.getOperator().equals(Operator.NOT_LIKE)) {
throw new IllegalArgumentException(
"This condition is not allowed for the PutIf operation. Condition: "
+ conditionalExpression);
CoreError.CONDITION_BUILD_ERROR_CONDITION_NOT_ALLOWED_FOR_PUT_IF.buildMessage(
conditionalExpression));
}
}
}
Expand Down Expand Up @@ -843,8 +844,8 @@ private void check(ConditionalExpression conditionalExpression) {
if (conditionalExpression.getOperator().equals(Operator.LIKE)
|| conditionalExpression.getOperator().equals(Operator.NOT_LIKE)) {
throw new IllegalArgumentException(
"This condition is not allowed for the DeleteIf operation. Condition: "
+ conditionalExpression);
CoreError.CONDITION_BUILD_ERROR_CONDITION_NOT_ALLOWED_FOR_DELETE_IF.buildMessage(
conditionalExpression));
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/com/scalar/db/api/GetBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.scalar.db.api.OperationBuilder.PartitionKeyBuilder;
import com.scalar.db.api.OperationBuilder.Projection;
import com.scalar.db.api.OperationBuilder.TableBuilder;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.Key;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -289,14 +290,18 @@ public BuildableGetOrGetWithIndexFromExisting clearNamespace() {
private void checkNotGet() {
if (!isGetWithIndex) {
throw new UnsupportedOperationException(
"This operation is not supported when getting records of a database without using a secondary index");
CoreError
.GET_BUILD_ERROR_OPERATION_NOT_SUPPORTED_WHEN_GETTING_RECORDS_OF_DATABASE_WITHOUT_USING_INDEX
.buildMessage());
}
}

private void checkNotGetWithIndex() {
if (isGetWithIndex) {
throw new UnsupportedOperationException(
"This operation is not supported when getting records of a database using a secondary index");
CoreError
.GET_BUILD_ERROR_OPERATION_NOT_SUPPORTED_WHEN_GETTING_RECORDS_OF_DATABASE_USING_INDEX
.buildMessage());
}
}

Expand Down
20 changes: 14 additions & 6 deletions core/src/main/java/com/scalar/db/api/LikeExpression.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.scalar.db.api;

import com.google.common.base.MoreObjects;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.TextColumn;
import java.util.Objects;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -40,29 +41,36 @@ public class LikeExpression extends ConditionalExpression {
private void check(String pattern, Operator operator, String escape) {
if (operator != Operator.LIKE && operator != Operator.NOT_LIKE) {
throw new IllegalArgumentException(
"Operator must be like or not-like. Operator: " + operator);
CoreError.LIKE_CHECK_ERROR_OPERATOR_MUST_BE_LIKE_OR_NOT_LIKE.buildMessage(operator));
}

if (escape == null || escape.length() > 1) {
throw new IllegalArgumentException(
"Escape character must be a string of a single character or an empty string");
CoreError
.LIKE_CHECK_ERROR_ESCAPE_CHARACTER_MUST_BE_STRING_OF_SINGLE_CHARACTER_OR_EMPTY_STRING
.buildMessage());
}

if (pattern == null) {
throw new IllegalArgumentException("LIKE pattern must not be null");
throw new IllegalArgumentException(
CoreError.LIKE_CHECK_ERROR_LIKE_PATTERN_MUST_NOT_BE_NULL.buildMessage());
}

Character escapeChar = escape.length() == 0 ? null : escape.charAt(0);
Character escapeChar = escape.isEmpty() ? null : escape.charAt(0);
char[] chars = pattern.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (escapeChar != null && c == escapeChar && i + 1 < chars.length) {
char nextChar = chars[++i];
if (nextChar != '_' && nextChar != '%' && nextChar != escapeChar) {
throw new IllegalArgumentException("LIKE pattern must not include only escape character");
throw new IllegalArgumentException(
CoreError.LIKE_CHECK_ERROR_LIKE_PATTERN_MUST_NOT_INCLUDE_ONLY_ESCAPE_CHARACTER
.buildMessage());
}
} else if (escapeChar != null && c == escapeChar) {
throw new IllegalArgumentException("LIKE pattern must not end with escape character");
throw new IllegalArgumentException(
CoreError.LIKE_CHECK_ERROR_LIKE_PATTERN_MUST_NOT_END_WITH_ESCAPE_CHARACTER
.buildMessage());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/scalar/db/api/Put.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.ImmutableSet;
import com.scalar.db.api.PutBuilder.BuildableFromExisting;
import com.scalar.db.api.PutBuilder.Namespace;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.BigIntColumn;
import com.scalar.db.io.BlobColumn;
import com.scalar.db.io.BooleanColumn;
Expand Down Expand Up @@ -705,7 +706,7 @@ public Set<String> getContainedColumnNames() {

private void checkIfExists(String name) {
if (!containsColumn(name)) {
throw new IllegalArgumentException(name + " doesn't exist");
throw new IllegalArgumentException(CoreError.COLUMN_NOT_FOUND.buildMessage(name));
}
}

Expand Down
22 changes: 15 additions & 7 deletions core/src/main/java/com/scalar/db/api/ScanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.scalar.db.api.OperationBuilder.WhereAnd;
import com.scalar.db.api.OperationBuilder.WhereOr;
import com.scalar.db.api.Scan.Conjunction;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.Key;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -858,37 +859,44 @@ public BuildableScanOrScanAllFromExisting clearNamespace() {
private void checkNotScanWithIndexOrScanAll() {
if (isScanWithIndex || isScanAll) {
throw new UnsupportedOperationException(
"This operation is not supported when scanning all the records of a database "
+ "or scanning records of a database using a secondary index");
CoreError
.SCAN_BUILD_ERROR_OPERATION_NOT_SUPPORTED_WHEN_SCANNING_ALL_RECORDS_OF_DATABASE_OR_SCANNING_RECORDS_OF_DATABASE_USING_INDEX
.buildMessage());
}
}

private void checkScanWithIndex() {
if (!isScanWithIndex) {
throw new UnsupportedOperationException(
"This operation is supported only when scanning records of a database using a secondary index");
CoreError
.SCAN_BUILD_ERROR_OPERATION_SUPPORTED_ONLY_WHEN_SCANNING_RECORDS_OF_DATABASE_USING_INDEX
.buildMessage());
}
}

private void checkNotScanWithIndex() {
if (isScanWithIndex) {
throw new UnsupportedOperationException(
"This operation is not supported when scanning records of a database using a secondary index");
CoreError
.SCAN_BUILD_ERROR_OPERATION_NOT_SUPPORTED_WHEN_SCANNING_RECORDS_OF_DATABASE_USING_INDEX
.buildMessage());
}
}

private void checkScanAll() {
if (!isScanAll) {
throw new UnsupportedOperationException(
"This operation is supported only when scanning all the records of a database");
CoreError
.SCAN_BUILD_ERROR_OPERATION_SUPPORTED_ONLY_WHEN_SCANNING_ALL_RECORDS_OF_DATABASE
.buildMessage());
}
}

private void checkConditionsEmpty() {
if (!conjunctions.isEmpty()) {
throw new IllegalStateException(
"This operation is supported only when no conditions are specified at all. "
+ "If you want to modify the condition, please use clearConditions() to remove all existing conditions first");
CoreError.SCAN_BUILD_ERROR_OPERATION_SUPPORTED_ONLY_WHEN_NO_CONDITIONS_ARE_SPECIFIED
.buildMessage());
}
}

Expand Down
18 changes: 10 additions & 8 deletions core/src/main/java/com/scalar/db/api/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.scalar.db.api.Scan.Ordering.Order;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.DataType;
import com.scalar.db.util.ImmutableLinkedHashSet;
import java.util.HashMap;
Expand Down Expand Up @@ -230,27 +231,28 @@ public Builder removeSecondaryIndex(String name) {

public TableMetadata build() {
if (columns.isEmpty()) {
throw new IllegalStateException("Need to specify one or more columns");
throw new IllegalStateException(
CoreError.TABLE_METADATA_BUILD_ERROR_NO_COLUMNS_SPECIFIED.buildMessage());
}
if (partitionKeyNames.isEmpty()) {
throw new IllegalStateException("Need to specify one or more partition keys");
throw new IllegalStateException(
CoreError.TABLE_METADATA_BUILD_ERROR_NO_PARTITION_KEYS_SPECIFIED.buildMessage());
}
partitionKeyNames.forEach(
k -> {
if (!columns.containsKey(k)) {
throw new IllegalStateException(
"Need to specify the column definition of "
+ k
+ " specified as a partition key");
CoreError.TABLE_METADATA_BUILD_ERROR_PARTITION_KEY_COLUMN_DEFINITION_NOT_SPECIFIED
.buildMessage(k));
}
});
clusteringKeyNames.forEach(
k -> {
if (!columns.containsKey(k)) {
throw new IllegalStateException(
"Need to specify the column definition of "
+ k
+ " specified as a clustering key");
CoreError
.TABLE_METADATA_BUILD_ERROR_CLUSTERING_KEY_COLUMN_DEFINITION_NOT_SPECIFIED
.buildMessage(k));
}
});
return new TableMetadata(
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/com/scalar/db/api/TransactionState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.scalar.db.api;

import com.scalar.db.common.error.CoreError;

public enum TransactionState {
PREPARED(1),
DELETED(2),
Expand All @@ -23,6 +25,7 @@ public static TransactionState getInstance(int id) {
return state;
}
}
throw new IllegalArgumentException("Invalid id specified");
throw new IllegalArgumentException(
CoreError.TRANSACTION_STATE_INSTANTIATION_ERROR_INVALID_ID.buildMessage(id));
}
}
Loading

0 comments on commit c49d064

Please sign in to comment.