Repository: GoogleCloudPlatform/pgadapter
Pull request #450 (2022-10-31 11:45:09)

chore: add class 22 error codes
Adds the `Class 22` PostgreSQL error codes.

codecov[bot]: # [Codecov](https://codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/450?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) Report
> Merging [#450](https://codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/450?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) (cf03d5f) into [postgresql-dialect](https://codecov.io/gh/GoogleCloudPlatform/pgadapter/commit/b8a38ddf5236222c458b24658dc4d1c75bcc9c19?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) (b8a38dd) will **increase** coverage by `0.13%`.
> The diff coverage is `100.00%`.

```diff
@@                   Coverage Diff                    @@
##             postgresql-dialect     #450      +/-   ##
========================================================
+ Coverage                 87.19%   87.33%   +0.13%     
- Complexity                 2018     2020       +2     
========================================================
  Files                       122      122              
  Lines                      6578     6649      +71     
  Branches                    956      956              
========================================================
+ Hits                       5736     5807      +71     
  Misses                      594      594              
  Partials                    248      248              
```

| Flag | Coverage Δ | |
|---|---|---|
| all_tests | `87.33% <100.00%> (+0.13%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/450?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) | Coverage Δ | |
|---|---|---|
| [...ud/spanner/pgadapter/error/PGExceptionFactory.java](https://codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/450/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#diff-c3JjL21haW4vamF2YS9jb20vZ29vZ2xlL2Nsb3VkL3NwYW5uZXIvcGdhZGFwdGVyL2Vycm9yL1BHRXhjZXB0aW9uRmFjdG9yeS5qYXZh) | `100.00% <100.00%> (ø)` | |
| [...google/cloud/spanner/pgadapter/error/SQLState.java](https://codecov.io/gh/GoogleCloudPlatform/pgadapter/pull/450/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#diff-c3JjL21haW4vamF2YS9jb20vZ29vZ2xlL2Nsb3VkL3NwYW5uZXIvcGdhZGFwdGVyL2Vycm9yL1NRTFN0YXRlLmphdmE=) | `98.78% <100.00%> (+0.84%)` | :arrow_up: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform)


Patch:
diff --git a/src/main/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactory.java b/src/main/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactory.java
index fabc9998..207cea96 100644
--- a/src/main/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactory.java
+++ b/src/main/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactory.java
@@ -23,6 +23,17 @@ import io.grpc.StatusRuntimeException;
 public class PGExceptionFactory {
   private PGExceptionFactory() {}
 
+  /**
+   * Throws a {@link PGException} with the given message and {@link SQLState#InvalidParameterValue}
+   * if valid is false. Otherwise, returns the given value.
+   */
+  public static <T> T checkArgument(T value, boolean valid, String message) {
+    if (!valid) {
+      throw newPGException(message, SQLState.InvalidParameterValue);
+    }
+    return value;
+  }
+
   /**
    * Creates a basic {@link PGException} with {@link Severity#ERROR} and {@link
    * SQLState#RaiseException}.
diff --git a/src/main/java/com/google/cloud/spanner/pgadapter/error/SQLState.java b/src/main/java/com/google/cloud/spanner/pgadapter/error/SQLState.java
index 6ef57cdb..1d47d0b6 100644
--- a/src/main/java/com/google/cloud/spanner/pgadapter/error/SQLState.java
+++ b/src/main/java/com/google/cloud/spanner/pgadapter/error/SQLState.java
@@ -24,7 +24,7 @@ public enum SQLState {
   NullValueEliminatedInSetFunction("01003"),
   PrivilegeNotGranted("01007"),
   PrivilegeNotRevoked("01006"),
-  StringDataRightTruncation("01004"),
+  StringDataRightTruncationWarning("01004"),
   DeprecatedFeature("01P01"),
   NoData("02000"),
   NoAdditionalDynamicResultSetsReturned("02001"),
@@ -65,6 +65,76 @@ public enum SQLState {
   InvalidAuthorizationSpecification("28000"),
   InvalidPassword("28P01"),
 
+  // Class 22
+  DataException("22000"),
+  ArraySubscriptError("2202E"),
+  CharacterNotInRepertoire("22021"),
+  DatetimeFieldOverflow("22008"),
+  DivisionByZero("22012"),
+  ErrorInAssignment("22005"),
+  EscapeCharacterConflict("2200B"),
+  IndicatorOverflow("22022"),
+  IntervalFieldOverflow("22015"),
+  InvalidArgumentForLogarithm("2201E"),
+  InvalidArgumentForNtileFunction("22014"),
+  InvalidArgumentForNthValueFunction("22016"),
+  InvalidArgumentForPowerFunction("2201F"),
+  InvalidArgumentForWidthBucketFunction("2201G"),
+  InvalidCharacterValueForCast("22018"),
+  InvalidDatetimeFormat("22007"),
+  InvalidEscapeCharacter("22019"),
+  InvalidEscapeOctet("2200D"),
+  InvalidEscapeSequence("22025"),
+  NonstandardUseOfEscapeCharacter("22P06"),
+  InvalidIndicatorParameterValue("22010"),
+  InvalidParameterValue("22023"),
+  InvalidPrecedingOrFollowingSize("22013"),
+  InvalidRegularExpression("2201B"),
+  InvalidRowCountInLimitClause("2201W"),
+  InvalidRowCountInResultOffsetClause("2201X"),
+  InvalidTablesampleArgument("2202H"),
+  InvalidTablesampleRepeat("2202G"),
+  InvalidTimeZoneDisplacementValue("22009"),
+  InvalidUseOfEscapeCharacter("2200C"),
+  MostSpecificTypeMismatch("2200G"),
+  NullValueNotAllowed("22004"),
+  NullValueNoIndicatorParameter("22002"),
+  NumericValueOutOfRange("22003"),
+  SequenceGeneratorLimitExceeded("2200H"),
+  StringDataLengthMismatch("22026"),
+  StringDataRightTruncation("22001"),
+  SubstringError("22011"),
+  TrimError("22027"),
+  UnterminatedCString("22024"),
+  ZeroLengthCharacterString("2200F"),
+  FloatingPointException("22P01"),
+  InvalidTextRepresentation("22P02"),
+  InvalidBinaryRepresentation("22P03"),
+  BadCopyFileFormat("22P04"),
+  UntranslatableCharacter("22P05"),
+  NotAnXmlDocument("2200L"),
+  InvalidXmlDocument("2200M"),
+  InvalidXmlContent("2200N"),
+  InvalidXmlComment("2200S"),
+  InvalidXmlProcessingInstruction("2200T"),
+  DuplicateJsonObjectKeyValue("22030"),
+  InvalidArgumentForSqlJsonDatetimeFunction("22031"),
+  InvalidJsonText("22032"),
+  InvalidSqlJsonSubscript("22033"),
+  MoreThanOneSqlJsonItem("22034"),
+  NoSqlJsonItem("22035"),
+  NonNumericSqlJsonItem("22036"),
+  NonUniqueKeysInAJsonObject("22037"),
+  SingletonSqlJsonItemRequired("22038"),
+  SqlJsonArrayNotFound("22039"),
+  SqlJsonMemberNotFound("2203A"),
+  SqlJsonNumberNotFound("2203B"),
+  SqlJsonObjectNotFound("2203C"),
+  TooManyJsonArrayElements("2203D"),
+  TooManyJsonObjectMembers("2203E"),
+  SqlJsonScalarRequired("2203F"),
+  SqlJsonItemCannotBeCastToTargetType("2203G"),
+
   // Class 42 — Syntax Error or Access Rule Violation
   SyntaxErrorOrAccessRuleViolation("42000"),
   SyntaxError("42601"),


Test patch:
diff --git a/src/test/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactoryTest.java b/src/test/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactoryTest.java
index 7888f41b..c504e617 100644
--- a/src/test/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactoryTest.java
+++ b/src/test/java/com/google/cloud/spanner/pgadapter/error/PGExceptionFactoryTest.java
@@ -14,8 +14,10 @@
 
 package com.google.cloud.spanner.pgadapter.error;
 
+import static com.google.cloud.spanner.pgadapter.error.PGExceptionFactory.checkArgument;
 import static com.google.cloud.spanner.pgadapter.error.PGExceptionFactory.toPGException;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import com.google.api.gax.grpc.GrpcStatusCode;
 import com.google.api.gax.rpc.ApiException;
@@ -51,4 +53,13 @@ public class PGExceptionFactoryTest {
                     GrpcStatusCode.of(Code.NOT_FOUND),
                     /* retryable= */ false))));
   }
+
+  @Test
+  public void testCheckArgument() {
+    assertEquals(1L, checkArgument(1L, true, "test message").longValue());
+    PGException exception =
+        assertThrows(PGException.class, () -> checkArgument(1L, false, "test message"));
+    assertEquals(SQLState.InvalidParameterValue, exception.getSQLState());
+    assertEquals("test message", exception.getMessage());
+  }
 }