Sha256: 0be64c3ba5a549d3f0262ce5370032aa247653819a0cf1d6dcc891a9352f8718

Contents?: true

Size: 1.4 KB

Versions: 36

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true
# typed: false

module T::Private
  module Casts
    def self.cast(value, type, cast_method)
      begin
        error = T::Utils.coerce(type).error_message_for_obj(value)
        return value unless error

        caller_loc = T.must(caller_locations(2..2)).first

        suffix = "Caller: #{T.must(caller_loc).path}:#{T.must(caller_loc).lineno}"

        raise TypeError.new("#{cast_method}: #{error}\n#{suffix}")
      rescue TypeError => e # raise into rescue to ensure e.backtrace is populated
        T::Configuration.inline_type_error_handler(e, {kind: cast_method, value: value, type: type})
        value
      end
    end

    # there's a lot of shared logic with the above one, but factoring
    # it out like this makes it easier to hopefully one day delete
    # this one
    def self.cast_recursive(value, type, cast_method)
      begin
        error = T::Utils.coerce(type).error_message_for_obj_recursive(value)
        return value unless error

        caller_loc = T.must(caller_locations(2..2)).first

        suffix = "Caller: #{T.must(caller_loc).path}:#{T.must(caller_loc).lineno}"

        raise TypeError.new("#{cast_method}: #{error}\n#{suffix}")
      rescue TypeError => e # raise into rescue to ensure e.backtrace is populated
        T::Configuration.inline_type_error_handler(e, {kind: cast_method, value: value, type: type})
        value
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.10549 lib/types/private/casts.rb
sorbet-runtime-0.5.10546 lib/types/private/casts.rb
sorbet-runtime-0.5.10545 lib/types/private/casts.rb
sorbet-runtime-0.5.10539 lib/types/private/casts.rb
sorbet-runtime-0.5.10535 lib/types/private/casts.rb
sorbet-runtime-0.5.10528 lib/types/private/casts.rb
sorbet-runtime-0.5.10526 lib/types/private/casts.rb
sorbet-runtime-0.5.10520 lib/types/private/casts.rb
sorbet-runtime-0.5.10517 lib/types/private/casts.rb
sorbet-runtime-0.5.10515 lib/types/private/casts.rb
sorbet-runtime-0.5.10514 lib/types/private/casts.rb
sorbet-runtime-0.5.10511 lib/types/private/casts.rb
sorbet-runtime-0.5.10508 lib/types/private/casts.rb
sorbet-runtime-0.5.10501 lib/types/private/casts.rb
sorbet-runtime-0.5.10499 lib/types/private/casts.rb
sorbet-runtime-0.5.10488 lib/types/private/casts.rb
sorbet-runtime-0.5.10486 lib/types/private/casts.rb
sorbet-runtime-0.5.10484 lib/types/private/casts.rb
sorbet-runtime-0.5.10482 lib/types/private/casts.rb
sorbet-runtime-0.5.10481 lib/types/private/casts.rb