moromi-error 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +4 -0
- data/Gemfile +14 -0
- data/LICENSE +22 -0
- data/README.md +79 -0
- data/Rakefile +26 -0
- data/app/views/moromi/error/_default.html.erb +36 -0
- data/app/views/moromi/error/_default.html.slim +25 -0
- data/app/views/moromi/error/_default.json.jbuilder +5 -0
- data/app/views/moromi/error/_force_update.html.erb +36 -0
- data/app/views/moromi/error/_force_update.html.slim +25 -0
- data/app/views/moromi/error/_force_update.json.jbuilder +6 -0
- data/bin/rails +13 -0
- data/circle.yml +11 -0
- data/config/locales/moromi-error.yml +18 -0
- data/lib/generators/moromi/error/views_generator.rb +44 -0
- data/lib/moromi/error.rb +25 -0
- data/lib/moromi/error/config.rb +11 -0
- data/lib/moromi/error/default_logger.rb +16 -0
- data/lib/moromi/error/engine.rb +11 -0
- data/lib/moromi/error/errors.rb +87 -0
- data/lib/moromi/error/loggerable.rb +9 -0
- data/lib/moromi/error/renderer.rb +69 -0
- data/lib/moromi/error/rescue.rb +16 -0
- data/lib/moromi/error/version.rb +5 -0
- data/moromi-error.gemspec +32 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d6cb8a7613142458e4bf5c15502e7db79cd58a58
|
4
|
+
data.tar.gz: 1e911e13d9cdd10139ea996cea880ce248f66831
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d9ce279a8a3967c17ae0de23cff806592229b9587b104fd320bdb7d515388d4fc790edeac80827e72b092434377321705077e3d79dc542ec6cc6611ab4a0d1e0
|
7
|
+
data.tar.gz: 41f2e945a3c68b3356fe90a56ae41603d61e234a50cbaad1e4033cb8d09493e69e5dbba73fc4496c745cfa653dddd5fb75bcd8dd7f5f2532f88ee96227ecb39c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in moromi-error.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 moromi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Moromi::Error
|
2
|
+
|
3
|
+
[](http://rubygems.org/gems/moromi-error)
|
4
|
+
[](https://circleci.com/gh/moromi/moromi-error)
|
5
|
+
|
6
|
+
Error templates.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'moromi-error'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install moromi-error
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Initializers
|
27
|
+
|
28
|
+
- initializers/moromi/error.rb
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
Moromi::Error.configure do |config|
|
32
|
+
config.debug = true
|
33
|
+
config.store_url = 'https://itunes.apple.com/jp/app/idxxxxxxxxxx'
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
### for Controller
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class PagesController < ApplicationController
|
41
|
+
include Moromi::Error::Renderer
|
42
|
+
include Moromi::Error::Rescue
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
- `Moromi::Error::Renderer`
|
47
|
+
|
48
|
+
define render_xxx methods.
|
49
|
+
|
50
|
+
- `Moromi::Error::Rescue`
|
51
|
+
|
52
|
+
when raise Moromi::Error::Default or subclass, handle with `rescue_from`.
|
53
|
+
|
54
|
+
it's optional module.
|
55
|
+
|
56
|
+
### Copy Template
|
57
|
+
|
58
|
+
- jbuilder and erb
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
bundle exec rails g moromi:error:views -e erb
|
62
|
+
```
|
63
|
+
|
64
|
+
- jbuilder and slim
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
bundle exec rails g moromi:error:views -e slim
|
68
|
+
```
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/moromi/moromi-error.
|
79
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
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 = 'Moromi::Error'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="ja">
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8" />
|
5
|
+
<title><%= title %></title>
|
6
|
+
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport" />
|
7
|
+
<meta name="robots" content="noindex" />
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
<div>
|
11
|
+
<h1><%= status %> <%= title %></h1>
|
12
|
+
<% unless Rails.env.production? %>
|
13
|
+
<table>
|
14
|
+
<tbody>
|
15
|
+
<tr>
|
16
|
+
<td>code</td>
|
17
|
+
<td><%= exception.code %></td>
|
18
|
+
</tr>
|
19
|
+
|
20
|
+
<tr>
|
21
|
+
<td>debug_message</td>
|
22
|
+
<td><%= exception.debug_message %></td>
|
23
|
+
</tr>
|
24
|
+
|
25
|
+
<% exception.errors.each do |error| %>
|
26
|
+
<tr>
|
27
|
+
<td>error</td>
|
28
|
+
<td><%= error %></td>
|
29
|
+
</tr>
|
30
|
+
<% end %>
|
31
|
+
</tbody>
|
32
|
+
</table>
|
33
|
+
<% end %>
|
34
|
+
</div>
|
35
|
+
</body>
|
36
|
+
</html>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
doctype html
|
2
|
+
html lang="ja"
|
3
|
+
head
|
4
|
+
meta charset="utf-8"
|
5
|
+
title= title
|
6
|
+
meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
|
7
|
+
meta name="robots" content="noindex"
|
8
|
+
body
|
9
|
+
div
|
10
|
+
h1= "#{status} #{title}"
|
11
|
+
|
12
|
+
- unless Rails.env.production?
|
13
|
+
table
|
14
|
+
tbody
|
15
|
+
tr
|
16
|
+
td code
|
17
|
+
td= exception.code
|
18
|
+
tr
|
19
|
+
td debug_message
|
20
|
+
td= exception.debug_message
|
21
|
+
|
22
|
+
- exception.errors.each do |error|
|
23
|
+
tr
|
24
|
+
td error
|
25
|
+
td= error
|
@@ -0,0 +1,36 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="ja">
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8" />
|
5
|
+
<title>Need Force Update</title>
|
6
|
+
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport" />
|
7
|
+
<meta name="robots" content="noindex" />
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
<div>
|
11
|
+
<h1><%= status %> <%= title %></h1>
|
12
|
+
<% unless Rails.env.production? %>
|
13
|
+
<table>
|
14
|
+
<tbody>
|
15
|
+
<tr>
|
16
|
+
<td>code</td>
|
17
|
+
<td><%= exception.code %></td>
|
18
|
+
</tr>
|
19
|
+
|
20
|
+
<tr>
|
21
|
+
<td>debug_message</td>
|
22
|
+
<td><%= exception.debug_message %></td>
|
23
|
+
</tr>
|
24
|
+
|
25
|
+
<% exception.errors.each do |error| %>
|
26
|
+
<tr>
|
27
|
+
<td>error</td>
|
28
|
+
<td><%= error %></td>
|
29
|
+
</tr>
|
30
|
+
<% end %>
|
31
|
+
</tbody>
|
32
|
+
</table>
|
33
|
+
<% end %>
|
34
|
+
</div>
|
35
|
+
</body>
|
36
|
+
</html>
|
@@ -0,0 +1,25 @@
|
|
1
|
+
doctype html
|
2
|
+
html lang="ja"
|
3
|
+
head
|
4
|
+
meta charset="utf-8"
|
5
|
+
title Need Force Update
|
6
|
+
meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
|
7
|
+
meta name="robots" content="noindex"
|
8
|
+
body
|
9
|
+
div
|
10
|
+
h1= "#{status} #{title}"
|
11
|
+
|
12
|
+
- unless Rails.env.production?
|
13
|
+
table
|
14
|
+
tbody
|
15
|
+
tr
|
16
|
+
td code
|
17
|
+
td= exception.code
|
18
|
+
tr
|
19
|
+
td debug_message
|
20
|
+
td= exception.debug_message
|
21
|
+
|
22
|
+
- exception.errors.each do |error|
|
23
|
+
tr
|
24
|
+
td error
|
25
|
+
td= error
|
data/bin/rails
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
3
|
+
# installed from the root of your application.
|
4
|
+
|
5
|
+
ENGINE_ROOT = File.expand_path('../..', __FILE__)
|
6
|
+
ENGINE_PATH = File.expand_path('../../lib/moromi/error/engine', __FILE__)
|
7
|
+
|
8
|
+
# Set up gems listed in the Gemfile.
|
9
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
10
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
11
|
+
|
12
|
+
require 'rails/all'
|
13
|
+
require 'rails/engine/commands'
|
data/circle.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
en:
|
2
|
+
strings:
|
3
|
+
moromi-error:
|
4
|
+
an_error_has_occurred: 'An error has occurred.'
|
5
|
+
validation_error: 'Validation error.'
|
6
|
+
not_found: 'Not found.'
|
7
|
+
permission_denied: 'Permission denied.'
|
8
|
+
authentication_failed: 'Authentication failed.'
|
9
|
+
need_force_update: 'Need update.'
|
10
|
+
ja:
|
11
|
+
strings:
|
12
|
+
moromi-error:
|
13
|
+
an_error_has_occurred: 'エラーが発生しました。'
|
14
|
+
validation_error: '不正な入力です。'
|
15
|
+
not_found: '見つかりませんでした。'
|
16
|
+
permission_denied: '権限がありません。'
|
17
|
+
authentication_failed: '認証に失敗しました。'
|
18
|
+
need_force_update: '新しいバージョンのアプリがあります。'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Moromi
|
2
|
+
module Error
|
3
|
+
module Generators
|
4
|
+
class ViewsGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../../../../../app/views/moromi/error', __FILE__)
|
6
|
+
|
7
|
+
class_option :template_engine, type: :string, aliases: '-e', desc: 'Template engine for the views. Available options are "erb" and "slim".'
|
8
|
+
|
9
|
+
def self.banner #:nodoc:
|
10
|
+
<<-BANNER.chomp
|
11
|
+
rails g moromi:error:views [options]
|
12
|
+
Copies all partial templates to your application.
|
13
|
+
BANNER
|
14
|
+
end
|
15
|
+
|
16
|
+
desc ''
|
17
|
+
def copy_or_fetch #:nodoc:
|
18
|
+
copy_html_templates
|
19
|
+
copy_json_templates
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def copy_html_templates
|
25
|
+
filename_pattern = File.join self.class.source_root, "*.html.#{template_engine}"
|
26
|
+
Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f|
|
27
|
+
copy_file f, "app/views/moromi/error/#{f}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def copy_json_templates
|
32
|
+
filename_pattern = File.join self.class.source_root, "*.json.jbuilder"
|
33
|
+
Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f|
|
34
|
+
copy_file f, "app/views/moromi/error/#{f}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def template_engine
|
39
|
+
options[:template_engine]&.to_s&.downcase || 'erb'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/moromi/error.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'moromi/error/config'
|
2
|
+
require 'moromi/error/loggerable'
|
3
|
+
require 'moromi/error/default_logger'
|
4
|
+
require 'moromi/error/engine'
|
5
|
+
require 'moromi/error/errors'
|
6
|
+
require 'moromi/error/renderer'
|
7
|
+
require 'moromi/error/rescue'
|
8
|
+
|
9
|
+
module Moromi
|
10
|
+
module Error
|
11
|
+
def self.configure(&block)
|
12
|
+
yield @config ||= Config.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.config
|
16
|
+
@config
|
17
|
+
end
|
18
|
+
|
19
|
+
configure do |config|
|
20
|
+
config.debug = false
|
21
|
+
config.logger = Moromi::Error::DefaultLogger.new
|
22
|
+
config.store_url = ''
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Moromi
|
2
|
+
module Error
|
3
|
+
class DefaultLogger
|
4
|
+
include Loggerable
|
5
|
+
|
6
|
+
def write(status, title, exception, options, locals)
|
7
|
+
return if exception.is_a? Moromi::Error::Default
|
8
|
+
|
9
|
+
messages = ['[ERROR]']
|
10
|
+
messages << ([exception.inspect] + (exception&.backtrace || [])).compact.join("\n").gsub("\n", '\n')
|
11
|
+
|
12
|
+
Rails.logger.error(messages.join(' '))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Moromi::Error
|
3
|
+
class Default < StandardError
|
4
|
+
DEFAULT_CODE = 0
|
5
|
+
DEFAULT_TITLE = 'moromi-error.an_error_has_occurred'
|
6
|
+
DEFAULT_ERRORS = ['moromi-error.an_error_has_occurred']
|
7
|
+
|
8
|
+
attr_reader :code
|
9
|
+
attr_reader :title
|
10
|
+
attr_reader :errors
|
11
|
+
|
12
|
+
# @param [Integer] code
|
13
|
+
# @param [String] title
|
14
|
+
# @param [Array<String>] errors
|
15
|
+
# @param [String] message
|
16
|
+
# @param [String] debug_message
|
17
|
+
def initialize(code: nil, title: nil, errors: self.class::DEFAULT_ERRORS, message: nil, debug_message: nil)
|
18
|
+
super(message)
|
19
|
+
@code = code || self.class::DEFAULT_CODE
|
20
|
+
@title = translate(title || self.class::DEFAULT_TITLE)
|
21
|
+
@errors = Array(errors).map { |error| translate(error) }
|
22
|
+
@debug_message = debug_message if Moromi::Error.config.debug
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [String]
|
26
|
+
def debug_message
|
27
|
+
return '' unless Moromi::Error.config.debug
|
28
|
+
@debug_message || try(:backtrace).try(:first) || ''
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param [Exception] exception
|
32
|
+
# @return [Moromi::Error::Default]
|
33
|
+
def self.make(exception)
|
34
|
+
return exception if exception.is_a? Default
|
35
|
+
new(debug_message: exception.message)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
# @param [String] key
|
41
|
+
# @return [String]
|
42
|
+
def translate(key)
|
43
|
+
I18n.translate(key, scope: [:strings], default: key.to_s)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class ValidationError < Default
|
48
|
+
DEFAULT_CODE = 10000
|
49
|
+
DEFAULT_ERRORS = ['moromi-error.validation_error']
|
50
|
+
|
51
|
+
# @param [Exception] exception
|
52
|
+
# @return [Moromi::Error::Default]
|
53
|
+
def self.make(exception)
|
54
|
+
if defined?(ActiveRecord::RecordInvalid) && exception.is_a?(ActiveRecord::RecordInvalid)
|
55
|
+
return new(debug_message: exception.try(:backtrace), message: exception.message, errors: [exception.message])
|
56
|
+
end
|
57
|
+
|
58
|
+
if defined?(WeakParameters::ValidationError) && exception.is_a?(WeakParameters::ValidationError)
|
59
|
+
return new(debug_message: exception.message, message: exception.message)
|
60
|
+
end
|
61
|
+
|
62
|
+
super(exception)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class NotFound < Default
|
67
|
+
DEFAULT_CODE = 10001
|
68
|
+
DEFAULT_ERRORS = ['moromi-error.not_found']
|
69
|
+
end
|
70
|
+
|
71
|
+
class PermissionDenied < Default
|
72
|
+
DEFAULT_CODE = 10002
|
73
|
+
DEFAULT_ERRORS = ['moromi-error.permission_denied']
|
74
|
+
end
|
75
|
+
|
76
|
+
class AuthenticationFailed < Default
|
77
|
+
DEFAULT_CODE = 10003
|
78
|
+
DEFAULT_ERRORS = ['moromi-error.authentication_failed']
|
79
|
+
end
|
80
|
+
|
81
|
+
class NeedForceUpdate < Default
|
82
|
+
DEFAULT_CODE = 10004
|
83
|
+
DEFAULT_ERRORS = ['moromi-error.need_force_update']
|
84
|
+
|
85
|
+
attr_accessor :store_url
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Moromi::Error
|
3
|
+
module Renderer
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
ERROR_TEMPLATES = {
|
7
|
+
default: 'moromi/error/default',
|
8
|
+
force_update: 'moromi/error/force_update'
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
included do
|
12
|
+
class_attribute :default_moromi_error_renderer_options
|
13
|
+
class_attribute :moromi_error_logger
|
14
|
+
|
15
|
+
self.default_moromi_error_renderer_options = {partial: ERROR_TEMPLATES[:default], layout: false}
|
16
|
+
self.moromi_error_logger = Moromi::Error::DefaultLogger.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def render_bad_request(exception: Moromi::Error::Default.new, options: nil, locals: {})
|
20
|
+
render_error(400, 'Bad Request', exception, options: options, locals: locals)
|
21
|
+
end
|
22
|
+
|
23
|
+
def render_unauthorized(exception: Moromi::Error::Default.new, options: nil, locals: {})
|
24
|
+
render_error(401, 'Unauthorized', exception, options: options, locals: locals)
|
25
|
+
end
|
26
|
+
|
27
|
+
def render_forbidden(exception: Moromi::Error::Default.new, options: nil, locals: {})
|
28
|
+
render_error(403, 'Forbidden', exception, options: options, locals: locals)
|
29
|
+
end
|
30
|
+
|
31
|
+
def render_not_found(exception: Moromi::Error::Default.new, options: nil, locals: {})
|
32
|
+
render_error(404, 'Not Found', exception, options: options, locals: locals)
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_conflict(exception: Moromi::Error::Default.new, options: nil, locals: {})
|
36
|
+
render_error(409, 'Conflict', exception, options: options, locals: locals)
|
37
|
+
end
|
38
|
+
|
39
|
+
def render_gone(exception: Moromi::Error::Default.new, options: nil, locals: {})
|
40
|
+
render_error(410, 'Gone', exception, options: options, locals: locals)
|
41
|
+
end
|
42
|
+
|
43
|
+
def render_force_update(exception: Moromi::Error::NeedForceUpdate.new, options: nil, locals: {})
|
44
|
+
options = options || {partial: ERROR_TEMPLATES[:force_update], layout: false}
|
45
|
+
render_bad_request(exception: exception, options: options, locals: locals)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def render_error(status, title, exception, options: nil, locals: {})
|
51
|
+
options = options || self.class::default_moromi_error_renderer_options
|
52
|
+
e = Moromi::Error::Default.make(exception)
|
53
|
+
|
54
|
+
self.class::moromi_error_logger.write(status, title, exception, options, locals)
|
55
|
+
|
56
|
+
options = {status: status}.merge(options)
|
57
|
+
locals = {status: status, title: title, exception: e}.merge(locals)
|
58
|
+
|
59
|
+
render_block = -> {
|
60
|
+
render options.merge({locals: locals})
|
61
|
+
}
|
62
|
+
|
63
|
+
respond_to do |format|
|
64
|
+
format.html &render_block
|
65
|
+
format.json &render_block
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Moromi
|
2
|
+
module Error
|
3
|
+
module Rescue
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
rescue_from Moromi::Error::Default, with: -> (e) { render_bad_request(exception: e) }
|
8
|
+
rescue_from Moromi::Error::ValidationError, with: -> (e) { render_bad_request(exception: e) }
|
9
|
+
rescue_from Moromi::Error::NotFound, with: -> (e) { render_not_found(exception: e) }
|
10
|
+
rescue_from Moromi::Error::PermissionDenied, with: -> (e) { render_forbidden(exception: e) }
|
11
|
+
rescue_from Moromi::Error::AuthenticationFailed, with: -> (e) { render_unauthorized(exception: e) }
|
12
|
+
rescue_from Moromi::Error::NeedForceUpdate, with: -> (e) { e.store_url = Moromi::Error.config.store_url; render_force_update(exception: e) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'moromi/error/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "moromi-error"
|
8
|
+
spec.version = Moromi::Error::VERSION
|
9
|
+
spec.authors = ["Takahiro Ooishi"]
|
10
|
+
spec.email = ["taka0125@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Error templates}
|
13
|
+
spec.description = %q{Error templates}
|
14
|
+
spec.homepage = "https://github.com/moromi/moromi-error"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.3'
|
23
|
+
|
24
|
+
spec.add_dependency 'rails', ['>= 4.2', '< 6']
|
25
|
+
spec.add_dependency 'jbuilder'
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "rspec-rails"
|
31
|
+
spec.add_development_dependency "sqlite3"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moromi-error
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takahiro Ooishi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: jbuilder
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.12'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.12'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec-rails
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: sqlite3
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
description: Error templates
|
118
|
+
email:
|
119
|
+
- taka0125@gmail.com
|
120
|
+
executables: []
|
121
|
+
extensions: []
|
122
|
+
extra_rdoc_files: []
|
123
|
+
files:
|
124
|
+
- ".gitignore"
|
125
|
+
- ".rspec"
|
126
|
+
- Gemfile
|
127
|
+
- LICENSE
|
128
|
+
- README.md
|
129
|
+
- Rakefile
|
130
|
+
- app/views/moromi/error/_default.html.erb
|
131
|
+
- app/views/moromi/error/_default.html.slim
|
132
|
+
- app/views/moromi/error/_default.json.jbuilder
|
133
|
+
- app/views/moromi/error/_force_update.html.erb
|
134
|
+
- app/views/moromi/error/_force_update.html.slim
|
135
|
+
- app/views/moromi/error/_force_update.json.jbuilder
|
136
|
+
- bin/rails
|
137
|
+
- circle.yml
|
138
|
+
- config/locales/moromi-error.yml
|
139
|
+
- lib/generators/moromi/error/views_generator.rb
|
140
|
+
- lib/moromi/error.rb
|
141
|
+
- lib/moromi/error/config.rb
|
142
|
+
- lib/moromi/error/default_logger.rb
|
143
|
+
- lib/moromi/error/engine.rb
|
144
|
+
- lib/moromi/error/errors.rb
|
145
|
+
- lib/moromi/error/loggerable.rb
|
146
|
+
- lib/moromi/error/renderer.rb
|
147
|
+
- lib/moromi/error/rescue.rb
|
148
|
+
- lib/moromi/error/version.rb
|
149
|
+
- moromi-error.gemspec
|
150
|
+
homepage: https://github.com/moromi/moromi-error
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
metadata: {}
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '2.3'
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 2.5.1
|
171
|
+
signing_key:
|
172
|
+
specification_version: 4
|
173
|
+
summary: Error templates
|
174
|
+
test_files: []
|