Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize reason and SQLState in PostgresqlServerErrorException. #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,57 @@
/*
* 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 java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

final class PostgresqlServerErrorExceptionTest {

@Test
void initializesSqlState() {

List<Field> fields = Collections.singletonList(new Field(Field.FieldType.CODE, "1234"));

PostgresqlServerErrorException exception = new PostgresqlServerErrorException(fields);

assertThat(exception.getSqlState()).isEqualTo("1234");
}

@Test
void initializesReason() {

List<Field> fields = Collections.singletonList(new Field(Field.FieldType.MESSAGE, "Duplicate"));

PostgresqlServerErrorException exception = new PostgresqlServerErrorException(fields);

assertThat(exception.getMessage()).isEqualTo("Duplicate");
}

@Test
void skipsInitializationWithEmptyFields() {

PostgresqlServerErrorException exception = new PostgresqlServerErrorException(Collections.emptyList());

assertThat(exception.getSqlState()).isNull();
assertThat(exception.getMessage()).isNull();
}
}