gris 0.5.2 → 0.5.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/gris.rb +1 -0
- data/lib/gris/application.rb +4 -8
- data/lib/gris/generators/templates/scaffold/config/application.rb.tt +2 -1
- data/lib/gris/grape_extensions/error_helpers.rb +3 -3
- data/lib/gris/middleware/error_handlers.rb +40 -0
- data/lib/gris/version.rb +1 -1
- data/spec/generators/scaffold_generator_spec.rb +4 -0
- data/spec/grape_extensions/error_helpers_spec.rb +6 -9
- data/spec/integration/application_error_response_spec.rb +1 -1
- data/spec/middleware/error_handlers_spec.rb +72 -0
- data/spec/spec_helper.rb +5 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba36f4e2e607cba06f2812fa378d65f8757f158f
|
4
|
+
data.tar.gz: a69a898390400d437e5c96d5b486835ce3e94b75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a99a3dbf8338aeb8a3897c468e7f800c08d83fbb7ee638f9e730634579bf04b6e42a9753f0ec3fc05b8e5f54e14457c0c084ca8413d62f9d120eae0c887401e9
|
7
|
+
data.tar.gz: 6c367e60923c73d404b0ff51238bf30c5367c35f2c75efc0443b7d79f7d8f00472555262d0c56cb25e75ded3c15b7f39c8e59d920fb8109ceeab6039d709f5b2
|
data/Gemfile.lock
CHANGED
data/lib/gris.rb
CHANGED
@@ -18,6 +18,7 @@ require 'gris/grape_extensions/date_time_helpers'
|
|
18
18
|
require 'gris/grape_extensions/error_helpers'
|
19
19
|
require 'gris/identity'
|
20
20
|
require 'gris/middleware/health'
|
21
|
+
require 'gris/middleware/error_handlers'
|
21
22
|
require 'gris/output_formatters/paginated_presenter'
|
22
23
|
require 'gris/output_formatters/presenter'
|
23
24
|
require 'gris/output_formatters/presenter_link_helpers'
|
data/lib/gris/application.rb
CHANGED
@@ -4,10 +4,13 @@ module Gris
|
|
4
4
|
class Application
|
5
5
|
include ActiveSupport::Configurable
|
6
6
|
config_accessor :use_health_middleware
|
7
|
+
config_accessor :use_error_handlers_middleware
|
7
8
|
|
8
9
|
def self.instance(config = {})
|
9
10
|
@instance ||= Rack::Builder.new do
|
10
11
|
use Gris::Middleware::Health unless config[:use_health_middleware] == false
|
12
|
+
use Gris::Middleware::ErrorHandlers unless config[:use_error_handlers_middleware] == false
|
13
|
+
|
11
14
|
use Rack::Cors do
|
12
15
|
allow do
|
13
16
|
origins '*'
|
@@ -19,14 +22,7 @@ module Gris
|
|
19
22
|
end
|
20
23
|
|
21
24
|
def call(env)
|
22
|
-
|
23
|
-
case response[0]
|
24
|
-
when 404, 500
|
25
|
-
body = { code: response[0], message: response[2] }.to_json
|
26
|
-
[response[0], response[1], [body]]
|
27
|
-
else
|
28
|
-
response
|
29
|
-
end
|
25
|
+
ApplicationEndpoint.call(env)
|
30
26
|
end
|
31
27
|
end
|
32
28
|
end
|
@@ -9,7 +9,8 @@ ActiveSupport::Dependencies.autoload_paths += relative_load_paths
|
|
9
9
|
|
10
10
|
module <%= app_name.classify %>
|
11
11
|
class Application < Gris::Application
|
12
|
-
|
12
|
+
config.use_error_handlers_middleware = true
|
13
|
+
config.use_health_middleware = true
|
13
14
|
|
14
15
|
def self.instance(*)
|
15
16
|
super(config)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Gris
|
2
2
|
module ErrorHelpers
|
3
|
-
def
|
4
|
-
|
5
|
-
throw :error, message:
|
3
|
+
def gris_error!(message, status)
|
4
|
+
response = { status: status, message: message }
|
5
|
+
throw :error, message: response, status: status
|
6
6
|
end
|
7
7
|
|
8
8
|
Grape::Endpoint.send :include, self if defined?(Grape)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Gris
|
2
|
+
class Middleware
|
3
|
+
class ErrorHandlers
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
response = @app.call env
|
10
|
+
case response[0]
|
11
|
+
when 400..500
|
12
|
+
format_error_response response
|
13
|
+
else
|
14
|
+
response
|
15
|
+
end
|
16
|
+
rescue RuntimeError => e
|
17
|
+
error = { status: 500, message: e.message }
|
18
|
+
error_response(error.to_json, 500)
|
19
|
+
rescue ::ActiveRecord::RecordNotFound => e
|
20
|
+
error = { status: 404, message: e.message }
|
21
|
+
error_response(error.to_json, 404)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def format_error_response(response)
|
27
|
+
if response[2].respond_to? :body
|
28
|
+
response
|
29
|
+
else
|
30
|
+
error = { status: response[0], message: response[2] }
|
31
|
+
error_response(error.to_json, response[0], response[1])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def error_response(message, status, headers = {})
|
36
|
+
Rack::Response.new([message], status, headers).finish
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/gris/version.rb
CHANGED
@@ -72,6 +72,10 @@ describe Gris::Generators::ScaffoldGenerator do
|
|
72
72
|
'ActiveSupport::Dependencies.autoload_paths += relative_load_paths'
|
73
73
|
)
|
74
74
|
end
|
75
|
+
it 'includes optional middleware configs' do
|
76
|
+
expect(config_application_file).to include('config.use_error_handlers_middleware = true')
|
77
|
+
expect(config_application_file).to include('config.use_health_middleware = true')
|
78
|
+
end
|
75
79
|
end
|
76
80
|
|
77
81
|
it 'generates an application endpoint' do
|
@@ -4,15 +4,12 @@ describe Gris::ErrorHelpers do
|
|
4
4
|
before do
|
5
5
|
@helper = SpecApiErrorHelper.new
|
6
6
|
end
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
expect(@helper.
|
12
|
-
|
13
|
-
it 'wraps a grape error with text' do
|
14
|
-
@helper.error!('error', 400, 'text')
|
15
|
-
expect(@helper.message).to eq(message: { error: 'error', text: 'text' }, status: 400)
|
7
|
+
|
8
|
+
context 'gris_error!' do
|
9
|
+
it 'returns a correctly formatted error message' do
|
10
|
+
@helper.gris_error! 'error', 400
|
11
|
+
expect(@helper.message)
|
12
|
+
.to eq(message: { message: 'error', status: 400 }, status: 400)
|
16
13
|
expect(@helper.thrown).to eq(:error)
|
17
14
|
end
|
18
15
|
end
|
@@ -8,7 +8,7 @@ describe 'application error response' do
|
|
8
8
|
request = Faraday.get "http://localhost:#{app_port}/bogus"
|
9
9
|
response = JSON.parse request.body
|
10
10
|
expect(request.status).to eq 404
|
11
|
-
expect(response['
|
11
|
+
expect(response['status']).to eq 404
|
12
12
|
expect(response['message']).to eq ['Not Found']
|
13
13
|
end
|
14
14
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Gris::Middleware::ErrorHandlers do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
describe 'errors' do
|
6
|
+
subject { Class.new(Grape::API) }
|
7
|
+
|
8
|
+
let(:app) { subject }
|
9
|
+
|
10
|
+
let(:fake_active_record) do
|
11
|
+
module ActiveRecord
|
12
|
+
class RecordNotFound < StandardError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
before do
|
18
|
+
subject.format :json
|
19
|
+
subject.use Gris::Middleware::ErrorHandlers
|
20
|
+
|
21
|
+
subject.get :generic_error do
|
22
|
+
fail 'api error'
|
23
|
+
end
|
24
|
+
|
25
|
+
subject.get :gris_error do
|
26
|
+
gris_error! 'Forbidden', 401
|
27
|
+
end
|
28
|
+
|
29
|
+
subject.get :error do
|
30
|
+
error! 'Forbidden', 401
|
31
|
+
end
|
32
|
+
|
33
|
+
subject.get :activerecord_error do
|
34
|
+
fail ActiveRecord::RecordNotFound
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'retuns a Not Found error for missing routes' do
|
39
|
+
get '/bogus'
|
40
|
+
expect(response_code).to eq 404
|
41
|
+
expect(response_body).to eq 'Not Found'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'returns a formatted message for gris_error!' do
|
45
|
+
get '/gris_error'
|
46
|
+
expect(response_code).to eq 401
|
47
|
+
expect(parsed_response_body['status']).to eq 401
|
48
|
+
expect(parsed_response_body['message']).to eq 'Forbidden'
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns a formatted message for error!' do
|
52
|
+
get '/error'
|
53
|
+
expect(response_code).to eq 401
|
54
|
+
expect(parsed_response_body['error']).to eq 'Forbidden'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns a formatted message for ActiveRecord::RecordNotFound' do
|
58
|
+
fake_active_record
|
59
|
+
get '/activerecord_error'
|
60
|
+
expect(response_code).to eq 404
|
61
|
+
expect(parsed_response_body['status']).to eq 404
|
62
|
+
expect(parsed_response_body['message']).to eq 'ActiveRecord::RecordNotFound'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns a formatted message when an exception is raised' do
|
66
|
+
get '/generic_error'
|
67
|
+
expect(response_code).to eq 500
|
68
|
+
expect(parsed_response_body['status']).to eq 500
|
69
|
+
expect(parsed_response_body['message']).to eq 'api error'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -6,7 +6,12 @@ require 'gris'
|
|
6
6
|
require 'gris/cli'
|
7
7
|
require 'rack/test'
|
8
8
|
require 'fakefs/spec_helpers'
|
9
|
+
require 'gris/rspec_extensions/response_helpers'
|
9
10
|
|
10
11
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
11
12
|
# in spec/support/ and its subdirectories.
|
12
13
|
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.include Gris::RspecExtensions::ResponseHelpers
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gris
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dylan Fareed
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -369,6 +369,7 @@ files:
|
|
369
369
|
- lib/gris/grape_extensions/date_time_helpers.rb
|
370
370
|
- lib/gris/grape_extensions/error_helpers.rb
|
371
371
|
- lib/gris/identity.rb
|
372
|
+
- lib/gris/middleware/error_handlers.rb
|
372
373
|
- lib/gris/middleware/health.rb
|
373
374
|
- lib/gris/output_formatters/paginated_presenter.rb
|
374
375
|
- lib/gris/output_formatters/presenter.rb
|
@@ -390,6 +391,7 @@ files:
|
|
390
391
|
- spec/grape_extensions/error_helpers_spec.rb
|
391
392
|
- spec/identity_spec.rb
|
392
393
|
- spec/integration/application_error_response_spec.rb
|
394
|
+
- spec/middleware/error_handlers_spec.rb
|
393
395
|
- spec/output_formatters/presenter_link_helpers_spec.rb
|
394
396
|
- spec/secrets_spec.rb
|
395
397
|
- spec/spec_helper.rb
|
@@ -436,6 +438,7 @@ test_files:
|
|
436
438
|
- spec/grape_extensions/error_helpers_spec.rb
|
437
439
|
- spec/identity_spec.rb
|
438
440
|
- spec/integration/application_error_response_spec.rb
|
441
|
+
- spec/middleware/error_handlers_spec.rb
|
439
442
|
- spec/output_formatters/presenter_link_helpers_spec.rb
|
440
443
|
- spec/secrets_spec.rb
|
441
444
|
- spec/spec_helper.rb
|