From dfbe69afbc3a1f510a240ed3a9deef587638f03b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bjo=CC=88rn=20Ko=CC=88ster?=
 <bjoern.koester@open-xchange.com>
Date: Wed, 25 Oct 2023 12:52:25 +0200
Subject: [PATCH] Fix: OXUI-1330: Log aggregate errors seperately

---
 src/files.js            |  2 +-
 src/plugins/logging.js  | 46 -----------------------------------------
 src/routes/autohooks.js |  9 ++++----
 3 files changed, 6 insertions(+), 51 deletions(-)
 delete mode 100644 src/plugins/logging.js

diff --git a/src/files.js b/src/files.js
index 615728e..6ce97ed 100644
--- a/src/files.js
+++ b/src/files.js
@@ -88,7 +88,7 @@ export async function fetchFileWithHeaders ({ path, version }) {
       return fetchFileWithHeadersFromBaseUrl({ path, baseUrl, version })
     } catch (err) {
       logger.debug(`[Files] File ${path} had a baseUrl but could not be found on that server: ${err}`)
-      if (err instanceof VersionMismatchError) throw err
+      if (isVersionMismatchError(err)) throw err
     }
   }
 
diff --git a/src/plugins/logging.js b/src/plugins/logging.js
deleted file mode 100644
index 8d17eda..0000000
--- a/src/plugins/logging.js
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- *
- * @copyright Copyright (c) OX Software GmbH, Germany <info@open-xchange.com>
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with OX App Suite. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
- *
- * Any use of the work other than as authorized under this license or copyright law is prohibited.
- *
- */
-
-import fp from 'fastify-plugin'
-
-// This plugin is used to log requests and responses.
-export default fp(async (fastify) => {
-  // Logs the request body with the 'trace' level
-  fastify.addHook('preHandler', function (req, reply, done) {
-    if (req.body) req.log.trace({ body: req.body }, 'parsed body')
-    done()
-  })
-
-  fastify.addHook('onError', (req, reply, { message, stack, statusCode }, done) => {
-    const error = { statusCode, stack }
-    reply.log.error(error, message || 'request errored')
-    done()
-  })
-
-  // Logs the request with the 'debug' level and also logs headers with the 'trace' level
-  fastify.addHook('onResponse', (req, reply, done) => {
-    const loggingOptions = { url: req.raw.url, res: reply, responseTime: reply.getResponseTime() }
-    if (process.env.LOG_LEVEL === 'trace') loggingOptions.headers = req.headers
-    reply.log.debug(loggingOptions, 'request completed')
-    done()
-  })
-})
diff --git a/src/routes/autohooks.js b/src/routes/autohooks.js
index 6c0770c..5467578 100644
--- a/src/routes/autohooks.js
+++ b/src/routes/autohooks.js
@@ -30,13 +30,14 @@ export default async function (app, opts) {
     reply.header('version', version)
     reply.header('latest-version', latestVersion)
     reply.version = version
+    if (req.body) req.log.trace({ body: req.body }, 'parsed body')
   })
 
   // Add a hook to log errors
-  app.addHook('onError', (req, reply, { message, stack, statusCode }, done) => {
-    const error = { statusCode, stack }
-    /* c8 ignore next */
-    reply.log.error(error, message || 'request errored')
+  app.addHook('onError', (req, reply, fastifyError, done) => {
+    // @ts-ignore - AggregateError is not yet part of the types
+    const aggregatedErrors = fastifyError.errors instanceof Array ? fastifyError.errors : [fastifyError]
+    aggregatedErrors.forEach(error => error.err ? reply.log.error(error, error.err.message) : reply.log.error(error, error.message || 'request errored'))
     done()
   })
 
-- 
GitLab