Exception: Lux::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/lux/error/error.rb

Direct Known Subclasses

AutoRaise

Defined Under Namespace

Classes: AutoRaise

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Error

Returns a new instance of Error.



225
226
227
228
229
230
231
# File 'lib/lux/error/error.rb', line 225

def initialize *args
  if args.first.is_a?(Integer)
    self.code    = args.shift
  end

  self.message = args.shift || name
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



223
224
225
# File 'lib/lux/error/error.rb', line 223

def message
  @message
end

Class Method Details

.clear_screen(time = 0) ⇒ Object

clear osx screen :)



161
162
163
164
165
166
167
# File 'lib/lux/error/error.rb', line 161

def clear_screen time = 0
  last = (Thread.current[:_lux_clear_screen] ||= 1.day.ago)
  if last < Time.now - time
    Thread.current[:_lux_clear_screen] = Time.now
    print "\e[H\e[2J\e[3J"
  end
end

.inline(object, msg = nil) ⇒ Object

render error inline



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/lux/error/error.rb', line 137

def inline object, msg = nil
  error, message = if object.is_a?(String)
    [nil, object]
  else
    [object, object.message]
  end

  error_key = error ? log(error) : nil
  message = message.to_s.gsub('","',%[",\n "]).gsub('<','&lt;')

  HtmlTag.pre(class: 'lux-inline-error', style: 'background: #fff; margin-top: 10px; padding: 10px; font-size: 14px; border: 2px solid #600; line-height: 20px;') do |n|
    n.h3 '%s : %s' % [error.class, message]
    n.p msg if msg
    n.p 'Key: %s' % error_key if error_key
    n.p 'Description: %s' % error.description if error && error.respond_to?(:description) && error.description

    if error && Lux.env.show_errors?
      n.hr
      n.push mark_backtrace(error, html: true).join("\n")
    end
  end
end

.log(err) ⇒ Object



214
215
216
217
218
# File 'lib/lux/error/error.rb', line 214

def log err
  if Lux.config.error_logger
    Lux.config.error_logger.call err
  end
end

.mark_backtrace(error, html: false) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/lux/error/error.rb', line 184

def mark_backtrace error, html: false
  return ['no backtrace present'] unless error && error.backtrace

  root = Lux.root.to_s

  error
    .backtrace
    .map do |line|
      path = line.split(':in').first
      path = path.sub(/^\.\//, root+'/')

      edit = html ? %[ &bull; <a href="subl://open?url=file:/#{path}">edit</a>] : ''
      line = line.sub(root, '.')
      (line[0,1] != '/' ? (html ? line.tag(:b) : line) : line) + edit
    end
end

.render(error) ⇒ Object

template to show full error page



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lux/error/error.rb', line 113

def render error
  error = StandardError.new(error) if error.is_a?(String)

  code = error.respond_to?(:code) ? error.code : 500
  Lux.current.response.status code

  Lux.current.response.body(
    HtmlTag.html do |n|
      n.head do |n|
        n.title 'Lux error'
      end
      n.body style: "margin: 20px 20px 20px 140px; background-color:#fdd; font-size: 14pt; font-family: sans-serif;" do |n|
        n.img src: "https://i.imgur.com/Zy7DLXU.png", style: "width: 100px; position: absolute; margin-left: -120px;"
        n.h4 do |n|
          n.push %[HTTP Error &mdash; <a href="https://httpstatuses.com/#{code}" target="http_error">#{code}</a>]
          n.push %[ &dash; #{error.name}] if error.respond_to?(:name)
        end
        n.push inline error
      end
    end
  )
end

.screen(error) ⇒ Object

show in stdout



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/lux/error/error.rb', line 202

def screen error
  return unless Lux.env.show_errors?

  data = split_backtrace(error)
  if error.class == Lux::Error
    Lux.info "Lux error: #{error.message} (#{error.code}) - #{data[1][0]}"
  else
    data[2] = data[2][0,5]
    ap data
  end
end

.split_backtrace(error) ⇒ Object

prepare backtrace for better render



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/lux/error/error.rb', line 170

def split_backtrace error
  # split app log rest of the log
  dmp = [[error.class, error.message], [], []]

  root = Lux.root.to_s

  (error.backtrace || caller).each do |line|
    line = line.sub(root, '.')
    dmp[line[0,1] == '.' ? 1 : 2].push line
  end

  dmp
end

Instance Method Details

#codeObject



233
234
235
236
# File 'lib/lux/error/error.rb', line 233

def code
  # 400 is a default
  @code || 400
end

#code=(num) ⇒ Object



242
243
244
245
246
# File 'lib/lux/error/error.rb', line 242

def code= num
  @code = num.to_i

  raise 'Status code %s not found' % @code unless CODE_LIST[@code]
end

#nameObject



238
239
240
# File 'lib/lux/error/error.rb', line 238

def name
  CODE_LIST[code][:name]
end