Skip to content

Commit bc90147

Browse files
committed
Collect exceptions under KrackenError and move them to errors.rb
This is also intended to clean up some of the duplicate code that was a by product of the changes living in an app before merged upstream.
1 parent 8bf58a0 commit bc90147

File tree

7 files changed

+46
-54
lines changed

7 files changed

+46
-54
lines changed

Gemfile.lock

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
kracken (0.0.9)
4+
kracken (0.1.0)
55
faraday (~> 0.8)
66
omniauth (~> 1.0)
77
omniauth-oauth2 (~> 1.1)
@@ -55,22 +55,21 @@ GEM
5555
erubis (2.7.0)
5656
faraday (0.9.1)
5757
multipart-post (>= 1.2, < 3)
58-
globalid (0.3.3)
58+
globalid (0.3.5)
5959
activesupport (>= 4.1.0)
60-
hashie (3.4.0)
61-
hike (1.2.3)
60+
hashie (3.4.2)
6261
i18n (0.7.0)
6362
json (1.8.2)
64-
jwt (1.4.1)
63+
jwt (1.5.1)
6564
loofah (2.0.1)
6665
nokogiri (>= 1.5.9)
6766
mail (2.6.3)
6867
mime-types (>= 1.16, < 3)
6968
method_source (0.8.2)
70-
mime-types (2.4.3)
69+
mime-types (2.6.1)
7170
mini_portile (0.6.2)
7271
minitest (5.5.1)
73-
multi_json (1.11.0)
72+
multi_json (1.11.2)
7473
multi_xml (0.5.5)
7574
multipart-post (2.0.0)
7675
nokogiri (1.6.6.2)
@@ -84,9 +83,7 @@ GEM
8483
omniauth (1.2.2)
8584
hashie (>= 1.2, < 4)
8685
rack (~> 1.0)
87-
omniauth-oauth2 (1.2.0)
88-
faraday (>= 0.8, < 0.10)
89-
multi_json (~> 1.3)
86+
omniauth-oauth2 (1.3.1)
9087
oauth2 (~> 1.0)
9188
omniauth (~> 1.2)
9289
pry (0.10.1)
@@ -142,18 +139,14 @@ GEM
142139
rspec-support (3.2.2)
143140
safe_yaml (1.0.4)
144141
slop (3.6.0)
145-
sprockets (2.12.3)
146-
hike (~> 1.2)
147-
multi_json (~> 1.0)
142+
sprockets (3.2.0)
148143
rack (~> 1.0)
149-
tilt (~> 1.1, != 1.3.0)
150-
sprockets-rails (2.2.4)
144+
sprockets-rails (2.3.2)
151145
actionpack (>= 3.0)
152146
activesupport (>= 3.0)
153147
sprockets (>= 2.8, < 4.0)
154148
thor (0.19.1)
155149
thread_safe (0.3.5)
156-
tilt (1.4.1)
157150
tzinfo (1.2.2)
158151
thread_safe (~> 0.1)
159152
webmock (1.20.4)

lib/kracken/controllers/json_api_compatible.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,6 @@ module Kracken
44
module Controllers
55
module JsonApiCompatible
66

7-
class ResourceNotFound < StandardError
8-
attr_reader :missing_ids, :resource
9-
def initialize(resource, missing_ids)
10-
@missing_ids = Array(missing_ids)
11-
@resource = resource
12-
super(
13-
"Couldn't find #{resource} with id(s): #{missing_ids.join(', ')}"
14-
)
15-
end
16-
end
17-
18-
class TokenUnauthorized < StandardError
19-
def initialize(msg = nil)
20-
msg ||= 'HTTP Token: Access denied.'
21-
super(msg)
22-
end
23-
end
24-
25-
class UnprocessableEntity < StandardError; end
267

278
module MungeAndMirror
289
# Wraps the data root in an Array, if it is not already an Array. This

lib/kracken/controllers/token_authenticatable.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ def self.included(base)
1313

