Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/errgonomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
module Errgonomic
class Error < StandardError; end

class TypeError < ::TypeError; end

class NotPresentError < Error; end

class TypeMismatchError < Error; end
class TypeMismatchError < TypeError; end

class UnwrapError < Error
def initialize(msg, value)
Expand All @@ -42,6 +44,8 @@ class ResultRequiredError < Error; end

class NotComparableError < StandardError; end

class SerializeError < TypeError; end

# A little bit of control over how pedantic we are in our runtime type checks.
def self.give_me_ambiguous_downstream_errors?
@give_me_ambiguous_downstream_errors || true
Expand Down
20 changes: 20 additions & 0 deletions lib/errgonomic/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,26 @@ def zip_with(other, &block)
Some(other)
end

# Refuse to serialize an unwrapped Option as a String. Options must be
# correctly handled to access their inner value.
#
# @example
# None().to_s # => raise Errgonomic::SerializeError, "cannot serialize an unwrapped Option"
def to_s
raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Option'
end

# Refuse to serialize an unwrapped Option as JSON. Not only should we
# require that options be correctly handled to access their inner value,
# but without this we will get undefined structures from default
# Object#to_json implementations.
#
# @example
# None().to_json # => raise Errgonomic::SerializeError, "cannot serialize an unwrapped Option"
def to_json(*_args)
raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Option'
end

# filter
# xor
# insert
Expand Down
8 changes: 0 additions & 8 deletions lib/errgonomic/rails/active_record_optional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,12 @@ class Some
delegate :marked_for_destruction?, to: :value
delegate :persisted?, to: :value
delegate :touch_later, to: :value

def to_s
raise "Attempted to convert Some to String, please use Option API to safely work with internal value -- #{value}"
end
end

class None
def nil?
true
end

def to_s
raise 'Cannot convert None to String - please use Option API to safely work with internal value'
end
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/errgonomic/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,28 @@ def map(&block)
@value = block.call(value)
self
end

# Refuse to serialize an unwrapped Result as a String. Results must be
# correctly handled to access their inner value.
#
# @example
# Ok("").to_s # => raise Errgonomic::SerializeError, "cannot serialize an unwrapped Result"
# Err("").to_s # => raise Errgonomic::SerializeError, "cannot serialize an unwrapped Result"
def to_s
raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Result'
end

# Refuse to serialize an unwrapped Result as JSON. Not only should we
# require that Results be correctly handled to access their inner value,
# but without this we will get undefined structures from default
# Object#to_json implementations.
#
# @example
# Ok("").to_json # => raise Errgonomic::SerializeError, "cannot serialize an unwrapped Result"
# Err("").to_json # => raise Errgonomic::SerializeError, "cannot serialize an unwrapped Result"
def to_json(*_args)
raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Result'
end
end

# The Ok variant.
Expand Down