From 24989fbd1c016d1cfb260a1a443caf79c2edcc3f Mon Sep 17 00:00:00 2001
From: Leechael Yim <yanleech@gmail.com>
Date: Thu, 17 Feb 2022 16:57:16 +0800
Subject: [PATCH 1/4] fix: check if headersSent

---
 src/driver/express/ExpressDriver.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/driver/express/ExpressDriver.ts b/src/driver/express/ExpressDriver.ts
index 5cffdb60..a3e73e86 100644
--- a/src/driver/express/ExpressDriver.ts
+++ b/src/driver/express/ExpressDriver.ts
@@ -253,7 +253,7 @@ export class ExpressDriver extends BaseDriver {
    */
   handleSuccess(result: any, action: ActionMetadata, options: Action): void {
     // if the action returned the response object itself, short-circuits
-    if (result && result === options.response) {
+    if (result && result === options.response || options.response.headersSent) {
       options.next();
       return;
     }
@@ -354,7 +354,7 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of failed executed controller action.
    */
   handleError(error: any, action: ActionMetadata | undefined, options: Action): any {
-    if (this.isDefaultErrorHandlingEnabled) {
+    if (this.isDefaultErrorHandlingEnabled && !options.response.headersSent) {
       const response: any = options.response;
 
       // set http code

From e597f97eb921e3b22a265105219f38ddf4dd4ada Mon Sep 17 00:00:00 2001
From: Leechael Yim <yanleech@gmail.com>
Date: Sat, 26 Mar 2022 07:03:22 +0800
Subject: [PATCH 2/4] chore: warn if headers sent and the default handle still
 try send response.

---
 src/driver/express/ExpressDriver.ts | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/driver/express/ExpressDriver.ts b/src/driver/express/ExpressDriver.ts
index a3e73e86..f9606ce5 100644
--- a/src/driver/express/ExpressDriver.ts
+++ b/src/driver/express/ExpressDriver.ts
@@ -252,8 +252,13 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of successfully executed controller action.
    */
   handleSuccess(result: any, action: ActionMetadata, options: Action): void {
+    if (action.response.headersSent) {
+      console.warn('Response headers were already sent, default handleSuccess will not be executed.');
+      options.next();
+      return;
+    }
     // if the action returned the response object itself, short-circuits
-    if (result && result === options.response || options.response.headersSent) {
+    if (result && result === options.response) {
       options.next();
       return;
     }
@@ -354,7 +359,12 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of failed executed controller action.
    */
   handleError(error: any, action: ActionMetadata | undefined, options: Action): any {
-    if (this.isDefaultErrorHandlingEnabled && !options.response.headersSent) {
+    if (action.response.headersSent) {
+      console.warn('Response headers were already sent, default handleError will not be executed.');
+      options.next(error);
+      return;
+    }
+    if (this.isDefaultErrorHandlingEnabled) {
       const response: any = options.response;
 
       // set http code

From f63867f4c32258e39ce8c04129d224826c75090a Mon Sep 17 00:00:00 2001
From: Leechael Yim <yanleech@gmail.com>
Date: Sat, 26 Mar 2022 07:09:14 +0800
Subject: [PATCH 3/4] fix: check and ensure response present.

---
 src/driver/express/ExpressDriver.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/driver/express/ExpressDriver.ts b/src/driver/express/ExpressDriver.ts
index f9606ce5..446da08e 100644
--- a/src/driver/express/ExpressDriver.ts
+++ b/src/driver/express/ExpressDriver.ts
@@ -252,7 +252,7 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of successfully executed controller action.
    */
   handleSuccess(result: any, action: ActionMetadata, options: Action): void {
-    if (action.response.headersSent) {
+    if (action.response && action.response.headersSent) {
       console.warn('Response headers were already sent, default handleSuccess will not be executed.');
       options.next();
       return;
@@ -359,7 +359,7 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of failed executed controller action.
    */
   handleError(error: any, action: ActionMetadata | undefined, options: Action): any {
-    if (action.response.headersSent) {
+    if (action.response && action.response.headersSent) {
       console.warn('Response headers were already sent, default handleError will not be executed.');
       options.next(error);
       return;

From 60f5e52650b648bf4bde97bd004c906f6bc4e75f Mon Sep 17 00:00:00 2001
From: Leechael Yim <yanleech@gmail.com>
Date: Sat, 26 Mar 2022 07:16:24 +0800
Subject: [PATCH 4/4] fix: response pass through options not action

---
 src/driver/express/ExpressDriver.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/driver/express/ExpressDriver.ts b/src/driver/express/ExpressDriver.ts
index 446da08e..15dcee08 100644
--- a/src/driver/express/ExpressDriver.ts
+++ b/src/driver/express/ExpressDriver.ts
@@ -252,7 +252,7 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of successfully executed controller action.
    */
   handleSuccess(result: any, action: ActionMetadata, options: Action): void {
-    if (action.response && action.response.headersSent) {
+    if (options && options.response && options.response.headersSent) {
       console.warn('Response headers were already sent, default handleSuccess will not be executed.');
       options.next();
       return;
@@ -359,7 +359,7 @@ export class ExpressDriver extends BaseDriver {
    * Handles result of failed executed controller action.
    */
   handleError(error: any, action: ActionMetadata | undefined, options: Action): any {
-    if (action.response && action.response.headersSent) {
+    if (options && options.response && options.response.headersSent) {
       console.warn('Response headers were already sent, default handleError will not be executed.');
       options.next(error);
       return;