Module: PactBroker::Api::Resources::ErrorHandlingMethods

Included in:
BaseResource
Defined in:
lib/pact_broker/api/resources/error_handling_methods.rb

Instance Method Summary collapse

Instance Method Details

#decorator_options_for_errorObject

If we use the normal decorator options that have policy objects we can get into recursive loops in Pactflow, so just make a simple variant of the decorator options here



77
78
79
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 77

def decorator_options_for_error
  { user_options: { base_url: base_url } }
end

#error_response_body(detail, title, type, status) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 53

def error_response_body(detail, title, type, status)
  if problem_json_error_content_type?
    decorator_class(:custom_error_problem_json_decorator).new(detail: detail, title: title, type: type, status: status).to_json(**decorator_options_for_error)
  else
    decorator_class(:error_decorator).new(detail).to_json
  end
end

#error_response_content_typeObject



45
46
47
48
49
50
51
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 45

def error_response_content_type
  if problem_json_error_content_type?
    "application/problem+json;charset=utf-8"
  else
    "application/hal+json;charset=utf-8"
  end
end

#handle_exception(error) ⇒ Object

Parameters:

  • error (StandardError)


11
12
13
14
15
16
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 11

def handle_exception(error)
  error_reference = log_and_report_error(error)
  headers, body = application_context.error_response_generator.call(error, error_reference, request.env)
  headers.each { | key, value | response.headers[key] = value }
  response.body = body
end

#log_and_report_error(error) ⇒ Object

Parameters:

  • error (StandardError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 19

def log_and_report_error(error)
  # generate reference
  error_reference = PactBroker::Errors.generate_error_reference
  # log error
  application_context.error_logger.call(error, error_reference, request.env)
  # report error
  application_context.error_reporter.call(error, error_reference, request.env)
  # generate response
  error_reference
end

#problem_json_error_content_type?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 71

def problem_json_error_content_type?
  request.headers["Accept"]&.include?("application/problem+json")
end

#set_json_error_message(detail, title: "Server error", type: "server-error", status: 500) ⇒ Object

Parameters:

  • detail (String)
  • title (String) (defaults to: "Server error")
  • type (String) (defaults to: "server-error")
  • status (Integer) (defaults to: 500)


34
35
36
37
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 34

def set_json_error_message(detail, title: "Server error", type: "server-error", status: 500)
  response.headers["Content-Type"] = error_response_content_type
  response.body = error_response_body(detail, title, type, status)
end

#set_json_validation_error_messages(errors) ⇒ Object

Parameters:

  • errors (Hash, Dry::Validation::MessageSet)


40
41
42
43
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 40

def set_json_validation_error_messages(errors)
  response.headers["Content-Type"] = error_response_content_type
  response.body = validation_errors_response_body(errors)
end

#validation_errors_decorator_class(errors) ⇒ Object

Parameters:

  • errors (Hash, Dry::Validation::MessageSet)


67
68
69
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 67

def validation_errors_decorator_class(errors)
  application_context.decorator_configuration.validation_error_decorator_class_for(errors.class, request.headers["Accept"])
end

#validation_errors_response_body(errors) ⇒ Object

Parameters:

  • errors (Hash, Dry::Validation::MessageSet)


62
63
64
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 62

def validation_errors_response_body(errors)
  validation_errors_decorator_class(errors).new(errors).to_json(**decorator_options_for_error)
end