2012-01-15 14:02:53 +00:00
|
|
|
require 'ffi'
|
|
|
|
|
|
|
|
require 'mach/functions'
|
|
|
|
|
|
|
|
module Mach
|
|
|
|
class Error < StandardError
|
|
|
|
class INVALID_ADDRESS < Error; end
|
|
|
|
class PROTECTION_FAILURE < Error; end
|
|
|
|
class NO_SPACE < Error; end
|
|
|
|
class INVALID_ARGUMENT < Error; end
|
|
|
|
class FAILURE < Error; end
|
2012-02-02 03:32:08 +00:00
|
|
|
class ABORTED < Error; end
|
|
|
|
class INVALID_NAME < Error; end
|
|
|
|
class OPERATION_TIMED_OUT < Error; end
|
2012-01-15 14:02:53 +00:00
|
|
|
|
|
|
|
include Functions
|
|
|
|
|
|
|
|
def self.new(msg, errno)
|
|
|
|
klass = case errno
|
|
|
|
when 1; then INVALID_ADDRESS
|
|
|
|
when 2; then PROTECTION_FAILURE
|
|
|
|
when 3; then NO_SPACE
|
|
|
|
when 4; then INVALID_ARGUMENT
|
|
|
|
when 5; then FAILURE
|
2012-02-02 03:32:08 +00:00
|
|
|
when 14; then ABORTED
|
2012-01-15 14:02:53 +00:00
|
|
|
when 15; then INVALID_NAME
|
2012-02-02 03:32:08 +00:00
|
|
|
when 49; then OPERATION_TIMED_OUT
|
2012-01-15 14:02:53 +00:00
|
|
|
else FAILURE
|
|
|
|
end
|
|
|
|
|
|
|
|
e = klass.allocate
|
|
|
|
e.send(:initialize, msg, errno)
|
|
|
|
e
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :errno
|
|
|
|
|
|
|
|
def initialize(msg, errno)
|
|
|
|
super(msg)
|
|
|
|
@errno = errno
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"#{super}: #{error_string(errno)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# NOTE: api does not say this string must be freed; assuming it
|
|
|
|
# does not
|
|
|
|
def error_string(errno)
|
|
|
|
ptr = mach_error_string(errno)
|
|
|
|
ptr.null? ? nil : ptr.read_string()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|