diff --git a/src/main/java/io/r2dbc/postgresql/PostgresqlServerErrorException.java b/src/main/java/io/r2dbc/postgresql/PostgresqlServerErrorException.java index 7dfdada0..e7df3148 100644 --- a/src/main/java/io/r2dbc/postgresql/PostgresqlServerErrorException.java +++ b/src/main/java/io/r2dbc/postgresql/PostgresqlServerErrorException.java @@ -97,29 +97,30 @@ public final class PostgresqlServerErrorException extends R2dbcException { * @throws NullPointerException if {@code fields} is {@code null} */ public PostgresqlServerErrorException(List fields) { - Objects.requireNonNull(fields, "fields must not be null"); - - Map map = fields.stream() - .collect(Collectors.toMap(Field::getType, Field::getValue)); + this(convertToMap(fields)); + } - this.code = map.get(CODE); - this.columnName = map.get(COLUMN_NAME); - this.constraintName = map.get(CONSTRAINT_NAME); - this.dataTypeName = map.get(DATA_TYPE_NAME); - this.detail = map.get(DETAIL); - this.file = map.get(FILE); - this.hint = map.get(HINT); - this.internalPosition = map.get(INTERNAL_POSITION); - this.internalQuery = map.get(INTERNAL_QUERY); - this.line = map.get(LINE); - this.message = map.get(MESSAGE); - this.position = map.get(POSITION); - this.routine = map.get(ROUTINE); - this.schemaName = map.get(SCHEMA_NAME); - this.severityLocalized = map.get(SEVERITY_LOCALIZED); - this.severityNonLocalized = map.get(SEVERITY_NON_LOCALIZED); - this.tableName = map.get(TABLE_NAME); - this.where = map.get(WHERE); + private PostgresqlServerErrorException(Map fields) { + super(fields.get(MESSAGE), fields.get(CODE)); + + this.code = fields.get(CODE); + this.columnName = fields.get(COLUMN_NAME); + this.constraintName = fields.get(CONSTRAINT_NAME); + this.dataTypeName = fields.get(DATA_TYPE_NAME); + this.detail = fields.get(DETAIL); + this.file = fields.get(FILE); + this.hint = fields.get(HINT); + this.internalPosition = fields.get(INTERNAL_POSITION); + this.internalQuery = fields.get(INTERNAL_QUERY); + this.line = fields.get(LINE); + this.message = fields.get(MESSAGE); + this.position = fields.get(POSITION); + this.routine = fields.get(ROUTINE); + this.schemaName = fields.get(SCHEMA_NAME); + this.severityLocalized = fields.get(SEVERITY_LOCALIZED); + this.severityNonLocalized = fields.get(SEVERITY_NON_LOCALIZED); + this.tableName = fields.get(TABLE_NAME); + this.where = fields.get(WHERE); } @Override @@ -352,6 +353,13 @@ static void handleErrorResponse(BackendMessage message, SynchronousSink convertToMap(List fields) { + Objects.requireNonNull(fields, "fields must not be null"); + + return fields.stream() + .collect(Collectors.toMap(Field::getType, Field::getValue)); + } + private static PostgresqlServerErrorException toException(ErrorResponse errorResponse) { Objects.requireNonNull(errorResponse, "errorResponse must not be null"); diff --git a/src/test/java/io/r2dbc/postgresql/PostgresqlServerErrorExceptionTest.java b/src/test/java/io/r2dbc/postgresql/PostgresqlServerErrorExceptionTest.java new file mode 100644 index 00000000..a972ae6d --- /dev/null +++ b/src/test/java/io/r2dbc/postgresql/PostgresqlServerErrorExceptionTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2017-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.r2dbc.postgresql; + +import io.r2dbc.postgresql.message.backend.Field; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; + +final class PostgresqlServerErrorExceptionTest { + + @Test + void initializesReason() { + PostgresqlServerErrorException exception = new PostgresqlServerErrorException(Collections.singletonList(new Field(Field.FieldType.MESSAGE, "Duplicate"))); + + assertThat(exception.getMessage()).isEqualTo("Duplicate"); + } + + @Test + void initializesSqlState() { + PostgresqlServerErrorException exception = new PostgresqlServerErrorException(Collections.singletonList(new Field(Field.FieldType.CODE, "1234"))); + + assertThat(exception.getSqlState()).isEqualTo("1234"); + } + + @Test + void skipsInitializationWithEmptyFields() { + PostgresqlServerErrorException exception = new PostgresqlServerErrorException(Collections.emptyList()); + + assertThat(exception.getSqlState()).isNull(); + assertThat(exception.getMessage()).isNull(); + } +}