Skip to content

Commit a081330

Browse files
authored
Merge pull request #4 from BjornMelgaard/master
meta with proc & registered_exception? helper
2 parents 4f708a6 + d2f05ba commit a081330

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

gemfiles/rails_5.gemfile.lock

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
PATH
2-
remote: ../
2+
remote: ..
33
specs:
4-
jsonapi_errorable (0.1.1)
5-
active_model_serializers (~> 0.10)
6-
rails (>= 4.1, < 6)
4+
jsonapi_errorable (0.6.2)
5+
jsonapi-serializable (~> 0.1)
76

87
GEM
98
remote: https://rubygems.org/
@@ -31,11 +30,6 @@ GEM
3130
erubis (~> 2.7.0)
3231
rails-dom-testing (~> 2.0)
3332
rails-html-sanitizer (~> 1.0, >= 1.0.2)
34-
active_model_serializers (0.10.2)
35-
actionpack (>= 4.1, < 6)
36-
activemodel (>= 4.1, < 6)
37-
jsonapi (~> 0.1.1.beta2)
38-
railties (>= 4.1, < 6)
3933
activejob (5.0.0.1)
4034
activesupport (= 5.0.0.1)
4135
globalid (>= 0.3.6)
@@ -64,9 +58,9 @@ GEM
6458
globalid (0.3.7)
6559
activesupport (>= 4.1.0)
6660
i18n (0.7.0)
67-
json (1.8.3)
68-
jsonapi (0.1.1.beta2)
69-
json (~> 1.8)
61+
jsonapi-renderer (0.1.2)
62+
jsonapi-serializable (0.1.3)
63+
jsonapi-renderer (~> 0.1)
7064
jsonapi_spec_helpers (0.2.0)
7165
loofah (2.0.3)
7266
nokogiri (>= 1.5.9)
@@ -167,4 +161,4 @@ DEPENDENCIES
167161
sqlite3
168162

169163
BUNDLED WITH
170-
1.12.5
164+
1.15.0

lib/jsonapi_errorable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def default_exception_handler
5555
self.class.default_exception_handler
5656
end
5757

58+
def registered_exception?(e)
59+
self.class._errorable_registry.key?(e.class)
60+
end
61+
5862
module ClassMethods
5963
def register_exception(klass, options = {})
6064
exception_klass = options[:handler] || default_exception_handler

lib/jsonapi_errorable/exception_handler.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ def initialize(options = {})
44
@status = options[:status]
55
@title = options[:title]
66
@message = options[:message]
7+
@meta = options[:meta]
78
@log = options[:log]
89
end
910

@@ -33,7 +34,8 @@ def detail(error)
3334
end
3435

3536
def meta(error)
36-
{}
37+
return {} unless @meta.respond_to?(:call)
38+
@meta.call(error)
3739
end
3840

3941
def error_payload(error)

spec/integration/jsonapi_errorable_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class CustomStatusError < StandardError;end
44
class CustomTitleError < StandardError;end
55
class MessageTrueError < StandardError;end
66
class MessageProcError < StandardError;end
7+
class MetaProcError < StandardError;end
78
class LogFalseError < StandardError;end
89
class CustomHandlerError < StandardError;end
910
class SpecialPostError < StandardError;end
@@ -21,6 +22,7 @@ class ApplicationController < ActionController::Base
2122
register_exception CustomTitleError, title: 'My Title'
2223
register_exception MessageTrueError, message: true
2324
register_exception MessageProcError, message: ->(e) { e.class.name.upcase }
25+
register_exception MetaProcError, meta: ->(e) { { class_name: e.class.name.upcase } }
2426
register_exception LogFalseError, log: false
2527
register_exception CustomHandlerError, handler: CustomErrorHandler
2628

@@ -82,6 +84,11 @@ def standard_detail
8284
end
8385

8486
context 'when the error is registered' do
87+
it 'is registered' do
88+
registered = controller.registered_exception?(CustomStatusError.new)
89+
expect(registered).to eq true
90+
end
91+
8592
context 'with custom status' do
8693
before do
8794
raises(CustomStatusError, 'some message')
@@ -128,6 +135,17 @@ def standard_detail
128135
end
129136
end
130137

138+
context 'with meta as proc' do
139+
before do
140+
raises(MetaProcError, 'some_error')
141+
end
142+
143+
it 'shows custom error detail' do
144+
get :index
145+
expect(error['meta']).to match('class_name' => 'METAPROCERROR')
146+
end
147+
end
148+
131149
context 'with log: false' do
132150
before do
133151
expect(Rails.logger).to_not receive(:error)

0 commit comments

Comments
 (0)