Class: KaeruEra::DatabaseReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/kaeruera/database_reporter.rb

Overview

Reporter class that inserts the error information directly into the database instead of reporting it to a web server via HTTP.

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(uri, email, application) ⇒ DatabaseReporter

Arguments:

uri

Either a Sequel::Database instance or a String treated as a URI. If a Sequel::Database instance is given, uses given database, otherwise, connects to the database specified by the URI via Sequel.

email

The KaeruEra email/login for the application.

application

The KaeruEra application name

Raises:



18
19
20
21
22
23
# File 'lib/kaeruera/database_reporter.rb', line 18

def initialize(uri, email, application)
  @db = uri.is_a?(Sequel::Database) ? uri : Sequel.connect(uri, :keep_reference=>false)
  @db.extension :pg_array, :pg_json
  @application_id, @user_id = @db[:applications].where(:user_id=>@db[:users].where(:email=>email).get(:id), :name=>application).get([:id, :user_id])
  raise(Error, "No matching application in database for #{email}/#{application}") unless @application_id
end

Instance Method Details

#report(opts = {}) ⇒ Object

If an error cannot be determined, returns false. Otherwise, inserts the error directly into the database. If an exception would be raised by this code, returns the exception instead of raising it.

Supports the same options as Reporter#report.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kaeruera/database_reporter.rb', line 32

def report(opts={})
  return false unless error = opts[:error] || $!

  h = {
    :user_id=>@user_id,
    :application_id=>@application_id,
    :error_class=>error.class.name,
    :message=>error.message.to_s,
    :backtrace=>Sequel.pg_array(error.backtrace)
  }

  if v = opts[:params]
    h[:params] = Sequel.pg_jsonb(v.to_hash)
  end
  if v = opts[:session]
    h[:session] = Sequel.pg_jsonb(v.to_hash)
  end
  if v = opts[:env]
    h[:env] = Sequel.pg_jsonb(v.to_hash)
  end

  @db[:errors].insert(h)
rescue => e
  e
end