From 6b98033d9120c7dfc5276f623e27e7af22dd7b88 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 17 May 2018 23:48:59 -0700 Subject: [PATCH] Fix api_json.log not always reporting the right HTTP status code As described in https://github.com/aserafin/grape_logging/issues/45, if a Grape error is caught by the handlers and a different return code is returned, then the api_json.log would have a 500 error code instead of the right value. Inserting the GrapeLogging middleware after the Grape middleware fixes this problem. Seen in https://gitlab.com/gitlab-com/infrastructure/issues/4249 --- .../sh-fix-grape-logging-status-code.yml | 5 +++++ lib/api/api.rb | 17 +++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 changelogs/unreleased/sh-fix-grape-logging-status-code.yml diff --git a/changelogs/unreleased/sh-fix-grape-logging-status-code.yml b/changelogs/unreleased/sh-fix-grape-logging-status-code.yml new file mode 100644 index 00000000000..aabf9a84bfb --- /dev/null +++ b/changelogs/unreleased/sh-fix-grape-logging-status-code.yml @@ -0,0 +1,5 @@ +--- +title: Fix api_json.log not always reporting the right HTTP status code +merge_request: +author: +type: fixed diff --git a/lib/api/api.rb b/lib/api/api.rb index 5139e869c71..2fbeaaffcfe 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -8,14 +8,15 @@ module API PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze COMMIT_ENDPOINT_REQUIREMENTS = PROJECT_ENDPOINT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze - use GrapeLogging::Middleware::RequestLogger, - logger: Logger.new(LOG_FILENAME), - formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new, - include: [ - GrapeLogging::Loggers::FilterParameters.new, - GrapeLogging::Loggers::ClientEnv.new, - Gitlab::GrapeLogging::Loggers::UserLogger.new - ] + insert_before Grape::Middleware::Error, + GrapeLogging::Middleware::RequestLogger, + logger: Logger.new(LOG_FILENAME), + formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new, + include: [ + GrapeLogging::Loggers::FilterParameters.new, + GrapeLogging::Loggers::ClientEnv.new, + Gitlab::GrapeLogging::Loggers::UserLogger.new + ] allow_access_with_scope :api prefix :api