sinatra-errorcodes 0.1.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +40 -10
- data/example/base.rb +3 -5
- data/lib/4xx_errors.rb +75 -43
- data/lib/5xx_errors.rb +39 -12
- data/lib/error_base.rb +4 -3
- data/lib/sinatra/errorcodes.rb +6 -4
- data/lib/sinatra/errorcodes/version.rb +1 -1
- data/spec/sinatra/errorcodes_spec.rb +0 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 890a05336d2bddbd8d7d3853e026a208b0ba2945
|
4
|
+
data.tar.gz: f36c90ad4904d41dd79995f6a165ebc140a291e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 283b8983a22483d30ce378c144f69ee4aca0187b9cd5305192fd9d8889b9e5e0dd5cd3d987b2193dd857802c2dadf87423694e81f8eefec829374723b897e543
|
7
|
+
data.tar.gz: 2d986cb131b7c770a374f13b738b7c0df41acb470d9e895416a4681987d5918bbc53a35ac5a63cc6d946ec2df3c935b2505c1ee2f36750b4e2ce5a7865b7f334
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -4,13 +4,13 @@
|
|
4
4
|
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
|
5
5
|
> Gem of HTTP error status code class pack
|
6
6
|
|
7
|
-
=======
|
8
7
|
## Table of Contents
|
9
|
-
- [Installation](#Installation)
|
10
|
-
- [
|
11
|
-
- [
|
12
|
-
- [
|
13
|
-
- [
|
8
|
+
- [Installation](README.md#Installation)
|
9
|
+
- [Usage](README.md#Usage)
|
10
|
+
- [Example](README.md#Example)
|
11
|
+
- [Test](README.md#Test)
|
12
|
+
- [Contribute](README.md#Contribute)
|
13
|
+
- [License](README.md#License)
|
14
14
|
|
15
15
|
## Installation
|
16
16
|
Add this line to your application's Gemfile:
|
@@ -24,6 +24,38 @@ Or install it yourself as:
|
|
24
24
|
$ gem install sinatra-errorcodes
|
25
25
|
```
|
26
26
|
|
27
|
+
## Usage
|
28
|
+
To use this gem, at first, you need to register `Sinatra::Errorcodes` in your configure block and call `handle_errorstatus` which wraps Sinatra's error handling block.
|
29
|
+
```ruby
|
30
|
+
class AppController < Sinatra::Base
|
31
|
+
configure do
|
32
|
+
register Sinatra::Errorcodes
|
33
|
+
|
34
|
+
handle_errorstatus # Need to add this.
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Now you can use a set of `HTTPError` class in order to halt manually with a specific HTTP status code and its message like below. The all process of these exception raising is done by `handle_errorstatus` you have just added above.
|
40
|
+
```ruby
|
41
|
+
...
|
42
|
+
|
43
|
+
get '/badrequest'
|
44
|
+
raise HTTPError::BadRequest
|
45
|
+
end
|
46
|
+
|
47
|
+
get '/error'
|
48
|
+
raise HTTPError::InternalServerError
|
49
|
+
end
|
50
|
+
|
51
|
+
# You can also raise an exception with your own custom message.
|
52
|
+
get '/custom'
|
53
|
+
raise HTTPError::InternalServerError, 'Custom error message'
|
54
|
+
end
|
55
|
+
|
56
|
+
...
|
57
|
+
```
|
58
|
+
|
27
59
|
## Example
|
28
60
|
Install gems
|
29
61
|
```bash
|
@@ -37,14 +69,12 @@ $ bundle exec rackup -p 3000
|
|
37
69
|
```
|
38
70
|
|
39
71
|
## Test
|
40
|
-
Default task in Rakefile is test
|
41
72
|
```bash
|
42
|
-
$ bundle exec rake
|
73
|
+
$ bundle exec rake spec
|
43
74
|
```
|
44
75
|
|
45
76
|
## Contribute
|
46
|
-
|
47
|
-
|
77
|
+
PRs accepted.
|
48
78
|
|
49
79
|
## License
|
50
80
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/example/base.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
class BaseController < Sinatra::Base
|
2
2
|
configure do
|
3
|
-
|
3
|
+
register Sinatra::Errorcodes
|
4
|
+
|
5
|
+
handle_errorstatus
|
4
6
|
|
5
7
|
set :raise_errors, false
|
6
8
|
set :show_exceptions, false
|
7
9
|
end
|
8
|
-
|
9
|
-
error do |e|
|
10
|
-
handle_errorcode(e)
|
11
|
-
end
|
12
10
|
end
|
data/lib/4xx_errors.rb
CHANGED
@@ -1,108 +1,140 @@
|
|
1
1
|
require_relative "./error_base"
|
2
2
|
|
3
3
|
module HTTPError
|
4
|
+
|
5
|
+
# 400 Bad Request
|
4
6
|
class BadRequest < ErrorBase
|
5
|
-
CODE =
|
6
|
-
MESSAGE = "Bad Request"
|
7
|
+
CODE = Rack::Utils.status_code(:bad_request)
|
7
8
|
end
|
8
9
|
|
10
|
+
# 401 Unauthorized
|
9
11
|
class Unauthorized < ErrorBase
|
10
|
-
CODE =
|
11
|
-
MESSAGE = "Unauthorized"
|
12
|
+
CODE = Rack::Utils.status_code(:unauthorized)
|
12
13
|
end
|
13
14
|
|
15
|
+
# 402 Payment Required
|
14
16
|
class PaymentRequired < ErrorBase
|
15
|
-
CODE =
|
16
|
-
MESSAGE = "Payment Required"
|
17
|
+
CODE = Rack::Utils.status_code(:payment_required)
|
17
18
|
end
|
18
19
|
|
20
|
+
# 403 Forbidden
|
19
21
|
class Forbidden < ErrorBase
|
20
|
-
CODE =
|
21
|
-
MESSAGE = "Forbidden"
|
22
|
+
CODE = Rack::Utils.status_code(:forbidden)
|
22
23
|
end
|
23
24
|
|
25
|
+
# 404 Not Found
|
24
26
|
class NotFound < ErrorBase
|
25
|
-
CODE =
|
26
|
-
MESSAGE = "Not Found"
|
27
|
+
CODE = Rack::Utils.status_code(:not_found)
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
# 405 Method Not Allowed
|
31
|
+
class MethodNotAllowed < ErrorBase
|
32
|
+
CODE = Rack::Utils.status_code(:method_not_allowed)
|
32
33
|
end
|
33
34
|
|
35
|
+
# 406 Not Acceptable
|
34
36
|
class NotAccpetable < ErrorBase
|
35
|
-
CODE =
|
36
|
-
MESSAGE = "Not Acceptable"
|
37
|
+
CODE = Rack::Utils.status_code(:not_acceptable)
|
37
38
|
end
|
38
39
|
|
40
|
+
# 407 Proxy Authentication Required
|
39
41
|
class ProxyAuthenticationRequired < ErrorBase
|
40
|
-
CODE =
|
41
|
-
MESSAGE = "Proxy Authentication Required"
|
42
|
+
CODE = Rack::Utils.status_code(:proxy_authentication_required)
|
42
43
|
end
|
43
44
|
|
45
|
+
# 408 Request Timeout
|
44
46
|
class RequestTimeout < ErrorBase
|
45
|
-
CODE =
|
46
|
-
MESSAGE = "Request Timeout"
|
47
|
+
CODE = Rack::Utils.status_code(:request_timeout)
|
47
48
|
end
|
48
49
|
|
50
|
+
# 409 Conflict
|
49
51
|
class Conflict < ErrorBase
|
50
|
-
CODE =
|
51
|
-
MESSAGE = "Conflict"
|
52
|
+
CODE = Rack::Utils.status_code(:conflict)
|
52
53
|
end
|
53
54
|
|
55
|
+
# 410 Gone
|
54
56
|
class Gone < ErrorBase
|
55
|
-
CODE =
|
56
|
-
MESSAGE = "Gone"
|
57
|
+
CODE = Rack::Utils.status_code(:gone)
|
57
58
|
end
|
58
59
|
|
60
|
+
# 411 Length Required
|
59
61
|
class LengthRequired < ErrorBase
|
60
|
-
CODE =
|
61
|
-
MESSAGE = "Length Required"
|
62
|
+
CODE = Rack::Utils.status_code(:length_required)
|
62
63
|
end
|
63
64
|
|
65
|
+
# 412 Precondition Failed
|
64
66
|
class PreconditionFailed < ErrorBase
|
65
|
-
CODE =
|
66
|
-
MESSAGE = "Precondition Failed"
|
67
|
+
CODE = Rack::Utils.status_code(:precondition_failed)
|
67
68
|
end
|
68
69
|
|
70
|
+
# 413 Payload Too Large
|
69
71
|
class PayloadTooLarge < ErrorBase
|
70
|
-
CODE =
|
71
|
-
MESSAGE = "Payload Too Large"
|
72
|
+
CODE = Rack::Utils.status_code(:payload_too_large)
|
72
73
|
end
|
73
74
|
|
75
|
+
# 414 URI Too Long
|
74
76
|
class UriTooLong < ErrorBase
|
75
|
-
CODE =
|
76
|
-
MESSAGE = "URI Too Long"
|
77
|
+
CODE = Rack::Utils.status_code(:uri_too_long)
|
77
78
|
end
|
78
79
|
|
80
|
+
# 415 Unsupported Media Type
|
79
81
|
class UnsupportedMediaType < ErrorBase
|
80
|
-
CODE =
|
81
|
-
MESSAGE = "Unsupported Media Type"
|
82
|
+
CODE = Rack::Utils.status_code(:unsupported_media_type)
|
82
83
|
end
|
83
84
|
|
85
|
+
# 416 Range Not Satisfiable
|
84
86
|
class RangeNotSatisfiable < ErrorBase
|
85
|
-
CODE =
|
86
|
-
MESSAGE = "Range Not Satisfiable"
|
87
|
+
CODE = Rack::Utils.status_code(:range_not_satisfiable)
|
87
88
|
end
|
88
89
|
|
90
|
+
# 417 Expectation Failed
|
89
91
|
class ExpectationFailed < ErrorBase
|
90
|
-
CODE =
|
91
|
-
MESSAGE = "Expectation Failed"
|
92
|
+
CODE = Rack::Utils.status_code(:expectation_failed)
|
92
93
|
end
|
93
94
|
|
95
|
+
# 421 Misdirected Request
|
94
96
|
class MisdirectedRequest < ErrorBase
|
95
|
-
CODE =
|
96
|
-
|
97
|
+
CODE = Rack::Utils.status_code(:misdirected_failed)
|
98
|
+
end
|
99
|
+
|
100
|
+
# 422 Unprocessable Entity
|
101
|
+
class UnprocessableEntity < ErrorBase
|
102
|
+
CODE = Rack::Utils.status_code(:unprocessable_entity)
|
103
|
+
end
|
104
|
+
|
105
|
+
# 423 Locked
|
106
|
+
class Locked < ErrorBase
|
107
|
+
CODE = Rack::Utils.status_code(:locked)
|
108
|
+
end
|
109
|
+
|
110
|
+
# 424 Failed Dependency
|
111
|
+
class FailedDependency < ErrorBase
|
112
|
+
CODE = Rack::Utils.status_code(:failed_dependency)
|
97
113
|
end
|
98
114
|
|
115
|
+
# 426 Upgrade Required
|
99
116
|
class UpgradeRequired < ErrorBase
|
100
|
-
CODE =
|
101
|
-
MESSAGE = "Upgrade Required"
|
117
|
+
CODE = Rack::Utils.status_code(:upgrade_required)
|
102
118
|
end
|
103
119
|
|
120
|
+
# 428 Precondition Required
|
121
|
+
class PreconditionRequired < ErrorBase
|
122
|
+
CODE = Rack::Utils.status_code(:precondition_required)
|
123
|
+
end
|
124
|
+
|
125
|
+
# 429 Too Many Requests
|
126
|
+
class TooManyRequests < ErrorBase
|
127
|
+
CODE = Rack::Utils.status_code(:too_many_requests)
|
128
|
+
end
|
129
|
+
|
130
|
+
# 431 Request Header Fields Too Large
|
131
|
+
class RequestHeaderFieldsTooLarge < ErrorBase
|
132
|
+
CODE = Rack::Utils.status_code(:request_header_fields_too_large)
|
133
|
+
end
|
134
|
+
|
135
|
+
# 451 Unavailable For Legal Reasons
|
104
136
|
class UnavailableForLegalReasons < ErrorBase
|
105
|
-
CODE =
|
106
|
-
MESSAGE = "Unavailable For Legal Reasons"
|
137
|
+
CODE = Rack::Utils.status_code(:unavailable_for_legal_reasons)
|
107
138
|
end
|
139
|
+
|
108
140
|
end
|
data/lib/5xx_errors.rb
CHANGED
@@ -1,33 +1,60 @@
|
|
1
1
|
require_relative "./error_base"
|
2
2
|
|
3
3
|
module HTTPError
|
4
|
+
|
5
|
+
# 500 Internal Server Error
|
4
6
|
class InternalServerError < ErrorBase
|
5
|
-
CODE =
|
6
|
-
MESSAGE = "Internal Server Error"
|
7
|
+
CODE = Rack::Utils.status_code(:internal_server_error)
|
7
8
|
end
|
8
9
|
|
10
|
+
# 501 Not Implemented
|
9
11
|
class NotImplemented < ErrorBase
|
10
|
-
CODE =
|
11
|
-
MESSAGE = "Not Impemented"
|
12
|
+
CODE = Rack::Utils.status_code(:not_implemented)
|
12
13
|
end
|
13
14
|
|
15
|
+
# 502 Bad Gateway
|
14
16
|
class BadGateway < ErrorBase
|
15
|
-
CODE =
|
16
|
-
MESSAGE = "Bad Gateway"
|
17
|
+
CODE = Rack::Utils.status_code(:bad_gateway)
|
17
18
|
end
|
18
19
|
|
20
|
+
# 503 Service Unavailable
|
19
21
|
class ServiceUnavailable < ErrorBase
|
20
|
-
CODE =
|
21
|
-
MESSAGE = "Service Unavailable"
|
22
|
+
CODE = Rack::Utils.status_code(:service_unavailable)
|
22
23
|
end
|
23
24
|
|
25
|
+
# 504 Gateway Timeout
|
24
26
|
class GatewayTimeout < ErrorBase
|
25
|
-
CODE =
|
26
|
-
MESSAGE = "Gateway Timeout"
|
27
|
+
CODE = Rack::Utils.status_code(:gateway_timeout)
|
27
28
|
end
|
28
29
|
|
30
|
+
# 505 HTTP Version Not Supported
|
29
31
|
class HTTPVersionNotSupported < ErrorBase
|
30
|
-
CODE =
|
31
|
-
|
32
|
+
CODE = Rack::Utils.status_code(:http_version_not_supported)
|
33
|
+
end
|
34
|
+
|
35
|
+
# 506 Variant Also Negotiates
|
36
|
+
class VariantAlsoNegotiates < ErrorBase
|
37
|
+
CODE = Rack::Utils.status_code(:variant_also_negotiates)
|
38
|
+
end
|
39
|
+
|
40
|
+
# 507 Insufficient Storage
|
41
|
+
class InsufficientStorage < ErrorBase
|
42
|
+
CODE = Rack::Utils.status_code(:insufficient_storage)
|
43
|
+
end
|
44
|
+
|
45
|
+
# 508 Loop Detected
|
46
|
+
class LoopDetected < ErrorBase
|
47
|
+
CODE = Rack::Utils.status_code(:loop_detected)
|
48
|
+
end
|
49
|
+
|
50
|
+
# 510 Not Extended
|
51
|
+
class NotExtended < ErrorBase
|
52
|
+
CODE = Rack::Utils.status_code(:not_extended)
|
32
53
|
end
|
54
|
+
|
55
|
+
# 511 Network Authentication Required
|
56
|
+
class NetworkAuthenticationRequired < ErrorBase
|
57
|
+
CODE = Rack::Utils.status_code(:network_authentication_required)
|
58
|
+
end
|
59
|
+
|
33
60
|
end
|
data/lib/error_base.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
class ErrorBase < StandardError
|
2
2
|
def initialize( *message )
|
3
|
-
|
4
|
-
@_msg = !_v.empty? ? _v : self.class::MESSAGE
|
3
|
+
_message = message.join(' ')
|
5
4
|
@_code = self.class::CODE
|
5
|
+
@_msg = _message.empty? ?
|
6
|
+
Rack::Utils::HTTP_STATUS_CODES[@_code] : _message
|
6
7
|
end
|
7
8
|
|
8
9
|
def code
|
9
10
|
@_code
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
+
def msg
|
13
14
|
@_msg
|
14
15
|
end
|
15
16
|
end
|
data/lib/sinatra/errorcodes.rb
CHANGED
@@ -9,12 +9,14 @@ module Sinatra
|
|
9
9
|
include HTTPError
|
10
10
|
|
11
11
|
module Errorcodes
|
12
|
-
def
|
13
|
-
|
14
|
-
|
12
|
+
def handle_errorstatus
|
13
|
+
error do |e|
|
14
|
+
if e.is_a? ErrorBase
|
15
|
+
halt e.code, e.msg
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
|
21
|
+
register Errorcodes
|
20
22
|
end
|
@@ -7,12 +7,10 @@ describe Sinatra::Errorcodes do
|
|
7
7
|
|
8
8
|
it 'provides status 500' do
|
9
9
|
expect(HTTPError::InternalServerError::CODE).to be 500
|
10
|
-
expect(HTTPError::InternalServerError::MESSAGE).to eq 'Internal Server Error'
|
11
10
|
end
|
12
11
|
|
13
12
|
it 'provides status 400' do
|
14
13
|
expect(HTTPError::BadRequest::CODE).to be 400
|
15
|
-
expect(HTTPError::BadRequest::MESSAGE).to eq 'Bad Request'
|
16
14
|
end
|
17
15
|
|
18
16
|
it 'returns status 500 ' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-errorcodes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- IzumiSy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|