Skip to content
Browse files

enable sending to new JSON API (#163)

  • Loading branch information...
1 parent 2aa1cbc commit 69cba659e81b159d318973239b1c12f490e7c701 @shime shime committed Jan 8, 2013
Showing with 127 additions and 0 deletions.
  1. +2 −0 airbrake.gemspec
  2. +41 −0 lib/airbrake/notice.rb
  3. +50 −0 resources/airbrake_3_0.json
  4. +1 −0 test/helper.rb
  5. +33 −0 test/notice_test.rb
View
2 airbrake.gemspec
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("builder")
s.add_runtime_dependency("activesupport")
+ s.add_runtime_dependency("json")
s.add_development_dependency("bourne", ">= 1.0")
s.add_development_dependency("cucumber-rails","~> 1.1.1")
@@ -29,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency("rspec-rails")
s.add_development_dependency("sinatra")
s.add_development_dependency("girl_friday")
+ s.add_development_dependency("json-schema")
s.authors = ["Airbrake"]
s.email = %q{support@airbrake.io}
View
41 lib/airbrake/notice.rb
@@ -209,6 +209,47 @@ def to_xml
xml.to_s
end
+ def to_json
+ {
+ 'notifier' => {
+ 'name' => 'airbrake',
+ 'version' => Airbrake::VERSION,
+ 'url' => 'https://github.com/airbrake/airbrake'
+ },
+ 'errors' => [{
+ 'type' => error_class,
+ 'message' => error_message,
+ 'backtrace' => backtrace.lines.map do |line|
+ {
+ 'file' => line.file,
+ 'line' => line.number.to_i,
+ 'function' => line.method
+ }
+ end
+ }],
+ 'context' => {}.tap do |hash|
+ if url || controller || action || !parameters.blank? || !cgi_data.blank? || !session_data.blank?
+ hash['url'] = url
+ hash['component'] = controller
+ hash['action'] = action
+ hash['rootDirectory'] = File.dirname(project_root)
+ hash['environment'] = environment_name
+ end
+ end.tap do |hash|
+ next if user.blank?
+
+ hash['userId'] = user[:id]
+ hash['userName'] = user[:name]
+ hash['userEmail'] = user[:email]
+ end
+
+ }.tap do |hash|
+ hash['environment'] = cgi_data unless cgi_data.blank?
+ hash['params'] = parameters unless parameters.blank?
+ hash['session'] = session_data unless session_data.blank?
+ end.to_json
+ end
+
# Determines if this notice should be ignored
def ignore?
ignored_class_names.include?(error_class) ||
View
50 resources/airbrake_3_0.json
@@ -0,0 +1,50 @@
+{
+ "type" : "object",
+ "additionalProperties" : false,
+ "properties" : {
+ "notifier" : {
+ "type" : "hash",
+ "required" : true,
+ "additionalProperties" : false,
+ "properties" : {
+ "name" : {"type" : "string", "required" : true},
+ "version" : {"type" : "string", "required" : true},
+ "url" : {"type" : "string", "required" : true}
+ }
+ },
+ "errors" : {
+ "type" : "array",
+ "required" : true,
+ "items" : {
+ "type" : [{
+ "type" : "hash",
+ "additionalProperties" : false,
+ "properties" : {
+ "backtrace" : {"type" : "array", "required" : true},
+ "type" : {"type" : "string", "required" : true},
+ "message" : {"type" : "string", "required" : true}
+ }
+ }]
+ }
+ },
+ "context" : {
+ "type" : "hash",
+ "additionalProperties" : false,
+ "properties" : {
+ "os" : {"type" : "string"},
+ "language" : {"type" : "string"},
+ "environment" : {"type" : "string"},
+ "version" : {"type" : "string"},
+ "url" : {"type" : "string"},
+ "rootDirectory" : {"type" : "string"},
+ "userId" : {"type" : "string"},
+ "userName" : {"type" : "string"},
+ "userEmail" : {"type" : "string"}
+ }
+ },
+ "environment" : {"type" : "hash"},
+ "session" : {"type" : "hash"},
+ "params" : {"type" : "hash"}
+ }
+}
+
View
1 test/helper.rb
@@ -20,6 +20,7 @@
require 'rack'
require 'bourne'
require 'sham_rack'
+require 'json-schema'
require "airbrake"
View
33 test/notice_test.rb
@@ -67,6 +67,12 @@ def assert_valid_notice_document(document)
assert errors.empty?, errors.collect{|e| e.message }.join
end
+ def assert_valid_json(notice)
+ json_schema = File.expand_path(File.join(File.dirname(__FILE__),"..", "resources", "airbrake_3_0.json"))
+ errors = JSON::Validator.fully_validate(json_schema, notice)
+ assert errors.empty?, errors.collect{|e| e.message }.join
+ end
+
def assert_filters_hash(attribute)
filters = ["abc", :def]
original = { 'abc' => "123", 'def' => "456", 'ghi' => "789", 'nested' => { 'abc' => '100' },
@@ -262,6 +268,32 @@ def user
assert_equal({"abc" => "123"}, notice.cgi_data)
end
+ context "a Notice turned into JSON" do
+ setup do
+ @exception = build_exception
+
+ @notice = build_notice({
+ :notifier_name => 'a name',
+ :notifier_version => '1.2.3',
+ :notifier_url => 'http://some.url/path',
+ :exception => @exception,
+ :controller => "controller",
+ :action => "action",
+ :url => "http://url.com",
+ :parameters => { "paramskey" => "paramsvalue",
+ "nestparentkey" => { "nestkey" => "nestvalue" } },
+ :session_data => { "sessionkey" => "sessionvalue" },
+ :cgi_data => { "cgikey" => "cgivalue" },
+ :project_root => "RAILS_ROOT",
+ :environment_name => "RAILS_ENV"
+ })
+ end
+
+ should "validate against the JSON schema" do
+ assert_valid_json @json
+ end
+ end
+
context "a Notice turned into XML" do
setup do
Airbrake.configure do |config|
@@ -295,6 +327,7 @@ def user
assert_valid_notice_document @document
end
+
should "serialize a Notice to XML when sent #to_xml" do
assert_valid_node(@document, "//api-key", @notice.api_key)

0 comments on commit 69cba65

Please sign in to comment.
Something went wrong with that request. Please try again.