From 79d0a0f2470905f98b71fb5c8ca007244004f99a Mon Sep 17 00:00:00 2001 From: Tom <19203795+that-one-tom@users.noreply.github.com> Date: Tue, 16 May 2023 11:19:41 +0200 Subject: [PATCH] feat(core): Return OAuth2 error body if available (#5794) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ --- .../src/credentials/oauth2Credential.api.ts | 18 +++++++++++------- .../templates/oauth-error-callback.handlebars | 14 +++++++++----- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/credentials/oauth2Credential.api.ts b/packages/cli/src/credentials/oauth2Credential.api.ts index a8fd84cef7..e286ae116a 100644 --- a/packages/cli/src/credentials/oauth2Credential.api.ts +++ b/packages/cli/src/credentials/oauth2Credential.api.ts @@ -12,7 +12,7 @@ import type { INodeCredentialsDetails, ICredentialsEncrypted, } from 'n8n-workflow'; -import { LoggerProxy } from 'n8n-workflow'; +import { LoggerProxy, jsonStringify } from 'n8n-workflow'; import { resolve as pathResolve } from 'path'; import * as Db from '@/Db'; @@ -173,8 +173,8 @@ oauth2CredentialController.get( }), ); -const renderCallbackError = (res: express.Response, errorMessage: string) => - res.render('oauth-error-callback', { error: { message: errorMessage } }); +const renderCallbackError = (res: express.Response, message: string, reason?: string) => + res.render('oauth-error-callback', { error: { message, reason } }); /** * GET /oauth2-credential/callback @@ -192,9 +192,8 @@ oauth2CredentialController.get( if (!code || !stateEncoded) { return renderCallbackError( res, - `Insufficient parameters for OAuth2 callback. Received following query parameters: ${JSON.stringify( - req.query, - )}`, + 'Insufficient parameters for OAuth2 callback.', + `Received following query parameters: ${JSON.stringify(req.query)}`, ); } @@ -326,7 +325,12 @@ oauth2CredentialController.get( return res.sendFile(pathResolve(TEMPLATES_DIR, 'oauth-callback.html')); } catch (error) { - return renderCallbackError(res, (error as Error).message); + return renderCallbackError( + res, + (error as Error).message, + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + 'body' in error ? jsonStringify(error.body) : undefined, + ); } }, ); diff --git a/packages/cli/templates/oauth-error-callback.handlebars b/packages/cli/templates/oauth-error-callback.handlebars index 38af48fd82..a911167234 100644 --- a/packages/cli/templates/oauth-error-callback.handlebars +++ b/packages/cli/templates/oauth-error-callback.handlebars @@ -3,17 +3,21 @@ n8n - OAuth Callback {{#if error}} -

Error:

-
{{error.message}}
+

Error: {{error.message}}

+
+ More details + {{#if error.reason}}
{{error.reason}}
{{/if}} +
{{/if}} - Failed to connect. The window can be closed now. + Failed to connect. The window can be closed now.