Browse Source

fix(core): Prevent errors being logged by Nest's ExternalExceptionFilter

Relates to #1335. A flaw in our own exception filter meant that all errors, regardless of the
intended logLevel, were being caught by Nest's ExternalExceptionFilter and being logged as errors.
This was the reason for e.g. the `new ForbiddenError(LogLevel.Verbose);` thrown in the AuthGuard
being logged as an Error in addition to Verbose.
Michael Bromley 4 years ago
parent
commit
cf4f2462b1

+ 6 - 0
packages/core/src/api/middleware/exception-logger.filter.ts

@@ -39,6 +39,12 @@ export class ExceptionLoggerFilter implements ExceptionFilter {
                     Logger.verbose(message);
                     break;
             }
+            if (exception.stack) {
+                Logger.debug(exception.stack);
+            }
+            if (isGraphQL) {
+                return exception;
+            }
         } else if (exception instanceof HttpException) {
             // Handle other Nestjs errors
             statusCode = exception.getStatus();

+ 35 - 6
packages/core/src/config/logger/vendure-logger.ts

@@ -9,12 +9,31 @@ import { LoggerService } from '@nestjs/common';
 export enum LogLevel {
     /**
      * @description
-     * Log Errors only.
+     * Log Errors only. These are usually indicative of some potentially
+     * serious issue, so should be acted upon.
      */
     Error = 0,
+    /**
+     * @description
+     * Warnings indicate that some situation may require investigation
+     * and handling. But not as serious as an Error.
+     */
     Warn = 1,
+    /**
+     * @description
+     * Logs general information such as startup messages.
+     */
     Info = 2,
+    /**
+     * @description
+     * Logs additional information
+     */
     Verbose = 3,
+    /**
+     * @description
+     * Logs detailed info useful in debug scenarios, including stack traces for
+     * all errors. In production this would probably generate too much noise.
+     */
     Debug = 4,
 }
 
@@ -34,11 +53,21 @@ export interface VendureLogger {
 }
 
 const noopLogger: VendureLogger = {
-    error() { /* */ },
-    warn() { /* */ },
-    info() { /* */ },
-    verbose() { /* */ },
-    debug() { /* */ },
+    error() {
+        /* */
+    },
+    warn() {
+        /* */
+    },
+    info() {
+        /* */
+    },
+    verbose() {
+        /* */
+    },
+    debug() {
+        /* */
+    },
 };
 
 /**