Skip to content

Commit

Permalink
Merge pull request #118 from jhawthorn/error-revamp-part-one
Browse files Browse the repository at this point in the history
Introduce EOFError and make SyscallError < ConnectionError
  • Loading branch information
jhawthorn authored Sep 14, 2023
2 parents e3ff68b + 3bc3927 commit 6c6cd9f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 9 additions & 2 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
VALUE Trilogy_CastError;
static VALUE Trilogy_BaseConnectionError, Trilogy_ProtocolError, Trilogy_SSLError, Trilogy_QueryError,
Trilogy_ConnectionClosedError, Trilogy_ConnectionRefusedError, Trilogy_ConnectionResetError,
Trilogy_TimeoutError, Trilogy_SyscallError, Trilogy_Result;
Trilogy_TimeoutError, Trilogy_SyscallError, Trilogy_Result, Trilogy_EOFError;

static ID id_socket, id_host, id_port, id_username, id_password, id_found_rows, id_connect_timeout, id_read_timeout,
id_write_timeout, id_keepalive_enabled, id_keepalive_idle, id_keepalive_interval, id_keepalive_count,
Expand Down Expand Up @@ -96,7 +96,7 @@ static void trilogy_syserr_fail_str(int e, VALUE msg)
rb_raise(Trilogy_ConnectionResetError, "%" PRIsVALUE, msg);
} else if (e == EPIPE) {
// Backwards compatibility: This error class makes no sense, but matches legacy behavior
rb_raise(Trilogy_QueryError, "%" PRIsVALUE ": TRILOGY_CLOSED_CONNECTION", msg);
rb_raise(Trilogy_EOFError, "%" PRIsVALUE ": TRILOGY_CLOSED_CONNECTION: EPIPE", msg);
} else {
VALUE exc = rb_funcall(Trilogy_SyscallError, id_from_errno, 2, INT2NUM(e), msg);
rb_exc_raise(exc);
Expand Down Expand Up @@ -158,6 +158,10 @@ static void handle_trilogy_error(struct trilogy_ctx *ctx, int rc, const char *ms
rb_raise(Trilogy_BaseConnectionError, "%" PRIsVALUE ": TRILOGY_DNS_ERROR", rbmsg);
}

case TRILOGY_CLOSED_CONNECTION: {
rb_raise(Trilogy_EOFError, "%" PRIsVALUE ": TRILOGY_CLOSED_CONNECTION", rbmsg);
}

default:
rb_raise(Trilogy_QueryError, "%" PRIsVALUE ": %s", rbmsg, trilogy_error(rc));
}
Expand Down Expand Up @@ -1176,6 +1180,9 @@ RUBY_FUNC_EXPORTED void Init_cext()
Trilogy_CastError = rb_const_get(Trilogy, rb_intern("CastError"));
rb_global_variable(&Trilogy_CastError);

Trilogy_EOFError = rb_const_get(Trilogy, rb_intern("EOFError"));
rb_global_variable(&Trilogy_EOFError);

id_socket = rb_intern("socket");
id_host = rb_intern("host");
id_port = rb_intern("port");
Expand Down
8 changes: 7 additions & 1 deletion contrib/ruby/lib/trilogy/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SyscallError
.select { |c| c.is_a?(Class) && c < SystemCallError }
.each do |c|
errno_name = c.to_s.split('::').last
ERRORS[c::Errno] = const_set(errno_name, Class.new(c) { include Trilogy::Error })
ERRORS[c::Errno] = const_set(errno_name, Class.new(c) { include Trilogy::ConnectionError })
end

ERRORS.freeze
Expand Down Expand Up @@ -112,7 +112,13 @@ class SSLError < BaseError
include ConnectionError
end

# Raised on attempt to use connection which was explicitly closed by the user
class ConnectionClosed < IOError
include ConnectionError
end

# Occurrs when a socket read or write returns EOF or when an operation is
# attempted on a socket which previously encountered an error.
class EOFError < BaseConnectionError
end
end
2 changes: 1 addition & 1 deletion contrib/ruby/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def create_test_table(client)
def assert_raises_connection_error(&block)
err = assert_raises(Trilogy::Error, &block)

if err.is_a?(Trilogy::QueryError)
if err.is_a?(Trilogy::EOFError)
assert_includes err.message, "TRILOGY_CLOSED_CONNECTION"
elsif err.is_a?(Trilogy::SSLError)
assert_includes err.message, "unexpected eof while reading"
Expand Down

0 comments on commit 6c6cd9f

Please sign in to comment.