startback 0.2.0 → 0.3.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 +4 -4
- data/lib/startback/context.rb +7 -0
- data/lib/startback/version.rb +1 -1
- data/lib/startback/web/catch_all.rb +12 -3
- data/spec/unit/web/test_catch_all.rb +76 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3117a103e54b43bf6703778fd08002b65d33bfd7
|
4
|
+
data.tar.gz: 119886b87454ac00d437a106039aedbf3245859d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9080577d0443af321abc8def35d0623837ba9a0624ab29341022e895769d3e9c702fec3b0cfd73b99fd1eb0331163b63513e0d8fe3d6aecb1dec567db74f10f7
|
7
|
+
data.tar.gz: a3d0f8ba4c1d98e084f4989f8868471313c3dccd8931cf31c5be4cec8af794eb2434eff04502e3b69ab615cb793e3ec96b2fdc0a72ceb3ef0fbd47aa23c874fc
|
data/lib/startback/context.rb
CHANGED
@@ -11,6 +11,13 @@ module Startback
|
|
11
11
|
class Context
|
12
12
|
attr_accessor :original_rack_env
|
13
13
|
|
14
|
+
# An error handler can be provided on the Context class. The latter
|
15
|
+
# MUST expose an API similar to ruby's Logger class. It can be a logger
|
16
|
+
# instance, simply.
|
17
|
+
#
|
18
|
+
# Fatal errors catched by Web::CatchAll are sent on `error_handler#fatal`
|
19
|
+
attr_accessor :error_handler
|
20
|
+
|
14
21
|
end # class Context
|
15
22
|
end # module Startback
|
16
23
|
require_relative 'context/middleware'
|
data/lib/startback/version.rb
CHANGED
@@ -8,6 +8,9 @@ module Startback
|
|
8
8
|
# This class aims at being used as top level of a Rack chain. It is not
|
9
9
|
# aimed at being subclassed.
|
10
10
|
#
|
11
|
+
# Fatal error cached are also sent as a `fatal` messange, on the error
|
12
|
+
# handler provided on Context#error_handler.fatal, if any.
|
13
|
+
#
|
11
14
|
# Examples:
|
12
15
|
#
|
13
16
|
# Rack::Builder.new do
|
@@ -18,17 +21,23 @@ module Startback
|
|
18
21
|
include Errors
|
19
22
|
|
20
23
|
FATAL_ERROR = {
|
21
|
-
code: "
|
24
|
+
code: "Startback::Errors::InternalServerError",
|
22
25
|
description: "An error occured, sorry"
|
23
26
|
}.to_json
|
24
27
|
|
25
28
|
self.catch_all
|
29
|
+
self.on(Exception)
|
26
30
|
self.status 500
|
27
31
|
self.content_type 'application/json'
|
28
|
-
self.body FATAL_ERROR
|
32
|
+
self.body FATAL_ERROR
|
29
33
|
|
30
34
|
self.ensure(true) do |ex|
|
31
|
-
|
35
|
+
context = env[Context::Middleware::RACK_ENV_KEY]
|
36
|
+
if context && context.respond_to?(:error_handler) && context.error_handler
|
37
|
+
context.error_handler.fatal(ex)
|
38
|
+
else
|
39
|
+
Startback::LOGGER.fatal(ex)
|
40
|
+
end
|
32
41
|
end
|
33
42
|
|
34
43
|
end # class CatchAll
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
module Startback
|
5
|
+
module Web
|
6
|
+
describe CatchAll do
|
7
|
+
include Rack::Test::Methods
|
8
|
+
|
9
|
+
context 'when used without context' do
|
10
|
+
def app
|
11
|
+
Rack::Builder.new do
|
12
|
+
use CatchAll
|
13
|
+
run ->(env){ raise "Hello error" }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns a 500 with json explanation' do
|
18
|
+
get '/'
|
19
|
+
expect(last_response.status).to eql(500)
|
20
|
+
expect(last_response.content_type).to eql("application/json")
|
21
|
+
result = JSON.parse(last_response.body)
|
22
|
+
expect(result).to eql({
|
23
|
+
"code" => "Startback::Errors::InternalServerError",
|
24
|
+
"description" => "An error occured, sorry"
|
25
|
+
})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when used with a context providing an error handler' do
|
30
|
+
|
31
|
+
class AnError < StandardError
|
32
|
+
end
|
33
|
+
|
34
|
+
class ErrorHandler
|
35
|
+
include Singleton
|
36
|
+
|
37
|
+
attr_reader :ex
|
38
|
+
|
39
|
+
def fatal(ex)
|
40
|
+
@ex = ex
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class MyContextWithErrorHandler < Startback::Context
|
46
|
+
|
47
|
+
def error_handler
|
48
|
+
ErrorHandler.instance
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def app
|
54
|
+
Rack::Builder.new do
|
55
|
+
use Context::Middleware, context_class: MyContextWithErrorHandler
|
56
|
+
use CatchAll
|
57
|
+
run ->(env){ raise AnError, "Hello error" }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns a 500 with json explanation' do
|
62
|
+
get '/'
|
63
|
+
expect(last_response.status).to eql(500)
|
64
|
+
expect(last_response.content_type).to eql("application/json")
|
65
|
+
result = JSON.parse(last_response.body)
|
66
|
+
expect(result).to eql({
|
67
|
+
"code" => "Startback::Errors::InternalServerError",
|
68
|
+
"description" => "An error occured, sorry"
|
69
|
+
})
|
70
|
+
expect(ErrorHandler.instance.ex).to be_a(AnError)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end # CatchAll
|
75
|
+
end # module Web
|
76
|
+
end # module Startback
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: startback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -139,6 +139,7 @@ files:
|
|
139
139
|
- spec/unit/context/test_middleware.rb
|
140
140
|
- spec/unit/test_operation.rb
|
141
141
|
- spec/unit/test_support.rb
|
142
|
+
- spec/unit/web/test_catch_all.rb
|
142
143
|
- spec/unit/web/test_healthcheck.rb
|
143
144
|
- tasks/gem.rake
|
144
145
|
- tasks/test.rake
|