Skip to content

Commit 24025bb

Browse files
committed
Replace Errno with dedicated Error types.
Errno is no longer a subclass of Exception in Crystal, instead it is an Enum and becomes more of an implementation detail. This set of changes doesn't go as far as it could for detecting the Errno and making appropriate types, but it does get things compiling again on the latest version of Crystal.
1 parent eceb2b5 commit 24025bb

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/type_checker.cr

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
require "./libmagic/magic"
44

55
module Magic
6-
class Errno < ::Errno
7-
def initialize(msg, checker)
8-
msg = <<-ERR
9-
#{msg}: libmagic says "#{(err = LibMagic.error(checker)) && String.new(err)}"
10-
ERR
11-
super msg, Errno.value
12-
end
6+
class LibMagicError < RuntimeError
7+
end
8+
9+
class NullPointerError < RuntimeError
10+
end
11+
12+
class ReadError < IO::Error
1313
end
1414

1515
# A TypeChecker checks the Magic database on the system for a given file,
@@ -69,9 +69,9 @@ module Magic
6969
limit_settings : Hash(Limit, Int32)? = nil)
7070
@checker = LibMagic.open @options
7171
if @checker.null?
72-
raise ::Errno.new(
72+
raise NullPointerError.from_errno(
7373
"opening the magic cookie (#{LibMagic.error(@checker)})",
74-
::Errno.value,
74+
Errno.value,
7575
)
7676
end
7777
@new_options = @options
@@ -96,7 +96,7 @@ module Magic
9696
return if chkr === @checker
9797
LibMagic.close @checker
9898
@checker = chkr
99-
raise error("opening the magic cookie") if @checker.null?
99+
raise NullPointerError.new "opening the magic cookie" if @checker.null?
100100
LibMagic.open @checker
101101
LibMagic.load @checker, @db_files
102102
@checker
@@ -135,7 +135,10 @@ module Magic
135135
end
136136

137137
def error(msg)
138-
Errno.new msg, @checker
138+
msg = <<-ERR
139+
#{msg}: libmagic says "#{(err = LibMagic.error(checker)) && String.new(err)}"
140+
ERR
141+
LibMagicError.new msg
139142
end
140143

141144
# Get the filetype "of" the given file, passing `opts` to `libmagic(2)`.
@@ -193,9 +196,9 @@ module Magic
193196
if (some_bytes.nil? || some_bytes.empty?)
194197
this.read (some_bytes = Bytes.new(32))
195198
end
196-
raise ::Errno.new "reading bytes, got #{some_bytes.inspect}" if some_bytes.nil? || some_bytes.empty?
199+
raise ReadError.new "reading bytes, got #{some_bytes.inspect}" if some_bytes.nil? || some_bytes.empty?
197200
ptr = LibMagic.buffer(checker, some_bytes, some_bytes.size)
198-
String.new ptr || raise error "checking filetype of given byte sequence"
201+
String.new ptr || raise NullPointerError.new "checking filetype of given byte sequence"
199202
end
200203

201204
# same as `#of()`

0 commit comments

Comments
 (0)