-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add exception classes #110
Open
brodyhoskins
wants to merge
3
commits into
crystal-lang:master
Choose a base branch
from
brodyhoskins:add-exception-classes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,37 @@ | ||
require "socket" | ||
|
||
class MySql::Connection < DB::Connection | ||
class ConnectionError < Exception; end | ||
|
||
class PacketError < ConnectionError | ||
getter :packet | ||
|
||
def initialize(packet : ReadPacket, message) | ||
@packet = packet | ||
super(message) | ||
end | ||
end | ||
|
||
class UnexpectedPacketError < PacketError | ||
getter :status | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would stick with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm admittedly inexperience with Crystal so I'll take the style advice, see 7fadad9. |
||
|
||
def initialize(packet, status : UInt8) | ||
@status = status | ||
super(packet, "unexpected packet #{status}") | ||
end | ||
end | ||
|
||
class UnexpectedPacketValueError < ConnectionError | ||
getter :attribute, :value | ||
|
||
def initialize(attribute : String, value : UInt64) | ||
@attribute = attribute | ||
@value = value | ||
|
||
super("unexpected value for #{attribute}: #{value}") | ||
end | ||
end | ||
|
||
record Options, | ||
host : String, | ||
port : Int32, | ||
|
@@ -9,7 +40,7 @@ class MySql::Connection < DB::Connection | |
initial_catalog : String?, | ||
charset : String do | ||
def self.from_uri(uri : URI) : Options | ||
host = uri.hostname || raise "no host provided" | ||
host = uri.hostname || raise ConnectionError.new("no host provided") | ||
port = uri.port || 3306 | ||
username = uri.user | ||
password = uri.password | ||
|
@@ -44,7 +75,7 @@ class MySql::Connection < DB::Connection | |
end | ||
|
||
read_ok_or_err do |packet, status| | ||
raise "packet #{status} not implemented" | ||
raise NotImplementedError.new("packet #{status} not implemented") | ||
end | ||
rescue IO::Error | ||
raise DB::ConnectionRefused.new | ||
|
@@ -117,13 +148,13 @@ class MySql::Connection < DB::Connection | |
# :nodoc: | ||
def handle_err_packet(packet) | ||
8.times { packet.read_byte! } | ||
raise packet.read_string | ||
raise ConnectionError.new(packet.read_string) | ||
end | ||
|
||
# :nodoc: | ||
def raise_if_err_packet(packet) | ||
raise_if_err_packet(packet) do |status| | ||
raise "unexpected packet #{status}" | ||
raise UnexpectedPacketError.new(packet, status) | ||
end | ||
end | ||
|
||
|
@@ -152,14 +183,14 @@ class MySql::Connection < DB::Connection | |
name = packet.read_lenenc_string | ||
org_name = packet.read_lenenc_string | ||
next_length = packet.read_lenenc_int # length of fixed-length fields, always 0x0c | ||
raise "Unexpected next_length value: #{next_length}." unless next_length == 0x0c | ||
raise UnexpectedPacketValueError.new("next_length", next_length) unless next_length == 0x0c | ||
character_set = packet.read_fixed_int(2).to_u16! | ||
column_length = packet.read_fixed_int(4).to_u32! | ||
column_type = packet.read_fixed_int(1).to_u8! | ||
flags = packet.read_fixed_int(2).to_u16! | ||
decimal = packet.read_fixed_int(1).to_u8! | ||
filler = packet.read_fixed_int(2).to_u16! # filler [00] [00] | ||
raise "Unexpected filler value #{filler}" unless filler == 0x0000 | ||
raise UnexpectedPacketValueError.new("filler", filler) unless filler == 0x0000 | ||
|
||
target << ColumnSpec.new(catalog, schema, table, org_table, name, org_name, character_set, column_length, column_type, flags, decimal) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think ReadPacket should be exposed, it's an internal implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check out the changes on c14f4e6; I'm hoping it addresses your concern.