yml_error_responder 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +37 -0
- data/app/assets/javascripts/yml_error_responder/application.js +13 -0
- data/app/assets/stylesheets/yml_error_responder/application.css +15 -0
- data/app/controllers/yml_error_responder/application_controller.rb +5 -0
- data/app/controllers/yml_error_responder/errors_controller.rb +7 -0
- data/app/controllers/yml_error_responder/errors_example_controller.rb +30 -0
- data/app/errors/api_error.rb +16 -0
- data/app/helpers/yml_error_responder/application_helper.rb +4 -0
- data/app/views/layouts/yml_error_responder/application.html.erb +14 -0
- data/app/views/yml_error_responder/errors/index.html.erb +14 -0
- data/config/errors/base_errors.yml +9 -0
- data/config/routes.rb +3 -0
- data/lib/tasks/yml_error_responder_tasks.rake +4 -0
- data/lib/yml_error_responder.rb +33 -0
- data/lib/yml_error_responder/engine.rb +5 -0
- data/lib/yml_error_responder/error_handlers.rb +21 -0
- data/lib/yml_error_responder/error_handlers/base_error_handler.rb +32 -0
- data/lib/yml_error_responder/error_handlers/default_error_handler.rb +6 -0
- data/lib/yml_error_responder/error_handlers/unknow_error_handler.rb +13 -0
- data/lib/yml_error_responder/responder.rb +55 -0
- data/lib/yml_error_responder/version.rb +3 -0
- data/lib/yml_error_responder/yml_mapper.rb +53 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/integration/navigation_test.rb +8 -0
- data/test/test_helper.rb +21 -0
- data/test/yml_error_responder_test.rb +7 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b712bf03484c6a6030038f21b29c23c356343bcc1ea9698dafc06158be20c462
|
4
|
+
data.tar.gz: 2b5454009878d9cd8a778d2880a7228d5b9056f9a919982f8a111401ddd194fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee6409416404f8ee3d73cc041e6cc37117e3d9c79095e1bde126e0ca246ad8d564bf202c1181bebfa8e2d8b42990e63d88edca61ffe01869e9ae090cdce04667
|
7
|
+
data.tar.gz: a914e3ec1325b1997666c565197206859cf8bab44f885102c127af3c100f4452c2847b57e9396877e34d684a4fb79bfabc7eb939f1e59bcdd5ec4ba35b5520d9
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Egench
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'YmlErrorResponder'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module YmlErrorResponder
|
2
|
+
class ErrorsExampleController < Api::V1::BaseController
|
3
|
+
|
4
|
+
class CvvInvalid < ApiError; end
|
5
|
+
class Appointment < ApiError; end
|
6
|
+
|
7
|
+
def cvv
|
8
|
+
raise CvvInvalid
|
9
|
+
end
|
10
|
+
|
11
|
+
def appointment
|
12
|
+
raise Appointment.new(meta: {
|
13
|
+
"available_slots": [
|
14
|
+
{
|
15
|
+
"id": 4,
|
16
|
+
"name": "Slot1"
|
17
|
+
},
|
18
|
+
{
|
19
|
+
"id": 5,
|
20
|
+
"name": "Slot2"
|
21
|
+
}
|
22
|
+
]
|
23
|
+
})
|
24
|
+
end
|
25
|
+
|
26
|
+
def uncovered
|
27
|
+
raise 'UncoveredExeption'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class ApiError < StandardError
|
2
|
+
attr_reader :meta
|
3
|
+
|
4
|
+
def initialize(meta: {})
|
5
|
+
@meta = meta
|
6
|
+
end
|
7
|
+
|
8
|
+
def serialize
|
9
|
+
errors = instance_variables.each_with_object({}) do |exceptions, var|
|
10
|
+
exceptions[var.to_s.gsub(/@/, '')] = instance_variable_get(var)
|
11
|
+
exceptions
|
12
|
+
end
|
13
|
+
|
14
|
+
{ errors: errors }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>YmlErrorResponder</title>
|
5
|
+
<%= stylesheet_link_tag "yml_error_responder/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "yml_error_responder/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'yml_error_responder/yml_mapper'
|
2
|
+
require 'yml_error_responder/error_handlers'
|
3
|
+
require 'yml_error_responder/error_handlers/base_error_handler'
|
4
|
+
require 'yml_error_responder/error_handlers/default_error_handler'
|
5
|
+
require 'yml_error_responder/error_handlers/unknow_error_handler'
|
6
|
+
require 'yml_error_responder/responder'
|
7
|
+
require 'yml_error_responder/engine'
|
8
|
+
|
9
|
+
module YmlErrorResponder
|
10
|
+
@configuration_path = 'config/errors/'
|
11
|
+
@handle_unknown_error = false
|
12
|
+
@unknown_error_logger = nil
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :configuration_path
|
16
|
+
attr_accessor :handle_unknown_error
|
17
|
+
attr_accessor :unknown_error_logger
|
18
|
+
|
19
|
+
def setup
|
20
|
+
yield self
|
21
|
+
end
|
22
|
+
|
23
|
+
def log_unknown_error_with(&block)
|
24
|
+
@unknown_error_logger = block
|
25
|
+
end
|
26
|
+
|
27
|
+
def routes(application)
|
28
|
+
unless Rails.env.production?
|
29
|
+
application.mount YmlErrorResponder::Engine => "/api/yml_error_responder"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module YmlErrorResponder
|
2
|
+
module ErrorHandlers
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def unknown_error
|
6
|
+
UnknowErrorHandler.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def handler(error, payload)
|
10
|
+
lookup_handler(error).new(error, payload)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def lookup_handler(error)
|
16
|
+
"::ErrorHandlers::#{error.class.to_s}".constantize
|
17
|
+
rescue
|
18
|
+
DefaultErrorHandler
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module YmlErrorResponder
|
2
|
+
module ErrorHandlers
|
3
|
+
class BaseErrorHandler
|
4
|
+
attr_accessor :handler_data, :error
|
5
|
+
|
6
|
+
def initialize(error, handler_data)
|
7
|
+
@handler_data = handler_data
|
8
|
+
@error = error
|
9
|
+
end
|
10
|
+
|
11
|
+
def http_code
|
12
|
+
@handler_data[:http_code]
|
13
|
+
end
|
14
|
+
|
15
|
+
def meta
|
16
|
+
@error.try(:meta) || {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def as_json
|
20
|
+
{
|
21
|
+
data: {
|
22
|
+
error: {
|
23
|
+
code: @handler_data[:error_code],
|
24
|
+
description: @handler_data[:description]
|
25
|
+
},
|
26
|
+
meta: meta
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module YmlErrorResponder
|
2
|
+
module ErrorHandlers
|
3
|
+
class UnknowErrorHandler < BaseErrorHandler
|
4
|
+
def initialize
|
5
|
+
@handler_data = {
|
6
|
+
http_code: :internal_server_error,
|
7
|
+
error_code: :unknow_error,
|
8
|
+
description: 'Something went wrong.'
|
9
|
+
}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module YmlErrorResponder
|
2
|
+
module Responder
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
rescue_from Exception, with: :resque_global
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def resque_global(error)
|
12
|
+
if YmlMapper.error_defined?(error)
|
13
|
+
respond_with_error ErrorHandlers::handler(error, YmlMapper.error_payload(error))
|
14
|
+
else
|
15
|
+
|
16
|
+
log_error(error)
|
17
|
+
ExceptionNotifier.notify_exception(error) if defined?(ExceptionNotifier)
|
18
|
+
|
19
|
+
if YmlErrorResponder.handle_unknown_error
|
20
|
+
respond_with_error ErrorHandlers.unknown_error
|
21
|
+
else
|
22
|
+
raise error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def respond_with_error(handler)
|
28
|
+
respond_to do |format|
|
29
|
+
format.html do
|
30
|
+
render file: "#{Rails.root}/public/#{handler.http_code}",
|
31
|
+
layout: false,
|
32
|
+
status: handler.http_code
|
33
|
+
end
|
34
|
+
format.json do
|
35
|
+
render json: handler.as_json,
|
36
|
+
status: handler.http_code
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def log_error(error)
|
42
|
+
if YmlErrorResponder.unknown_error_logger.respond_to?(:call)
|
43
|
+
YmlErrorResponder.unknown_error_logger.call(error)
|
44
|
+
end
|
45
|
+
Rails.logger.error error.message
|
46
|
+
Rails.logger.error error.backtrace.join("\n")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class << ActionController::Base
|
52
|
+
def handle_errors_with_yml_responder
|
53
|
+
include YmlErrorResponder::Responder
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module YmlErrorResponder
|
2
|
+
module YmlMapper
|
3
|
+
extend self
|
4
|
+
attr_reader :errors_handlers
|
5
|
+
FILE_MASK = '**/*.yml'
|
6
|
+
|
7
|
+
@errors_handlers = HashWithIndifferentAccess.new
|
8
|
+
|
9
|
+
def error_payload(error)
|
10
|
+
@errors_handlers[error.class.to_s]
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_defined?(error)
|
14
|
+
@errors_handlers.key?(error.class.to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
def load!
|
18
|
+
raise 'Configuration path is not set!' if YmlErrorResponder.configuration_path.blank?
|
19
|
+
|
20
|
+
read_dir do |path|
|
21
|
+
add_handler(load_handlers(path, path_to_namespace(path)))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def read_dir
|
26
|
+
Dir[[YmlErrorResponder.configuration_path, FILE_MASK].join].each do |path|
|
27
|
+
yield path
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_handlers(path, namespace)
|
32
|
+
YAML
|
33
|
+
.load_file(path)
|
34
|
+
.try(:transform_keys) do |key|
|
35
|
+
namespace.blank? ? key.to_s.camelize : [namespace, key.to_s.camelize].join('::')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def path_to_namespace(path)
|
40
|
+
path.match(/#{YmlErrorResponder.configuration_path}([^.]*)\/(.*).yml/)
|
41
|
+
.try(:[], 1)
|
42
|
+
.try do |match|
|
43
|
+
match.split('/')
|
44
|
+
.map{|m| m.camelize}
|
45
|
+
.join('::')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def add_handler(handlers)
|
50
|
+
@errors_handlers.merge!(handlers) if handlers.present?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|