Class: Appsignal::Rack::JSExceptionCatcher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/rack/js_exception_catcher.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

JavaScript error catching middleware.

Listens to the endpoint specified in the frontend_error_catching_path configuration option.

This is automatically included middleware in Rails apps if the frontend_error_catching_path configuration option is active.

If AppSignal is not active in the current environment, but does have JavaScript error catching turned on, we assume that a JavaScript script still sends errors to this endpoint. When AppSignal is not active in this scenario this middleware still listens to the endpoint, but won't record the error. It will return HTTP status code 202.

Examples:

with a Sinatra app

Sinatra::Application.use(Appsignal::Rack::JSExceptionCatcher)

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(app, _options = nil) ⇒ JSExceptionCatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of JSExceptionCatcher.



26
27
28
29
30
# File 'lib/appsignal/rack/js_exception_catcher.rb', line 26

def initialize(app, _options = nil)
  Appsignal.logger.debug \
    "Initializing Appsignal::Rack::JSExceptionCatcher"
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/appsignal/rack/js_exception_catcher.rb', line 32

def call(env)
  # Ignore other paths than the error catching path.
  return @app.call(env) unless error_cathing_endpoint?(env)

  # Prevent raising a 404 not found when a non-active environment posts
  # to this endpoint.
  unless Appsignal.active?
    return [
      202,
      {},
      ["AppSignal JavaScript error catching endpoint is not active."]
    ]
  end

  begin
    body = JSON.parse(env["rack.input"].read)
  rescue JSON::ParserError
    return [400, {}, ["Request payload is not valid JSON."]]
  end

  if body["name"].is_a?(String) && !body["name"].empty?
    transaction = JSExceptionTransaction.new(body)
    transaction.complete!
    code = 200
  else
    Appsignal.logger.debug \
      "JSExceptionCatcher: Could not send exception, 'name' is empty."
    code = 422
  end

  [code, {}, []]
end