1414
rescue_from Kracken::RequestError do |_|
1515
# TODO: Handle other types of errors (such as if the server is down)
16-
raise Kracken::Controllers::JsonApiCompatible::TokenUnauthorized,
16+
raise TokenUnauthorized,
1717
"Invalid credentials"
1818
end
1919
end
2020

21+
2122
end
2223

2324
attr_reader :current_user
@@ -51,14 +52,6 @@ def munge_header_auth_token!
5152
request.env['HTTP_AUTHORIZATION'] = "Token token=\"#{params[:token]}\""
5253
end
5354

54-
# Customize the `authenticate_or_request_with_http_token` process:
55-
# http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token/ControllerMethods.html#method-i-request_http_token_authentication
56-
def request_http_token_authentication(realm = 'Application')
57-
# Modified from https://github.com/rails/rails/blob/60d0aec7/actionpack/lib/action_controller/metal/http_authentication.rb#L490-L499
58-
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
59-
render json: { error: 'HTTP Token: Access denied.' }, status: :unauthorized
60-
end
61-
6255
def realm
6356
self.class.realm
6457
end

lib/kracken/error.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
module Kracken
2-
KrackenError = Class.new(StandardError)
3-
RequestError = Class.new(KrackenError)
4-
MissingUIDError = Class.new(KrackenError)
2+
KrackenError = Class.new(StandardError)
3+
RequestError = Class.new(KrackenError)
4+
MissingUIDError = Class.new(KrackenError)
5+
6+
module Controllers
7+
UnprocessableEntity = Class.new(StandardError)
8+
9+
class TokenUnauthorized < KrackenError
10+
def initialize(msg = nil)
11+
msg ||= 'HTTP Token: Access denied.'
12+
super(msg)
13+
end
14+
end
15+
16+
class ResourceNotFound < KrackenError
17+
attr_reader :missing_ids, :resource
18+
def initialize(resource, missing_ids)
19+
@missing_ids = Array(missing_ids)
20+
@resource = resource
21+
super(
22+
"Couldn't find #{resource} with id(s): #{missing_ids.join(', ')}"
23+
)
24+
end
25+
end
26+
end
527
end

lib/kracken/json_api/exception_wrapper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ class ExceptionWrapper < ActionDispatch::ExceptionWrapper
44
cattr_accessor :rescue_with_details_responses
55
@@rescue_with_details_responses = Hash.new
66
@@rescue_with_details_responses.merge!(
7-
'Kracken::Controllers::JsonApiCompatible::ResourceNotFound' => :not_found,
8-
'Kracken::Controllers::JsonApiCompatible::TokenUnauthorized' => :unauthorized,
9-
'Kracken::Controllers::JsonApiCompatible::UnprocessableEntity' => :unprocessable_entity,
7+
'Kracken::Controllers::ResourceNotFound' => :not_found,
8+
'Kracken::Controllers::TokenUnauthorized' => :unauthorized,
9+
'Kracken::Controllers::UnprocessableEntity' => :unprocessable_entity,
1010
)
1111

1212
def self.status_code_for_exception(class_name)

spec/dummy/config/application.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
Bundler.require(*Rails.groups)
1111
require "kracken"
12+
require "kracken/json_api"
1213

1314
module Dummy
1415
class Application < Rails::Application
@@ -23,6 +24,9 @@ class Application < Rails::Application
2324
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
2425
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
2526
# config.i18n.default_locale = :de
27+
config.middleware.insert_after ActionDispatch::DebugExceptions,
28+
Kracken::JsonApi::PublicExceptions
29+
2630
end
2731
end
2832

spec/requests/api_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ def headers_with_token(token)
99

1010
let(:json){ Fixtures.auth_hash.to_json }
1111

12-
describe "requests to and authenticatable resource", type: :request do
13-
it "Renders unauthorized if there is no token" do
14-
get api_index_path
15-
expect(response.status).to eq(401)
12+
describe "authenticatable resource", type: :request do
13+
it "will raise error if there is no token" do
14+
expect{get api_index_path}.to raise ::Kracken::Controllers::TokenUnauthorized
1615
end
1716

1817
it "is redirected to the oauth server if there is no current user" do

0 commit comments

Comments
 (0)