Skip to content

Commit

Permalink
Initialize reason and
Browse files Browse the repository at this point in the history
Initialize reason and SQLState in PostgresqlServerErrorException.

Previously, the reason and SQLState properties of the R2dbcException type were
unspecified.  This change updates the code to provide these values from the
exception field payload.  This aligns the behavior with the JDBC driver that
populates SQLState and leaves errorCode empty.

[resolves #3, resolves #4]
  • Loading branch information
mp911de authored and nebhale committed Jun 22, 2018
1 parent 0ab72c5 commit d1aa083
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,30 @@ public final class PostgresqlServerErrorException extends R2dbcException {
* @throws NullPointerException if {@code fields} is {@code null}
*/
public PostgresqlServerErrorException(List<Field> fields) {
Objects.requireNonNull(fields, "fields must not be null");

Map<FieldType, String> 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<FieldType, String> 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
Expand Down Expand Up @@ -352,6 +353,13 @@ static void handleErrorResponse(BackendMessage message, SynchronousSink<BackendM
}
}

private static Map<FieldType, String> convertToMap(List<Field> 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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit d1aa083

Please sign in to comment.