startback 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a7f631f93683bc9a9fe0705fc5e3f7df55fa782
4
- data.tar.gz: 6335d0ff1b733355c96116ce19f9094adea45694
3
+ metadata.gz: 3117a103e54b43bf6703778fd08002b65d33bfd7
4
+ data.tar.gz: 119886b87454ac00d437a106039aedbf3245859d
5
5
  SHA512:
6
- metadata.gz: b1bec4146b344fac7d8ef51b130e149fb823e6d10ab5a3050be9165c877585f8dcf38092fa2548c884a8974d97868f0f161d7871908da0117af06fc851e5aaa1
7
- data.tar.gz: f7482b4b5446cc1557841302cade907c4b597ff704b88ff8017ebf652a92809cf44d5ca24d9c59d0af63dd68b579eed30afd71b5deebc516b0ae94214f6e2cb2
6
+ metadata.gz: 9080577d0443af321abc8def35d0623837ba9a0624ab29341022e895769d3e9c702fec3b0cfd73b99fd1eb0331163b63513e0d8fe3d6aecb1dec567db74f10f7
7
+ data.tar.gz: a3d0f8ba4c1d98e084f4989f8868471313c3dccd8931cf31c5be4cec8af794eb2434eff04502e3b69ab615cb793e3ec96b2fdc0a72ceb3ef0fbd47aa23c874fc
@@ -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'
@@ -1,7 +1,7 @@
1
1
  module Startback
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 2
4
+ MINOR = 3
5
5
  TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
@@ -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: "Gybr::Errors::ServerError",
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.to_json
32
+ self.body FATAL_ERROR
29
33
 
30
34
  self.ensure(true) do |ex|
31
- Startback::LOGGER.fatal(ex)
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.2.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-09-26 00:00:00.000000000 Z
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