Sha256: 942b0e2d3bb243e4ecb8faa14da27674b25c710941ace01c46289c4f1794c931

Contents?: true

Size: 1.4 KB

Versions: 245

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

245 entries across 245 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.10417 lib/types/private/casts.rb
sorbet-runtime-0.5.10416 lib/types/private/casts.rb
sorbet-runtime-0.5.10415 lib/types/private/casts.rb
sorbet-runtime-0.5.10413 lib/types/private/casts.rb
sorbet-runtime-0.5.10409 lib/types/private/casts.rb
sorbet-runtime-0.5.10404 lib/types/private/casts.rb
sorbet-runtime-0.5.10400 lib/types/private/casts.rb
sorbet-runtime-0.5.10399 lib/types/private/casts.rb
sorbet-runtime-0.5.10398 lib/types/private/casts.rb
sorbet-runtime-0.5.10393 lib/types/private/casts.rb
sorbet-runtime-0.5.10390 lib/types/private/casts.rb
sorbet-runtime-0.5.10386 lib/types/private/casts.rb
sorbet-runtime-0.5.10382 lib/types/private/casts.rb
sorbet-runtime-0.5.10381 lib/types/private/casts.rb
sorbet-runtime-0.5.10377 lib/types/private/casts.rb
sorbet-runtime-0.5.10374 lib/types/private/casts.rb
sorbet-runtime-0.5.10370 lib/types/private/casts.rb
sorbet-runtime-0.5.10365 lib/types/private/casts.rb
sorbet-runtime-0.5.10354 lib/types/private/casts.rb
sorbet-runtime-0.5.10353 lib/types/private/casts.rb