Class: Klenod::Build::ResolveError

Inherits:
Error
  • Object
show all
Defined in:
lib/klenod/build/errors.rb

Constant Summary collapse

REASONS =
[:incorrect_case, :not_found].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, unresolved_path: nil, dependency: nil, importer_id: nil, reason: nil, requested_specifier: nil, suggestions: []) ⇒ ResolveError

Returns a new instance of ResolveError.



14
15
16
17
18
19
20
21
22
23
# File 'lib/klenod/build/errors.rb', line 14

def initialize(message = nil, unresolved_path: nil, dependency: nil, importer_id: nil, reason: nil, requested_specifier: nil, suggestions: [])
  @unresolved_path = unresolved_path
  @dependency = dependency
  @importer_id = importer_id
  @reason = reason
  @requested_specifier = requested_specifier
  @suggestions = suggestions.freeze

  super(message || resolution_message)
end

Instance Attribute Details

#dependencyObject (readonly)

Returns the value of attribute dependency.



12
13
14
# File 'lib/klenod/build/errors.rb', line 12

def dependency
  @dependency
end

#importer_idObject (readonly)

Returns the value of attribute importer_id.



12
13
14
# File 'lib/klenod/build/errors.rb', line 12

def importer_id
  @importer_id
end

#reasonObject (readonly)

Returns the value of attribute reason.



12
13
14
# File 'lib/klenod/build/errors.rb', line 12

def reason
  @reason
end

#requested_specifierObject (readonly)

Returns the value of attribute requested_specifier.



12
13
14
# File 'lib/klenod/build/errors.rb', line 12

def requested_specifier
  @requested_specifier
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



12
13
14
# File 'lib/klenod/build/errors.rb', line 12

def suggestions
  @suggestions
end

#unresolved_pathObject (readonly)

Returns the value of attribute unresolved_path.



12
13
14
# File 'lib/klenod/build/errors.rb', line 12

def unresolved_path
  @unresolved_path
end

Instance Method Details

#imported_byObject



41
42
43
44
45
46
47
# File 'lib/klenod/build/errors.rb', line 41

def imported_by
  if source_location&.line
    "#{display_importer(source_location.path)}:#{source_location.line}"
  else
    display_importer(dependency&.importer_id || importer_id)
  end
end

#resolution_failure?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/klenod/build/errors.rb', line 25

def resolution_failure?
  REASONS.include?(reason) && requested_specifier
end

#source_locationObject



37
38
39
# File 'lib/klenod/build/errors.rb', line 37

def source_location
  dependency&.loc
end

#titleObject



29
30
31
32
33
34
35
# File 'lib/klenod/build/errors.rb', line 29

def title
  case reason
  when :incorrect_case then "Incorrect import path casing"
  when :not_found then "Module not found"
  else self.class.name
  end
end

#with_path_prefix(prefix) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/klenod/build/errors.rb', line 73

def with_path_prefix(prefix)
  return self unless resolution_failure?

  self.class.new(
    nil,
    unresolved_path: unresolved_path,
    dependency: dependency,
    importer_id: importer_id,
    reason: reason,
    requested_specifier: prefixed_path(prefix, requested_specifier),
    suggestions: suggestions.map { prefixed_path(prefix, it) }
  )
end

#with_resolution_context(dependency:, importer_id:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/klenod/build/errors.rb', line 49

def with_resolution_context(dependency:, importer_id:)
  unless resolution_failure?
    return self if self.dependency

    return self.class.new(
      contextual_message(dependency, importer_id),
      unresolved_path: unresolved_path,
      dependency: dependency,
      importer_id: importer_id
    )
  end

  replacements = suggestions.map { replacement_specifier(it, dependency) }
  self.class.new(
    nil,
    unresolved_path: unresolved_path,
    dependency: dependency,
    importer_id: importer_id,
    reason: reason,
    requested_specifier: dependency.specifier.to_s,
    suggestions: replacements
  )
end