# frozen_string_literal: true # 400: for bad parameter request or similar BadRequestError ||= Class.new(StandardError) # 401: for unauthorized access UnauthorizedError ||= Class.new(StandardError) # 403: for unalloed access ForbidenError ||= Class.new(StandardError) # 404: for not found pages NotFoundError ||= Class.new(StandardError) # 503: for too many requests at the same time RateLimitError ||= Class.new(StandardError) module Lux::Error OUT_OF_PROCESS_ERROR_PATH = Lux.root.join('tmp/request-error.txt') extend self def try(name) begin yield rescue log($!) inline('%s (%s)' % [name, $!.class]) end end def render data %[
#{data.gsub('<','<')}] end def show(desc=nil) Lux.page.status(500) data = "Lux #{Lux.page.status} error\n\n#{desc}" Lux.page.body! render(data) end def inline(name=nil, o=nil) o ||= $! trace = o.backtrace.select{ |el| el.index(Lux.root.to_s) }.map{ |el| el.split(Lux.root.to_s, 2)[1] }.map{ |el| "- #{el}" }.join("\n") msg = $!.to_s.gsub('","',%[",\n "]).gsub('<','<') %[
#{name || 'Undefined name'}\n\n#{msg}\n\n#{trace}] end def log(exception) return if Lux.env == 'test' return unless Lux.page history = exception.backtrace.reject{ |el| el.index('/gems/') }.map{ |el| el.sub(Lux.root.to_s, '') }.join("\n") data = '%s in %s (user: %s)' % [exception.class, Lux.page.request.url, (Lux.page.var.user.email rescue 'guest')] data = [data, exception.message, history].join("\n\n") key = Digest::SHA1.hexdigest exception.backtrace.first.split(' ').first folder = Lux.root.join('log/exceptions').to_s Dir.mkdir(folder) unless Dir.exists?(folder) folder += "/#{exception.class.to_s.tableize.gsub('/','-')}" Dir.mkdir(folder) unless Dir.exists?(folder) File.write("#{folder}/#{key}.txt", data) end # if some other process as asset complation raises error # use this method to show errors def log_out_of_process_error(data) return unless Lux.config(:show_server_errors) File.write OUT_OF_PROCESS_ERROR_PATH, data end end