Search in sources :

Example 1 with ErrorResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse in project pgadapter by GoogleCloudPlatform.

the class CopyFailMessage method sendPayload.

@Override
protected void sendPayload() throws Exception {
    // If backend error occurred during copy-in mode, drop any subsequent CopyFail messages.
    if (this.statement != null) {
        MutationWriter mutationWriter = this.statement.getMutationWriter();
        mutationWriter.rollback();
        mutationWriter.closeErrorFile();
        statement.close();
        if (!statement.hasException(0)) {
            new ErrorResponse(this.outputStream, new Exception(this.errorMessage), State.IOError).send();
        }
    }
    this.connection.setStatus(ConnectionStatus.IDLE);
    this.connection.removeActiveStatement(this.statement);
    new ReadyResponse(this.outputStream, ReadyResponse.Status.IDLE).send();
}
Also used : ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Example 2 with ErrorResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse in project pgadapter by GoogleCloudPlatform.

the class ProxyServer method handleConnectionError.

/**
 * Sends a message to the client that the connection could not be established.
 *
 * @param exception The exception that caused the connection request to fail.
 * @param socket The socket that was created for the connection.
 */
private void handleConnectionError(SpannerException exception, Socket socket) {
    logger.log(Level.SEVERE, exception, () -> String.format("Something went wrong in establishing a Spanner connection: %s", exception.getMessage()));
    try {
        DataOutputStream output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        new ErrorResponse(output, exception, ErrorResponse.State.ConnectionException, Severity.FATAL).send();
        output.flush();
    } catch (Exception e) {
        logger.log(Level.WARNING, e, () -> String.format("Failed to send fatal error message to client: %s", e.getMessage()));
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) SocketException(java.net.SocketException) IOException(java.io.IOException) SpannerException(com.google.cloud.spanner.SpannerException) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Example 3 with ErrorResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse in project pgadapter by GoogleCloudPlatform.

the class ConnectionHandler method handleError.

/**
 * Takes an Exception Object and relates its results to the user within the client.
 *
 * @param e The exception to be related.
 * @throws IOException if there is some issue in the sending of the error messages.
 */
private void handleError(DataOutputStream output, Exception e) throws Exception {
    logger.log(Level.WARNING, e, () -> String.format("Exception on connection handler with ID %s: %s", getName(), e));
    if (this.status == ConnectionStatus.TERMINATED) {
        new ErrorResponse(output, e, ErrorResponse.State.InternalError, Severity.FATAL).send();
        new TerminateResponse(output).send();
    } else if (this.status == ConnectionStatus.COPY_IN) {
        new ErrorResponse(output, e, ErrorResponse.State.InternalError).send();
    } else {
        this.status = ConnectionStatus.IDLE;
        new ErrorResponse(output, e, ErrorResponse.State.InternalError).send();
        new ReadyResponse(output, ReadyResponse.Status.IDLE).send();
    }
}
Also used : TerminateResponse(com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse) ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Example 4 with ErrorResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse in project pgadapter by GoogleCloudPlatform.

the class ControlMessage method handleError.

/**
 * Takes an Exception Object and relates its results to the user within the client.
 *
 * @param e The exception to be related.
 * @throws Exception if there is some issue in the sending of the error messages.
 */
protected void handleError(Exception e) throws Exception {
    new ErrorResponse(this.outputStream, e, State.RaiseException).send(false);
    if (connection.getSpannerConnection().isInTransaction()) {
        connection.getSpannerConnection().rollbackAsync();
        connection.setStatus(ConnectionStatus.TRANSACTION_ABORTED);
    }
    this.outputStream.flush();
}
Also used : ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Example 5 with ErrorResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse in project pgadapter by GoogleCloudPlatform.

the class PasswordMessage method sendPayload.

protected void sendPayload() throws Exception {
    if (useAuthentication() && !checkCredentials(this.username, this.password)) {
        new ErrorResponse(this.outputStream, new Exception("Could not Authenticate User"), State.InternalError).send();
        new TerminateResponse(this.outputStream).send();
    } else {
        BootstrapMessage.sendStartupMessage(this.outputStream, this.connection.getConnectionId(), this.connection.getSecret(), this.connection.getServer().getOptions());
    }
}
Also used : TerminateResponse(com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Aggregations

ErrorResponse (com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)6 ReadyResponse (com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse)3 TerminateResponse (com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse)2 SpannerException (com.google.cloud.spanner.SpannerException)1 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)1 MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)1 CopyInResponse (com.google.cloud.spanner.pgadapter.wireoutput.CopyInResponse)1 EmptyQueryResponse (com.google.cloud.spanner.pgadapter.wireoutput.EmptyQueryResponse)1 RowDescriptionResponse (com.google.cloud.spanner.pgadapter.wireoutput.RowDescriptionResponse)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1