# 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 extend self def try(name) begin yield rescue Lux.page.status 500 log($!) inline('%s (%s)' % [name, $!.class]) end end def render data %[Server error (#{Lux.page.status})
#{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 ||= $! dmp = [[], []] o.backtrace.each do |line| line = line.sub(Lux.root.to_s, '.') dmp[line.include?('/app/') ? 0 : 1].push line end dmp[0] = dmp[0].map { |_| _ = _.split(':', 3); '%s - %s - %s' % _ } name ||= 'Undefined name' msg = $!.to_s.gsub('","',%[",\n "]).gsub('<','<') %[
#{name}\n\n#{msg}\n\n#{dmp[0].join("\n")}\n\n#{dmp[1].join("\n")}
] 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 end