Exception: Lux::Error

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

Overview

default error handler for lux e = Lux::Error.new 404 e.code => 404 e.message => ‘Not Found’

e = Lux::Error.not_found(‘foo’) e.code => 404 e.message => foo

Direct Known Subclasses

AutoRaise

Defined Under Namespace

Classes: AutoRaise

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code_num, message = nil) ⇒ Error

Returns a new instance of Error.



173
174
175
176
# File 'lib/lux/error/error.rb', line 173

def initialize code_num, message=nil
  self.code = code_num
  @message = message || CODE_LIST[code_num][:name]
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



171
172
173
# File 'lib/lux/error/error.rb', line 171

def message
  @message
end

Class Method Details

.inline(name, error = nil) ⇒ Object

render error inline or break in production



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/lux/error/error.rb', line 114

def inline name, error=nil
  error ||= $!

  unless Lux.config(:dump_errors)
    key = log error
    render "Lux inline error: %s\n\nkey: %s" % [error.message, key]
  end

  name ||= 'Undefined name'
  msg    = error.message.to_s.gsub('","',%[",\n "]).gsub('<','&lt;')

  dmp = split_backtrace error

  dmp[0] = dmp[0].map { |_| _ = _.split(':', 3); '<b>%s</b> - %s - %s' % _ }

  log error

  <<~TEXT
    <pre style="color:red; background:#eee; padding:10px; font-family:'Lucida Console'; line-height:15pt; font-size:11pt;">
    <b style="font-size:110%;">#{name}</b>

    <b>#{error}: #{msg}</b>

    #{dmp[0].join("\n")}

    #{dmp[1].join("\n")}
    </pre>
  TEXT
end

.log(error) ⇒ Object



150
151
152
# File 'lib/lux/error/error.rb', line 150

def log error
  Lux.config.error_logger.call error
end

.render(text, status = 500) ⇒ Object

template to show full error page



107
108
109
110
111
# File 'lib/lux/error/error.rb', line 107

def render text, status=500
  Lux.current.response.status status
  Lux.current.response.body Lux.config.server_error_template.call(text)
  throw :done
end

.report(code, msg = nil) ⇒ Object



144
145
146
147
148
# File 'lib/lux/error/error.rb', line 144

def report code, msg=nil
  e = Integer === code ? Lux::Error.new(code) : Lux::Error.send(code)
  e.message = msg if msg
  raise e
end

.split_backtrace(error) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lux/error/error.rb', line 154

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

  root = Lux.root.to_s

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

  dmp
end

Instance Method Details

#codeObject



178
179
180
181
# File 'lib/lux/error/error.rb', line 178

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

#code=(num) ⇒ Object



183
184
185
186
187
# File 'lib/lux/error/error.rb', line 183

def code= num
  @code = num.to_i

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

#renderObject



189
190
191
# File 'lib/lux/error/error.rb', line 189

def render
  self.class.render message, code
end