insights-api-common 4.1.0 → 4.1.1
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/README.md +2 -2
- data/lib/insights/api/common.rb +1 -0
- data/lib/insights/api/common/application_controller_mixins/exception_handling.rb +27 -10
- data/lib/insights/api/common/custom_exceptions.rb +16 -0
- data/lib/insights/api/common/metrics.rb +8 -2
- data/lib/insights/api/common/version.rb +1 -1
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38345e2eca706bb365900e3e3eb7ea2488d38587b654536cfe60bc04367a9e8c
|
4
|
+
data.tar.gz: 101d18e408627530170f6b62013ed494970939fa40e46fecb8e38e7cf4493685
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcf19c204af28d1a540408f86967e96e25e7345ffe0cdd1cfd9fc07b697cdbfba2abfee9fb9dca9f042c34988826dd3f2e000e57239fd3d1f5a19b1fd72309f8
|
7
|
+
data.tar.gz: a0bb8e4e1499fcdca95a925565fe601db6cc1a410c2c2d0c36b47ec3d766e248477bb67ecb842d4c79540a4cb3b49df332faa87c9913bb3e2851fe60a8e2a514
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Insights::API::Common
|
2
2
|
|
3
3
|
[](https://travis-ci.org/RedHatInsights/insights-api-common-rails)
|
4
|
-
[](https://codeclimate.com/github/RedHatInsights/insights-api-common-rails/maintainability)
|
5
|
+
[](https://codeclimate.com/github/RedHatInsights/insights-api-common-rails/test_coverage)
|
6
6
|
[](https://hakiri.io/github/RedHatInsights/insights-api-common-rails/master)
|
7
7
|
|
8
8
|
Header, Encryption, RBAC, Serialization, Pagination and other common behavior for Insights microservices built with Rails
|
data/lib/insights/api/common.rb
CHANGED
@@ -7,19 +7,28 @@ module Insights
|
|
7
7
|
|
8
8
|
def self.included(other)
|
9
9
|
other.rescue_from(StandardError, RuntimeError) do |exception|
|
10
|
-
|
11
|
-
|
12
|
-
exception_list_from(exception).each do |exc|
|
13
|
-
if api_client_exception?(exc)
|
14
|
-
api_client_errors(exc, error_document)
|
15
|
-
else
|
16
|
-
error_document.add(error_code_from_class(exc).to_s, "#{exc.class}: #{exc.message}")
|
17
|
-
end
|
18
|
-
end
|
10
|
+
rescue_from_handler(exception) do |error_document, exc|
|
11
|
+
error_document.add(error_code_from_class(exc).to_s, "#{exc.class}: #{exc.message}")
|
19
12
|
end
|
13
|
+
end
|
14
|
+
end
|
20
15
|
|
21
|
-
|
16
|
+
def rescue_from_handler(exception)
|
17
|
+
logger.error("#{exception.class.name}: #{exception.message}\n#{exception.backtrace.join("\n")}")
|
18
|
+
errors = Insights::API::Common::ErrorDocument.new.tap do |error_document|
|
19
|
+
exception_list_from(exception).each do |exc|
|
20
|
+
if api_client_exception?(exc)
|
21
|
+
api_client_errors(exc, error_document)
|
22
|
+
elsif custom_exception?(exc)
|
23
|
+
message = fetch_custom_message(exc)
|
24
|
+
error_document.add(error_code_from_class(exc).to_s, message)
|
25
|
+
else
|
26
|
+
yield error_document, exc
|
27
|
+
end
|
28
|
+
end
|
22
29
|
end
|
30
|
+
|
31
|
+
render :json => errors.to_h, :status => error_code_from_class(exception)
|
23
32
|
end
|
24
33
|
|
25
34
|
def exception_list_from(exception)
|
@@ -31,6 +40,14 @@ module Insights
|
|
31
40
|
end
|
32
41
|
end
|
33
42
|
|
43
|
+
def custom_exception?(exception)
|
44
|
+
Insights::API::Common::CustomExceptions::CUSTOM_EXCEPTION_LIST.include?(exception.class.to_s)
|
45
|
+
end
|
46
|
+
|
47
|
+
def fetch_custom_message(exception)
|
48
|
+
Insights::API::Common::CustomExceptions.custom_message(exception)
|
49
|
+
end
|
50
|
+
|
34
51
|
def error_code_from_class(exception)
|
35
52
|
if ActionDispatch::ExceptionWrapper.rescue_responses.key?(exception.class.to_s)
|
36
53
|
Rack::Utils.status_code(ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s])
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Insights
|
2
|
+
module API
|
3
|
+
module Common
|
4
|
+
class CustomExceptions
|
5
|
+
CUSTOM_EXCEPTION_LIST = %w[Pundit::NotAuthorizedError].freeze
|
6
|
+
|
7
|
+
def self.custom_message(exception)
|
8
|
+
case exception.class.to_s
|
9
|
+
when "Pundit::NotAuthorizedError"
|
10
|
+
"You are not authorized to #{exception.query.delete_suffix('?')} this #{exception.record.model_name.human.downcase}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -6,6 +6,8 @@ module Insights
|
|
6
6
|
require 'prometheus_exporter'
|
7
7
|
require 'prometheus_exporter/client'
|
8
8
|
|
9
|
+
return if metrics_port == 0
|
10
|
+
|
9
11
|
ensure_exporter_server
|
10
12
|
enable_in_process_metrics
|
11
13
|
enable_web_server_metrics(prefix)
|
@@ -14,10 +16,10 @@ module Insights
|
|
14
16
|
|
15
17
|
private_class_method def self.ensure_exporter_server
|
16
18
|
require 'socket'
|
17
|
-
TCPSocket.open("localhost",
|
19
|
+
TCPSocket.open("localhost", metrics_port) {}
|
18
20
|
rescue Errno::ECONNREFUSED
|
19
21
|
require 'prometheus_exporter/server'
|
20
|
-
server = PrometheusExporter::Server::WebServer.new(port:
|
22
|
+
server = PrometheusExporter::Server::WebServer.new(port: metrics_port)
|
21
23
|
server.start
|
22
24
|
|
23
25
|
PrometheusExporter::Client.default = PrometheusExporter::LocalClient.new(collector: server.collector)
|
@@ -51,6 +53,10 @@ module Insights
|
|
51
53
|
end
|
52
54
|
end
|
53
55
|
end
|
56
|
+
|
57
|
+
private_class_method def self.metrics_port
|
58
|
+
@metrics_port ||= (ENV['METRICS_PORT']&.to_i || 9394)
|
59
|
+
end
|
54
60
|
end
|
55
61
|
end
|
56
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: insights-api-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.1.
|
4
|
+
version: 4.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Insights Authors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: acts_as_tenant
|
@@ -58,20 +58,20 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 5.2.2
|
62
62
|
- - ">="
|
63
63
|
- !ruby/object:Gem::Version
|
64
|
-
version: 5.2.
|
64
|
+
version: 5.2.2.1
|
65
65
|
type: :runtime
|
66
66
|
prerelease: false
|
67
67
|
version_requirements: !ruby/object:Gem::Requirement
|
68
68
|
requirements:
|
69
69
|
- - "~>"
|
70
70
|
- !ruby/object:Gem::Version
|
71
|
-
version:
|
71
|
+
version: 5.2.2
|
72
72
|
- - ">="
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
version: 5.2.
|
74
|
+
version: 5.2.2.1
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
76
|
name: manageiq-loggers
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -236,16 +236,16 @@ dependencies:
|
|
236
236
|
name: rake
|
237
237
|
requirement: !ruby/object:Gem::Requirement
|
238
238
|
requirements:
|
239
|
-
- - "
|
239
|
+
- - ">="
|
240
240
|
- !ruby/object:Gem::Version
|
241
|
-
version:
|
241
|
+
version: 12.3.3
|
242
242
|
type: :development
|
243
243
|
prerelease: false
|
244
244
|
version_requirements: !ruby/object:Gem::Requirement
|
245
245
|
requirements:
|
246
|
-
- - "
|
246
|
+
- - ">="
|
247
247
|
- !ruby/object:Gem::Version
|
248
|
-
version:
|
248
|
+
version: 12.3.3
|
249
249
|
- !ruby/object:Gem::Dependency
|
250
250
|
name: rspec
|
251
251
|
requirement: !ruby/object:Gem::Requirement
|
@@ -345,6 +345,7 @@ files:
|
|
345
345
|
- lib/insights/api/common/application_controller_mixins/request_body_validation.rb
|
346
346
|
- lib/insights/api/common/application_controller_mixins/request_parameter_validation.rb
|
347
347
|
- lib/insights/api/common/application_controller_mixins/request_path.rb
|
348
|
+
- lib/insights/api/common/custom_exceptions.rb
|
348
349
|
- lib/insights/api/common/engine.rb
|
349
350
|
- lib/insights/api/common/entitlement.rb
|
350
351
|
- lib/insights/api/common/error_document.rb
|