fanforce-exceptions 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjVlNGY3MDU0NTYwNGE3OGM4ODE0ZTMwOTU0ZTE5MWM4YmJjYjVmZA==
5
+ data.tar.gz: !binary |-
6
+ YjEwMjZhNWE0MTg0NjQwMDM3NGE0M2JhY2JhMGVmYWJlYmEwMjJmYg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MjI4YzgyZWY0ZjUyODgxNDVjNzM1YzNlZGExMTYzNmM1MDQ2M2IyNDQ2MjFl
10
+ N2JjOGIwMDFhY2ZjODQ4YTY1NzNlMThiYjA1Nzc0NWYzYjU5NDg4MWFmZjE0
11
+ YWNkYThhZjJhZDcwZGM5ODYwOTE4ZDc4ZWNkMmY5NjFlMmJmODA=
12
+ data.tar.gz: !binary |-
13
+ ZDM5ZWFmYTY0Mzc4YzU1NTEwNjk0OTJkODMwNjcwZmU0ZTQ5Yjg0NDYwNGFi
14
+ MmYwNzBjNmIxZDIwYjgzNDg2ZTYzZWFkYjQ2ZjVlZmZhOTE1NzY1NzhlYWUw
15
+ ZjZiNmU0MzFkN2Y3ZmM2OTdmYmMxMjZkMjY1ZGQyYjYwYzI0NGM=
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-exceptions
2
+ ==
3
+
4
+ Internal exceptions 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/exceptions/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'fanforce-exceptions'
8
+ gem.version = Fanforce::Exceptions::VERSION
9
+ gem.date = Time.now.utc.strftime('%Y-%m-%d')
10
+ gem.summary = %q{Internal exceptions used by the Fanforce codebase.}
11
+ gem.authors = ['Caleb Clark']
12
+ gem.email = ['cclark@fanforce.com']
13
+ gem.homepage = 'http://github.com/fanforce/fanforce-exceptions'
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.3.0'
20
+ end
@@ -0,0 +1,170 @@
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("#{@public_errors[0].to_json}")
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
+ def initialize(*args)
74
+ if args[0].is_a?(Hash)
75
+ super(*args)
76
+ else
77
+ super(
78
+ resource: args[0].to_s.demodulize.singularize,
79
+ message: args[1]
80
+ )
81
+ end
82
+ end
83
+ def self.for(resource, message=nil)
84
+ self.new(resource, message)
85
+ end
86
+ end
87
+
88
+ class Fanforce::Error::RequestFailed < Fanforce::Error # Request has correct fields but failed for another reason
89
+ def code; 422 end
90
+ def default_message; 'Request Failed' end
91
+ def valid_error_codes; [:invalid_data, :record_already_exists, :maxed_capacity] end
92
+ def default_error_code; :unknown end
93
+ end
94
+
95
+ class Fanforce::Error::InternalError < Fanforce::Error
96
+ def code; 500 end
97
+ def default_message; 'Request Failed for Internal Reason' end
98
+ def valid_error_codes; [:internal_error, :invalid_fanforce_auth, :user_tampering, :third_party_error] end
99
+ def default_error_code; :unknown end
100
+ end
101
+
102
+ class Fanforce::Error::WebhookFailed < Fanforce::Error # Parameters were valid but request failed for another reason
103
+ attr_reader :request_url, :request_params, :response_code, :response_body, :request_curl_command
104
+ def code; 500 end
105
+ def default_message; 'Webhook Failed' end
106
+ def valid_error_codes; [:third_party_error] end
107
+ def default_error_code; :unknown end
108
+
109
+ def initialize(hash)
110
+ @request_url = hash[:request_url]
111
+ @request_params = hash[:request_params]
112
+ @response_code = hash[:response_code]
113
+ @response_body = hash[:response_body]
114
+ @request_curl_command = "curl -X POST -d \"#{@request_params.to_param}\" #{@request_url}"
115
+ super("Callback returned an HTTP response code of #{@response_code} when only 200 and 201 are allowed.")
116
+ end
117
+ end
118
+
119
+ class Fanforce::Error::AddonFailed < Fanforce::Error # A 3rd-party addon crapped out
120
+ attr_reader :plugin_dev_error
121
+ def default_message; 'Third-Party Plugin Error' end
122
+ def valid_error_codes; [:third_party_error] end
123
+ def default_error_code; :third_party_error end
124
+
125
+ def initialize(plugin_type, plugin_id, e, msg, &block)
126
+ plugin_dev_error = {}
127
+ plugin_dev_error[:problem] = msg
128
+ if e.is_a?(Fanforce::Errors::WebhookFailed)
129
+ plugin_dev_error[:request_url] = e.request_url
130
+ plugin_dev_error[:request_params] = e.request_params
131
+ plugin_dev_error[:response_code] = e.response_code
132
+ plugin_dev_error[:response_body] = e.response_body.to_s
133
+ plugin_dev_error[:curl_command] = "curl -X POST -d \"#{e.request_params.to_param}\" #{e.request_url}"
134
+ end
135
+ @plugin_dev_errors = e.respond_to?(:plugin_dev_errors) ? e.plugin_dev_errors : []
136
+ @plugin_dev_errors << plugin_dev_error
137
+
138
+ self.class.handler_blocks.each {|block| block.call(plugin_type, plugin_id, plugin_dev_error, e.backtrace) }
139
+ super(msg)
140
+ end
141
+
142
+ def self.handler(&block)
143
+ handler_blocks << block
144
+ end
145
+
146
+ def self.handler_blocks
147
+ @handler_blocks ||= []
148
+ end
149
+ end
150
+
151
+ module Fanforce::Errors
152
+ def self.included(base) base.extend(self) end
153
+
154
+ BadRequest = Fanforce::Error::BadRequest unless defined?(BadRequest)
155
+ Unauthorized = Fanforce::Error::Unauthorized unless defined?(Unauthorized)
156
+ Forbidden = Fanforce::Error::Forbidden unless defined?(Forbidden)
157
+ NotFound = Fanforce::Error::NotFound unless defined?(NotFound)
158
+ RequestFailed = Fanforce::Error::RequestFailed unless defined?(RequestFailed)
159
+ InternalError = Fanforce::Error::InternalError unless defined?(InternalError)
160
+ WebhookFailed = Fanforce::Error::WebhookFailed unless defined?(WebhookFailed)
161
+ AddonFailed = Fanforce::Error::AddonFailed unless defined?(AddonFailed)
162
+
163
+ def self.format_admin_details(e, errors=[], additional_options={})
164
+ admin_details = {error_class: e.class.name, public_response: errors}
165
+ admin_details[:curl_command] = e.curl_command if e.respond_to?(:curl_command)
166
+ admin_details[:raw_message] = e.message
167
+ admin_details.merge(additional_options)
168
+ end
169
+
170
+ end
@@ -0,0 +1,4 @@
1
+ class Fanforce;
2
+ require_relative 'errors/_base'
3
+ end
4
+
@@ -0,0 +1,7 @@
1
+ module Fanforce::Exceptions
2
+ require_relative '../errors'
3
+ require_relative '../notices'
4
+
5
+ include Fanforce::Errors
6
+ include Fanforce::Notices
7
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ module Exceptions
3
+ VERSION = '0.4.0'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ require_relative 'exceptions/version'
3
+ require_relative 'exceptions/_base'
4
+ end
5
+
@@ -0,0 +1,18 @@
1
+ require_relative '../errors/_base'
2
+
3
+ class Fanforce::Notice < Fanforce::Error
4
+ def public_notices
5
+ @public_errors
6
+ end
7
+ end
8
+
9
+ class Fanforce::Notice::ProcessingWebhook < Fanforce::Notice
10
+ def code; 409 end
11
+ def default_message; 'Processing Request' end
12
+ end
13
+
14
+ module Fanforce::Notices
15
+ def self.included(base) base.extend(self) end
16
+
17
+ ProcessingWebhook = Fanforce::Notice::ProcessingWebhook
18
+ end
@@ -0,0 +1,4 @@
1
+ class Fanforce;
2
+ require_relative 'notices/_base'
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fanforce-exceptions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-06 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.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.0
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-exceptions.gemspec
39
+ - lib/fanforce/errors.rb
40
+ - lib/fanforce/errors/_base.rb
41
+ - lib/fanforce/exceptions.rb
42
+ - lib/fanforce/exceptions/_base.rb
43
+ - lib/fanforce/exceptions/version.rb
44
+ - lib/fanforce/notices.rb
45
+ - lib/fanforce/notices/_base.rb
46
+ homepage: http://github.com/fanforce/fanforce-exceptions
47
+ licenses: []
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Internal exceptions used by the Fanforce codebase.
69
+ test_files: []