fanforce-errors 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWNlMjUyYzI1YjRkNWVlZTk2ZjM0YzA4NjVkN2JiYmQxNTkxMDU3NA==
5
+ data.tar.gz: !binary |-
6
+ M2U0YzBlOTRkZWI0ODkyZjNkM2FhNGJmY2EzNDg2ZjliZmRmMjU2NQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MzEwYTI1NWQ1ZDE4NjZhODQ1NjQ5MDViYzQyNzk1NzIxNWVhNWZjNzQyNjYw
10
+ N2Y5NjhmYzE0YjkxYTcwOWIwOTU4NTE1ZTExMTdkMzkwMzNjYjc3MjI3NjM1
11
+ NGVjMDQwZTdkMjU0MDY2ODJmM2YyNzZmZThlZTMyYWIwYTEyYjg=
12
+ data.tar.gz: !binary |-
13
+ OWU1NTMyMjdlMGQyMzZmOGJlY2VhODA0NjM0YmE4Yjc3MDU1NTZiMWI1ZmFi
14
+ MDY1NmI2ZjVkY2YzZTIyOGFhMTNmN2RmYzViYTIyYTVmMDhkNmYxMjk2MzAw
15
+ Y2VjNzA3YTI2YzY2Y2RjZDViNTg2MzIyZTNmYmExMDM5NmZlNGY=
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+ .DS_Store
20
+ .idea/
21
+ .idea/*
22
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ fanforce-errors
2
+ ==
3
+
4
+ Internal errors library used by the Fanforce codebase.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ # Default Rake task is compile
7
+ #task :default => :compile
8
+
9
+ ########################################################################
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs.push "lib"
13
+ t.libs.push "test"
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fanforce/errors/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'fanforce-errors'
8
+ gem.version = Fanforce::Errors::VERSION
9
+ gem.date = '2012-10-19'
10
+ gem.summary = %q{Internal errors library used by the Fanforce codebase.}
11
+ gem.authors = ['Caleb Clark']
12
+ gem.email = ['cclark@fanforce.com']
13
+ gem.homepage = 'http://github.com/fanforce/fanforce-errors'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_runtime_dependency 'fanforce-utils', '~> 0.1'
20
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce;
2
+ require_relative 'errors/version'
3
+ require_relative 'errors/errors'
4
+ end
5
+
@@ -0,0 +1,157 @@
1
+ class Fanforce::Error < StandardError
2
+ require 'fanforce/utils'
3
+ include Fanforce::Utils
4
+
5
+ attr_reader :public_errors, :env
6
+
7
+ def for(*args)
8
+ self.new(*args)
9
+ end
10
+
11
+ def initialize(*errors)
12
+ if errors[0].is_a?(String) and errors[1].is_a?(Hash) and is_blank?(errors[1][:message])
13
+ tmp = errors.shift() and errors[0][:message] = tmp
14
+ end
15
+ @public_errors = format_errors(errors)
16
+ @env = Fanforce.env
17
+ super(default_message.to_s)
18
+ end
19
+
20
+ def format_errors(raw_errors)
21
+ raw_errors.is_a?(Array) ? raw_errors.inject([]) {|errors, e| errors << format_error(e) } : [format_error(raw_errors)]
22
+ end
23
+
24
+ def format_error(raw_error)
25
+ return {message: is_blank?(raw_error) ? default_message : raw_error.to_s} if !raw_error.is_a?(Hash)
26
+ raw_error = symbolize_keys(raw_error)
27
+ error = { message: is_blank?(raw_error[:message]) ? default_message : raw_error[:message].to_s }
28
+ error[:resource] = raw_error[:resource].to_s if is_present?(raw_error[:resource])
29
+ error[:code] = (is_blank?(raw_error[:code]) or !valid_error_codes.include? raw_error[:code].to_sym) ? default_error_code : raw_error[:code].to_s
30
+ error[:field] = raw_error[:field].to_s if is_present?(raw_error[:field])
31
+ error[:fields] = raw_error[:fields].map {|f| f.to_s } if is_present?(raw_error[:fields])
32
+ error
33
+ end
34
+
35
+ def curl_command
36
+ return nil if is_blank?(@env[:request_method]) or is_blank?(@env[:request_url])
37
+ Fanforce::Utils.curl_command(@env[:request_method], @env[:request_url], @env[:request_params])
38
+ end
39
+
40
+ def code; 500 end
41
+ def default_message; end
42
+ def valid_error_codes; [] end
43
+ def default_error_code; end
44
+
45
+ end
46
+
47
+ class Fanforce::Error::BadRequest < Fanforce::Error # Request is missing something or invalid
48
+ def code; 400 end
49
+ def default_message; 'Bad Request' end
50
+ def valid_error_codes; [:blank_field, :missing_field, :invalid_association, :invalid_field] end
51
+ def default_error_code; :unknown end
52
+ end
53
+
54
+ class Fanforce::Error::Unauthorized < Fanforce::Error # Could not successfully authenticate
55
+ def code; 401 end
56
+ def default_message; 'Unauthorized Request' end
57
+ def valid_error_codes; [:unauthorized, :unknown_login] end
58
+ def default_error_code; :unauthorized end
59
+ end
60
+
61
+ class Fanforce::Error::Forbidden < Fanforce::Error # Current API key does not allow access
62
+ def code; 403 end
63
+ def default_message; 'API Key Forbids Access' end
64
+ def valid_error_codes; [:weak_api_key] end
65
+ def default_error_code; :weak_api_key end
66
+ end
67
+
68
+ class Fanforce::Error::NotFound < Fanforce::Error # Method or record could not be found
69
+ def code; 404 end
70
+ def default_message; 'Not Found' end
71
+ def valid_error_codes; [:record_not_found, :method_not_found] end
72
+ def default_error_code; :unknown end
73
+ end
74
+
75
+ class Fanforce::Error::RequestFailed < Fanforce::Error # Request has correct fields but failed for another reason
76
+ def code; 422 end
77
+ def default_message; 'Request Failed' end
78
+ def valid_error_codes; [:invalid_data, :record_already_exists, :maxed_capacity] end
79
+ def default_error_code; :unknown end
80
+ end
81
+
82
+ class Fanforce::Error::InternalError < Fanforce::Error
83
+ def code; 500 end
84
+ def default_message; 'Request Failed for Internal Reason' end
85
+ def valid_error_codes; [:internal_error, :invalid_fanforce_auth, :user_tampering, :third_party_error] end
86
+ def default_error_code; :unknown end
87
+ end
88
+
89
+ class Fanforce::Error::WebhookFailed < Fanforce::Error # Parameters were valid but request failed for another reason
90
+ attr_reader :request_url, :request_params, :response_code, :response_body, :request_curl_command
91
+ def code; 500 end
92
+ def default_message; 'Webhook Failed' end
93
+ def valid_error_codes; [:third_party_error] end
94
+ def default_error_code; :unknown end
95
+
96
+ def initialize(hash)
97
+ @request_url = hash[:request_url]
98
+ @request_params = hash[:request_params]
99
+ @response_code = hash[:response_code]
100
+ @response_body = hash[:response_body]
101
+ @request_curl_command = "curl -X POST -d \"#{@request_params.to_param}\" #{@request_url}"
102
+ super("Callback returned an HTTP response code of #{@response_code} when only 200 and 201 are allowed.")
103
+ end
104
+ end
105
+
106
+ class Fanforce::Error::AddonFailed < Fanforce::Error # A 3rd-party addon crapped out
107
+ attr_reader :plugin_dev_error
108
+ def default_message; 'Third-Party Plugin Error' end
109
+ def valid_error_codes; [:third_party_error] end
110
+ def default_error_code; :third_party_error end
111
+
112
+ def initialize(plugin_type, plugin_id, e, msg, &block)
113
+ plugin_dev_error = {}
114
+ plugin_dev_error[:problem] = msg
115
+ if e.is_a?(Fanforce::Errors::WebhookFailed)
116
+ plugin_dev_error[:request_url] = e.request_url
117
+ plugin_dev_error[:request_params] = e.request_params
118
+ plugin_dev_error[:response_code] = e.response_code
119
+ plugin_dev_error[:response_body] = e.response_body.to_s
120
+ plugin_dev_error[:curl_command] = "curl -X POST -d \"#{e.request_params.to_param}\" #{e.request_url}"
121
+ end
122
+ @plugin_dev_errors = e.respond_to?(:plugin_dev_errors) ? e.plugin_dev_errors : []
123
+ @plugin_dev_errors << plugin_dev_error
124
+
125
+ self.class.handler_blocks.each {|block| block.call(plugin_type, plugin_id, plugin_dev_error, e.backtrace) }
126
+ super(msg)
127
+ end
128
+
129
+ def self.handler(&block)
130
+ handler_blocks << block
131
+ end
132
+
133
+ def self.handler_blocks
134
+ @handler_blocks ||= []
135
+ end
136
+ end
137
+
138
+ module Fanforce::Errors
139
+ def self.included(base) base.extend(self) end
140
+
141
+ BadRequest = Fanforce::Error::BadRequest
142
+ Unauthorized = Fanforce::Error::Unauthorized
143
+ Forbidden = Fanforce::Error::Forbidden
144
+ NotFound = Fanforce::Error::NotFound
145
+ RequestFailed = Fanforce::Error::RequestFailed
146
+ InternalError = Fanforce::Error::InternalError
147
+ WebhookFailed = Fanforce::Error::WebhookFailed
148
+ AddonFailed = Fanforce::Error::AddonFailed
149
+
150
+ def self.format_admin_details(e, errors=[], additional_options={})
151
+ admin_details = {error_class: e.class.name, public_response: errors}
152
+ admin_details[:curl_command] = e.curl_command if e.respond_to?(:curl_command)
153
+ admin_details[:raw_message] = e.message
154
+ admin_details.merge(additional_options)
155
+ end
156
+
157
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ module Errors
3
+ VERSION = '0.2'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fanforce-errors
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fanforce-utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ description:
28
+ email:
29
+ - cclark@fanforce.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - fanforce-errors.gemspec
39
+ - lib/fanforce/errors.rb
40
+ - lib/fanforce/errors/errors.rb
41
+ - lib/fanforce/errors/version.rb
42
+ homepage: http://github.com/fanforce/fanforce-errors
43
+ licenses: []
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.0.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Internal errors library used by the Fanforce codebase.
65
+ test_files: []