Skip to content

Commit

Permalink
Add the Errno::Error exception class
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Oct 10, 2019
1 parent 75881e2 commit b142dd7
Show file tree
Hide file tree
Showing 29 changed files with 512 additions and 238 deletions.
2 changes: 1 addition & 1 deletion spec/std/http/server/request_processor_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe HTTP::Server::RequestProcessor do

it "handles Errno" do
processor = HTTP::Server::RequestProcessor.new { }
input = RaiseErrno.new(Errno::ECONNRESET)
input = RaiseErrno.new(Errno::ECONNRESET.value)
output = IO::Memory.new
processor.process(input, output)
output.rewind.gets_to_end.empty?.should be_true
Expand Down
4 changes: 2 additions & 2 deletions spec/support/errno.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# *message* should usually only validate parts of the error message added by
# Crystal.
def expect_raises_errno(errno, message = nil, file = __FILE__, line = __LINE__, end_line = __END_LINE__)
error = expect_raises(Errno, message, file: file, line: line) do
error = expect_raises(Errno::Error, message, file: file, line: line) do
yield
end
error.errno.should eq(errno), file: file, line: line
error.class.value.should eq(errno.value), file: file, line: line
error
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/cache_dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module Crystal
begin
Dir.mkdir_p(candidate)
return @dir = candidate
rescue Errno
rescue Errno::Error
# Try next one
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module Crystal
private def self.runtime_libc
ldd_version = String.build do |io|
Process.run("ldd", {"--version"}, output: io, error: io)
rescue Errno
rescue Errno::Error
# In case of an error (eg. `ldd` not available), we assume it's gnu.
return "gnu"
end
Expand Down
6 changes: 3 additions & 3 deletions src/crystal/system/unix/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module Crystal::System::File
if ret == 0
FileInfo.new(stat)
else
if {Errno::ENOENT, Errno::ENOTDIR}.includes? Errno.value
case Errno.value
when Errno::ENOENT.value, Errno::ENOTDIR.value
return nil
else
raise Errno.new("Unable to get info for '#{path.inspect_unquoted}'")
Expand Down Expand Up @@ -122,8 +123,7 @@ module Crystal::System::File
end
end

Errno.value = Errno::ENAMETOOLONG
raise Errno.new("readlink")
raise Errno::ENAMETOOLONG.new("readlink")
end

def self.rename(old_filename, new_filename)
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Crystal::System::FileDescriptor
private def unbuffered_write(slice : Bytes)
evented_write(slice, "Error writing file") do |slice|
LibC.write(@fd, slice, slice.size).tap do |return_code|
if return_code == -1 && Errno.value == Errno::EBADF
if return_code == -1 && Errno.value == Errno::EBADF.value
raise IO::Error.new "File not open for writing"
end
end
Expand Down Expand Up @@ -112,7 +112,7 @@ module Crystal::System::FileDescriptor
private def system_close
if LibC.close(@fd) != 0
case Errno.value
when Errno::EINTR, Errno::EINPROGRESS
when Errno::EINTR.value, Errno::EINPROGRESS.value
# ignore
else
raise Errno.new("Error closing file")
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/getrandom.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module Crystal::System::Random
private def self.sys_getrandom(buf : Bytes)
loop do
read_bytes = LibC.syscall(LibC::SYS_getrandom, buf, LibC::SizeT.new(buf.size), 0)
if read_bytes < 0 && (Errno.value == Errno::EINTR || Errno.value == Errno::EAGAIN)
if read_bytes < 0 && (Errno.value == Errno::EINTR.value || Errno.value == Errno::EAGAIN.value)
Fiber.yield
else
return read_bytes
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Crystal::System::Dir

def self.open(path : String) : DirHandle
unless ::Dir.exists? path
raise Errno.new("Error opening directory #{path.inspect}", Errno::ENOENT)
raise Errno::ENOENT.new("Error opening directory #{path.inspect}")
end

DirHandle.new(LibC::INVALID_HANDLE_VALUE, to_windows_path(path + "\\*"))
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module Crystal::System::File
end

unless exists? real_path
raise Errno.new("Error resolving real path of #{path.inspect}", Errno::ENOTDIR)
raise Errno::ENOTDIR.new("Error resolving real path of #{path.inspect}")
end

real_path
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Crystal::System::FileDescriptor
until slice.empty?
bytes_written = LibC._write(@fd, slice, slice.size)
if bytes_written == -1
if Errno.value == Errno::EBADF
if Errno.value == Errno::EBADF.value
raise IO::Error.new "File not open for writing"
else
raise Errno.new("Error writing file")
Expand Down Expand Up @@ -117,7 +117,7 @@ module Crystal::System::FileDescriptor
err = nil
if LibC._close(@fd) != 0
case Errno.value
when Errno::EINTR
when Errno::EINTR.value
# ignore
else
raise Errno.new("Error closing file")
Expand Down
4 changes: 2 additions & 2 deletions src/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ class Dir
return false
end
true
rescue ex : Errno
raise Errno.new("Error determining size of '#{path}'", ex.errno)
rescue ex : Errno::Error
raise ex.class.new("Error determining size of '#{path}'", ex.value)
end

# Creates a new directory at the given path. The linux-style permission mode
Expand Down
8 changes: 4 additions & 4 deletions src/dir/glob.cr
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Dir
begin
dir = Dir.new(path || ".")
dir_stack << dir
rescue Errno
rescue Errno::Error
return
end
recurse = false
Expand All @@ -211,7 +211,7 @@ class Dir
if recurse
begin
dir = Dir.new(dir_path)
rescue Errno
rescue Errno::Error
dir_path_stack.pop
break if dir_path_stack.empty?
dir_path = dir_path_stack.last
Expand Down Expand Up @@ -293,8 +293,8 @@ class Dir
yield entry
end
end
rescue exc : Errno
raise exc unless exc.errno == Errno::ENOENT
rescue ex : Errno::Error
raise ex unless ex.value == Errno::ENOENT.value
end

private def self.read_entry(dir)
Expand Down
5 changes: 3 additions & 2 deletions src/ecr/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ buffer_name = ARGV[1]

begin
puts ECR.process_file(filename, buffer_name)
rescue ex : Errno
if {Errno::ENOENT, Errno::EISDIR}.includes?(ex.errno)
rescue ex : Errno::Error
case ex.value
when Errno::ENOENT.value, Errno::EISDIR.value
STDERR.puts ex.message
exit 1
else
Expand Down

0 comments on commit b142dd7

Please sign in to comment.