Exception: Pakyow::Error

Inherits:
StandardError
  • Object
show all
Extended by:
Support::ClassState
Defined in:
lib/pakyow/error.rb

Overview

Base error object.

Defined Under Namespace

Classes: CLIFormatter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeError

Returns a new instance of Error.



57
58
59
60
61
# File 'lib/pakyow/error.rb', line 57

def initialize(*)
  @context = nil

  super
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



55
56
57
# File 'lib/pakyow/error.rb', line 55

def context
  @context
end

#wrapped_exceptionObject

Returns the value of attribute wrapped_exception.



55
56
57
# File 'lib/pakyow/error.rb', line 55

def wrapped_exception
  @wrapped_exception
end

Class Method Details

.build(original_error, message_type = :default, context: nil, **message_values) ⇒ Object

Wraps an error in a pakyow error instance, with additional context.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pakyow/error.rb', line 19

def build(original_error, message_type = :default, context: nil, **message_values)
  if original_error.is_a?(self)
    original_error
  else
    message = message(message_type, **message_values)
    message = original_error.message if message.empty?
    new(message).tap do |error|
      error.wrapped_exception = original_error
      error.set_backtrace(original_error.backtrace)
      error.context = context
    end
  end
end

.new_with_message(type = :default, **values) ⇒ Object

Initialize an error with a particular message.



35
36
37
# File 'lib/pakyow/error.rb', line 35

def new_with_message(type = :default, **values)
  new(message(type, **values))
end

Instance Method Details

#causeObject



63
64
65
# File 'lib/pakyow/error.rb', line 63

def cause
  wrapped_exception || super
end

#condensed_backtraceObject

Returns the backtrace without any of the framework locations, unless the error originated from the framework. Return value is as an array of strings rather than backtrace location objects.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pakyow/error.rb', line 140

def condensed_backtrace
  if project?
    project_backtrace_locations.map { |line|
      line.to_s.gsub(/^#{Pakyow.config.root}\//, "")
    }
  else
    padded_length = backtrace.map { |line|
      Support::Dependencies.library_name(line).to_s.gsub(/^pakyow-/, "")
    }.max_by(&:length).length + 3

    backtrace.map { |line|
      modified_line = Support::Dependencies.strip_path_prefix(line)
      if line.start_with?(Pakyow.config.root)
        "".rjust(padded_length) + modified_line
      elsif modified_line.start_with?("ruby")
        "ruby | ".rjust(padded_length) + modified_line.split("/", 3)[2].to_s
      else
        "#{Support::Dependencies.library_name(line).to_s.gsub(/^pakyow-/, "")} | ".rjust(padded_length) + modified_line.split("/", 2)[1].to_s
      end
    }
  end
end

#detailsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pakyow/error.rb', line 75

def details
  if project? && location = project_backtrace_locations[0]
    message = "`#{(cause || self).class}' occurred on line `#{location.lineno}' of `#{path}':"

    begin
      <<~MESSAGE
        #{message}

        #{indent_as_source(MethodSource.source_helper([path, location.lineno], location.label), location.lineno)}
      MESSAGE
    rescue StandardError
      <<~MESSAGE
        #{message}

            Error parsing source.
      MESSAGE
    end
  elsif location = (cause || self).backtrace_locations.to_a[0]
    library_name = Support::Dependencies.library_name(location.absolute_path)
    library_type = Support::Dependencies.library_type(location.absolute_path)

    occurred_in = if library_type == :pakyow || library_name.start_with?("pakyow-")
      "within the `#{library_name.split("-", 2)[1]}' framework"
    elsif library_type == :gem || library_type == :bundler
      "within the `#{library_name}' gem"
    else
      "somewhere within ruby itself"
    end

    <<~MESSAGE
      `#{(cause || self).class}' occurred outside of your project, #{occurred_in}.
    MESSAGE
  else
    <<~MESSAGE
      `#{(cause || self).class}' occurred at an unknown location.
    MESSAGE
  end
end

#nameObject



67
68
69
70
71
72
73
# File 'lib/pakyow/error.rb', line 67

def name
  Support.inflector.humanize(
    Support.inflector.underscore(
      Support.inflector.demodulize(self.class.name)
    )
  )
end

#pathObject

If the error occurred in the project, returns the relative path to where the error occurred. Otherwise returns the absolute path to where the error occurred.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pakyow/error.rb', line 118

def path
  @path ||= if project?
    Pathname.new(
      File.expand_path(project_backtrace_locations[0].absolute_path.to_s)
    ).relative_path_from(
      Pathname.new(Pakyow.config.root)
    ).to_s
  else
    File.expand_path(project_backtrace_locations[0].absolute_path.to_s)
  end
end

#project?Boolean

Returns true if the error occurred in the project.

Returns:

  • (Boolean)


132
133
134
# File 'lib/pakyow/error.rb', line 132

def project?
  File.expand_path(backtrace[0].to_s).start_with?(Pakyow.config.root)
end