Skip to content

Commit

Permalink
Merge branch 'dev' into task/rc-2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrandahl committed Dec 20, 2024
2 parents 4d04030 + b18c3df commit 0d93281
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changes to the UKHPI app by version and date

## 2.0.1 - 2024-12

- (Jon) Improves error metrics reporting to ensure that logging always happens
with the appropriate severity depending on the exception status while reducing
the types of errors that can trigger a an error metric and therefore a
notification in slack
[GH-149](https://github.com/epimorphics/hmlr-linked-data/issues/149)
- (Jon) Updated the `maybe_report_to_sentry` method to call the
`instrument_internal_error` method for reporting internal errors to the
Prometheus metrics when necessary
- (Jon) Renamed the `handle_error` method to `handle_exceptions` to better
reflect the method's purpose

## 2.0.0 - 2024-11

- (Bogdan) Updated all gems by regenerating `Gemfile.lock`
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ RUN apk add --update \
tzdata \
yarn \
&& rm -rf /var/cache/apk/* \
&& gem install rubygems-update -v 3.4.22 \
&& update_rubygems \
&& gem update --system \
&& gem install bundler:$BUNDLER_VERSION \
&& bundle config --global frozen 1

Expand Down
19 changes: 16 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,23 @@ def log_response(status, error_log)

# Notify subscriber(s) of an internal error event with the payload of the
# exception once done
# @param [Exception] exp the exception that caused the error
# @param [exc] exp the exception that caused the error
# @return [ActiveSupport::Notifications::Event] provides an object-oriented
# interface to the event
def instrument_internal_error(exception)
ActiveSupport::Notifications.instrument('internal_error.application', exception: exception)
def instrument_internal_error(exc) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
err = {
message: exc&.message || exc,
status: exc&.status || Rack::Utils::SYMBOL_TO_STATUS_CODE[exc]
}
err[:type] = exc.class&.name if exc&.class
err[:cause] = exc&.cause if exc&.cause
err[:backtrace] = exc&.backtrace if exc&.backtrace && Rails.env.development?
# Log the exception to the Rails logger with the appropriate severity
Rails.logger.send(err[:status] < 500 ? :warn : :error, JSON.generate(err))
# Return unless the status code is 500 or greater to ensure subscribers are NOT notified
return unless err[:status] >= 500

# Instrument the internal error event to notify subscribers of the error
ActiveSupport::Notifications.instrument('internal_error.application', exception: err)
end
end
5 changes: 4 additions & 1 deletion app/controllers/exceptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class ExceptionsController < ApplicationController
layout 'application'

def handle_error
def handle_exceptions
env = request.env
exception = env['action_dispatch.exception']
status_code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code
Expand All @@ -26,6 +26,9 @@ def maybe_report_to_sentry(exception, status_code)
return nil if Rails.env.development? # Why are we reporting to Sentry in dev?
return nil unless status_code >= 500

# add the exception to the prometheus metrics
instrument_internal_error(exception)

sevent = Sentry.capture_exception(exception)
sevent&.event_id
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Version
MAJOR = 2
MINOR = 0
PATCH = 0
PATCH = 1
SUFFIX = nil
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && ".#{SUFFIX}"}"
end
2 changes: 1 addition & 1 deletion config/initializers/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# Custom error handling via a Rack middleware action
Rails.application.config.exceptions_app =
lambda do |env|
ExceptionsController.action(:handle_error).call(env)
ExceptionsController.action(:handle_exceptions).call(env)
end

0 comments on commit 0d93281

Please sign in to comment.