Exception: Danger::DSLError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/danger/danger_core/standard_error.rb

Overview

Wraps an exception raised by a DSL file in order to show to the user the contents of the line that raised the exception.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, dsl_path, backtrace, contents = nil) ⇒ DSLError



29
30
31
32
33
34
# File 'lib/danger/danger_core/standard_error.rb', line 29

def initialize(description, dsl_path, backtrace, contents = nil)
  @description = description
  @dsl_path    = dsl_path
  @backtrace   = backtrace
  @contents    = contents
end

Instance Attribute Details

#backtraceException (readonly)



24
25
26
# File 'lib/danger/danger_core/standard_error.rb', line 24

def backtrace
  @backtrace
end

#descriptionString (readonly)



15
16
17
# File 'lib/danger/danger_core/standard_error.rb', line 15

def description
  @description
end

#dsl_pathString (readonly)



19
20
21
# File 'lib/danger/danger_core/standard_error.rb', line 19

def dsl_path
  @dsl_path
end

Instance Method Details

#contentsString



39
40
41
42
43
# File 'lib/danger/danger_core/standard_error.rb', line 39

def contents
  @contents ||= begin
    dsl_path && File.exist?(dsl_path) && File.read(dsl_path)
  end
end

#messageString

The message of the exception reports the content of podspec for the line that generated the original exception.

Examples:

Output


Invalid podspec at `RestKit.podspec` - undefined method
`exclude_header_search_paths=' for #<Pod::Specification for
`RestKit/Network (0.9.3)`>

    from spec-repos/master/RestKit/0.9.3/RestKit.podspec:36
    -------------------------------------------
        # because it would break: #import <CoreData/CoreData.h>
 >      ns.exclude_header_search_paths = 'Code/RestKit.h'
      end
    -------------------------------------------


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/danger/danger_core/standard_error.rb', line 63

def message
  @message ||= begin
    trace_line, description = parse_line_number_from_description

    m = "\n[!] "
    m << description
    m << ". Updating the Danger gem might fix the issue.\n"
    m = m.red if m.respond_to?(:red)

    return m unless backtrace && dsl_path && contents

    trace_line = backtrace.find { |l| l.include?(dsl_path.to_s) } || trace_line
    return m unless trace_line
    line_numer = trace_line.split(':')[1].to_i - 1
    return m unless line_numer

    lines      = contents.lines
    indent     = ' #  '
    indicator  = indent.tr('#', '>')
    first_line = line_numer.zero?
    last_line  = (line_numer == (lines.count - 1))

    m << "\n"
    m << "#{indent}from #{trace_line.gsub(/:in.*$/, '')}\n"
    m << "#{indent}-------------------------------------------\n"
    m << "#{indent}#{lines[line_numer - 1]}" unless first_line
    m << "#{indicator}#{lines[line_numer]}"
    m << "#{indent}#{lines[line_numer + 1]}" unless last_line
    m << "\n" unless m.end_with?("\n")
    m << "#{indent}-------------------------------------------\n"
  end
end