restful_error 1.0.3 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4744dd451535b3d85e7ce8b47a76abee9fa33970a72d6748e553f89bb830e9e5
4
- data.tar.gz: 6cfc09fbf86294c2453a3effeb1537f83a9c8fef8bed444f24916e3f45c2d470
3
+ metadata.gz: cf170d758ce3175fe4b56ec695fbf4be09569474e66b376df7395084b8babda5
4
+ data.tar.gz: df9042f7e3a5ab6d80b7c861e61f35604f894715226c2b2b87bd4ccbff610ad9
5
5
  SHA512:
6
- metadata.gz: 6d339f3e39861bfdc9bf64fc983d9b03e15708f4323d3f9987da1ae70c7bc973b8cfecb7cc7568a1078957f7eef6ba9a8e1f5c533f50a734b2fcc0005f5c2170
7
- data.tar.gz: cbbf4ee06b69b396f79cd351c2659458422090d927af7584f31eab4e8e4fe1bcc4019cb21b90f2695ddbfe2683199afd412a4f6bad11ddf61365cb182a26f55f
6
+ metadata.gz: 1b649d3d114cfc2dfb0eefd9c96a0e69be07715d3e2c9517dda75b0d80ad0d08e05f07ed968ecb5f74bb51da4b3d68dfdc512097d78c5d030a9343b16d922ad6
7
+ data.tar.gz: b9440442b318f6d64958c14e0252d26a628cb8545c03d716464f10493e016c9a6848b1636dd6f0321b0bcf605833e3bc20db0188da6096d64177f390a47f896b
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2025 Ohkubo KOHEI
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ [![Gem Version](https://badge.fury.io/rb/restful_error.svg)](https://badge.fury.io/rb/restful_error)
2
+
3
+ # RestfulError
4
+
5
+ Define your error with status code. Raise it and you will get formatted response with i18nized message.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'restful_error'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ### Pure ruby
20
+ ```ruby
21
+ ex = RestfulError[404].new
22
+ ex.restful.code # => 404
23
+ ex.restful.reason_phrase # => "Not Found"
24
+ ```
25
+
26
+ #### your custom error by subclassing
27
+ ```ruby
28
+ class ::NoSession < RestfulError[404]; end
29
+ # or
30
+ class ::NoSession < RestfulError::NotFound; end
31
+ ```
32
+ #### your custom error with http_status
33
+ ```ruby
34
+ class OAuthController < ApplicationController
35
+
36
+ # define http_status and include RestfulError::Helper
37
+ class PermissionError < StandardError
38
+ include RestfulError::Helper
39
+ def http_status = 401
40
+ end
41
+ # or
42
+ class PermissionError < StandardError
43
+ include RestfulError::Helper
44
+ def http_status; :unauthorized; end
45
+ end
46
+ end
47
+ PermissionError.new.restful.reason_phrase # => "Unauthorized"
48
+ ```
49
+
50
+ ### With Rails
51
+ `config.exceptions_app` will automatically set.
52
+
53
+ #### raise me in request
54
+ ```ruby
55
+ class PostsController < ApplicationController
56
+ before_action do
57
+ raise RestfulError[401] unless current_user
58
+ # or
59
+ raise RestfulError::Unauthorized unless current_user
60
+ end
61
+ end
62
+ ```
63
+
64
+
65
+ #### Multi format response
66
+
67
+ ```ruby
68
+ get '/posts/new'
69
+ #=> render 'restful_error/show.html' with @status_code and @message
70
+
71
+ post '/posts.json'
72
+ #=> { status_code: 401, message: "Sign in required" } or write your json at 'restful_error/show.json'
73
+
74
+ get '/session.xml'
75
+ #=> "<error><status_code type="integer">401</status_code><message>Sign in required</message></error>" or write your xml at 'restful_error/show.xml'
76
+ ```
77
+
78
+ #### I18n
79
+
80
+ ```yaml
81
+ ja:
82
+ restful_error:
83
+ unauthorized: ログインしてください #401
84
+ not_found: ページが存在しません #404
85
+ ```
86
+
87
+ #### library defined error
88
+ ``` ruby
89
+ config.action_dispatch.rescue_responses["CanCan::Unauthorized"] = 401
90
+ # or
91
+ config.action_dispatch.rescue_responses["CanCan::Unauthorized"] = :unauthorized
92
+ ```
93
+ #### I18n
94
+ ```yaml
95
+ ja:
96
+ restful_error:
97
+ no_session: セッションがありません
98
+ oauth_controller/require_twitter_login: Twitterログインが必要です
99
+ can_can/unauthorized: 権限がありません
100
+ active_record/record_not_found: 要求されたリソースが存在しません
101
+ ```
102
+ #### custom message
103
+
104
+ ```ruby
105
+ class RequireLogin < StandardError
106
+ def initialize(provider = 'Unknown')
107
+ @provider = provider
108
+ end
109
+ def status_code
110
+ :unauthorized
111
+ end
112
+ def response_message
113
+ I18n.t('restful_error.require_login', provider: provider)
114
+ end
115
+ end
116
+ ```
117
+
118
+ ## Contributing
119
+
120
+ 1. Fork it ( https://github.com/kuboon/restful_error/fork )
121
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
122
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
123
+ 4. Push to the branch (`git push origin my-new-feature`)
124
+ 5. Create a new Pull Request
@@ -0,0 +1,4 @@
1
+ <div class="'page_header">
2
+ <h1><%= @status_code %> <%= @reason_phrase %></h1>
3
+ <p><%= @response_message %></p>
4
+ </div>
@@ -0,0 +1,7 @@
1
+ <%=
2
+ raw JSON.generate(
3
+ status_code: @status_code,
4
+ reason_phrase: @reason_phrase,
5
+ response_message: @response_message
6
+ )
7
+ %>
@@ -0,0 +1,5 @@
1
+ <error>
2
+ <status_code><%= @status_code %></status_code>
3
+ <reason_phrase><%= @reason_phrase %></reason_phrase>
4
+ <response_message><%= @response_message %></response_message>
5
+ </error>
@@ -0,0 +1,14 @@
1
+ en:
2
+ restful_error:
3
+ active_record/record_not_found: Requested resource is not found
4
+ action_controller/routing_error: Requested resource is not found
5
+ can_can/unauthorized: Unauthorized
6
+ bad_request: Bad request #400
7
+ unauthorized: Unauthorized #401
8
+ forbidden: Forbidden #403
9
+ not_found: Page not found #404
10
+ method_not_allowed: Requested HTTP Method is not allowd #405
11
+ gone: Gone #410
12
+ unprocessable_content: Content is readable but unprocessable #422
13
+ internal_server_error: Internal server error #500
14
+ service_unavailable: Service unavailable #503
@@ -0,0 +1,13 @@
1
+ ja:
2
+ restful_error:
3
+ active_record/record_not_found: 要求されたリソースが存在しません
4
+ can_can/unauthorized: 権限がありません
5
+ bad_request: 不正なリクエスト #400
6
+ unauthorized: 権限がありません #401
7
+ forbidden: そのURLへのアクセスは禁止されています #403
8
+ not_found: そのURLは存在しません #404
9
+ method_not_allowed: その HTTP method は使用できません #405
10
+ gone: 要求されたリソースは消滅しました #410
11
+ unprocessable_content: コンテンツを読みましたが処理できません #422
12
+ internal_server_error: サーバエラーです #500
13
+ service_unavailable: サービスが一時的に利用不可能になっています。しばらく時間をおいて、再度ご確認願います。 #503
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RestfulError
4
- VERSION = "1.0.3"
4
+ VERSION = "1.0.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kuboon
@@ -38,14 +38,18 @@ executables: []
38
38
  extensions: []
39
39
  extra_rdoc_files: []
40
40
  files:
41
+ - LICENSE.txt
42
+ - README.md
43
+ - app/views/restful_error/show.html.erb
44
+ - app/views/restful_error/show.json.erb
45
+ - app/views/restful_error/show.xml.erb
46
+ - config/locales/en.restful_error.yml
47
+ - config/locales/ja.restful_error.yml
41
48
  - lib/restful_error.rb
42
49
  - lib/restful_error/inflector.rb
43
50
  - lib/restful_error/railtie.rb
44
51
  - lib/restful_error/status.rb
45
52
  - lib/restful_error/version.rb
46
- - spec/restful_error_spec.rb
47
- - spec/spec_helper.rb
48
- - spec/with_rails/exceptions_app_spec.rb
49
53
  homepage: https://github.com/kuboon/restful_error
50
54
  licenses:
51
55
  - MIT
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe RestfulError do
6
- describe "RestfullError[404]" do
7
- subject { described_class[404].new }
8
-
9
- it do
10
- expect(subject.http_status).to eq 404
11
- expect(subject.restful.reason_phrase).to eq "Not Found"
12
- end
13
- end
14
-
15
- describe "RestfullError[:bad_request]" do
16
- subject { described_class[:bad_request].new }
17
-
18
- it do
19
- expect(subject.http_status).to eq 400
20
- expect(subject.restful.reason_phrase).to eq "Bad Request"
21
- end
22
- end
23
-
24
- describe described_class::Forbidden do
25
- subject { described_class.new }
26
-
27
- it do
28
- expect(subject.http_status).to eq 403
29
- expect(subject.restful.reason_phrase).to eq "Forbidden"
30
- end
31
- end
32
-
33
- describe "custom class" do
34
- subject { klass.new }
35
-
36
- let(:klass) do
37
- Class.new(StandardError) do
38
- include RestfulError::Helper
39
- def http_status = 404
40
- end
41
- end
42
-
43
- it do
44
- expect(subject.restful.symbol).to eq :not_found
45
- end
46
- end
47
- end
data/spec/spec_helper.rb DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
-
5
- require "debug"
6
- require "restful_error"
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "action_controller"
4
- require "i18n"
5
- require "spec_helper"
6
- require "restful_error/railtie" # require again for run after restful_error_spec
7
-
8
- RSpec.describe RestfulError::ExceptionsController do
9
- include Rack::Test::Methods
10
- def app = RestfulError.exceptions_app
11
-
12
- shared_context "json" do
13
- let(:request) { get "/#{status_code}", {}, 'HTTP_ACCEPT' => 'application/json' }
14
- let(:json) { request; JSON.parse(last_response.body) }
15
- end
16
-
17
- before do
18
- env "action_dispatch.exception", exception
19
- end
20
- describe RestfulError[404] do
21
- let(:status_code) { 404 }
22
- include_context "json" do
23
- context 'default message' do
24
- let(:exception) { described_class.new }
25
- it do
26
- expect(json).to eq({status_code: 404, reason_phrase: "Not Found", response_message: 'Page not found'}.stringify_keys)
27
- expect(last_response.status).to eq status_code
28
- end
29
- end
30
- context 'custom message' do
31
- let(:exception) { described_class.new("custom message") }
32
- it do
33
- expect(json).to eq({status_code:, reason_phrase: "Not Found", response_message: "custom message"}.stringify_keys)
34
- end
35
- end
36
- end
37
- end
38
- context ActionController::RoutingError do
39
- let(:exception) { described_class.new("no route") }
40
- let(:status_code) { 404 }
41
- include_context "json" do
42
- it do
43
- expect(json).to eq({status_code:, reason_phrase: "Not Found", response_message: 'Requested resource is not found'}.stringify_keys)
44
- expect(last_response.status).to eq status_code
45
- end
46
- end
47
- end
48
- end