Error Handling

These types are used to deal with error conditions.


Topics

enum RbError

An error raised by the RubyGateway module. Ruby exceptions generate RbError.rubyException(_:), unusual Ruby flow control generates RbError.rubyJump(_:), and the other cases correspond to error conditions encountered by the Swift software.

Declaration
Swift
public enum RbError: Error

extension RbError: CustomStringConvertible

struct RbException

A Ruby exception.

This provides some convenience methods on top of the underlying Exception object. RubyGateway does not throw these directly, it always wraps them in an RbError instance.

Create and throw one of these to raise a Ruby exception from a block implemented in Swift by an RbBlockCallback.

Declaration
Swift
public struct RbException: CustomStringConvertible, Error

struct RbFailableAccess

A way to call Ruby methods and so on with a different error-handling style to the try/catch approach of RbObjectAccess: the methods return nil in failure cases instead of throwing errors. Get hold of this failable interface using RbObject.failable or RbGateway.failable:

if let classObj = Ruby.failable.getClass("Mod::Service"),
   let instance = classObj.failable.call("new"),
   let result = instance.failable.get("summary") {
   print(result)
}

This interface makes it easier to ignore errors especially on setters where there is often no apparent need to check the result. This may be construed as a feature; I’m not really convinced it pulls its weight over try?.

If any methods in this interface do return nil then an RbError has been raised and suppressed. You can access the most recent RbErrors whether suppressed or not via RbError.history.

Declaration
Swift
public struct RbFailableAccess