From 0f7b88970f59ed6c4ed6de32dd6c8dcf0b3ff5c0 Mon Sep 17 00:00:00 2001 From: Mert E Date: Thu, 28 Mar 2024 14:48:19 +0300 Subject: [PATCH] fix: shared form pw error handling (#7991) * fix: shared form password error * fix: remove unnecessary export & duplicate file * fix: filter non-generic errors from logs for NcBaseErrorv2 --- .../composables/useSharedFormViewStore.ts | 11 +++- packages/nc-gui/utils/errorUtils.ts | 37 +++++++++++ packages/nocodb-sdk/src/lib/globals.ts | 1 + .../global-exception.filter.ts | 9 ++- packages/nocodb/src/helpers/catchError.ts | 65 ++----------------- packages/nocodb/src/middlewares/catchError.ts | 4 -- .../extract-ids/extract-ids.middleware.ts | 2 +- 7 files changed, 59 insertions(+), 70 deletions(-) delete mode 100644 packages/nocodb/src/middlewares/catchError.ts diff --git a/packages/nc-gui/composables/useSharedFormViewStore.ts b/packages/nc-gui/composables/useSharedFormViewStore.ts index 9eb7a004c2..e10d40e92e 100644 --- a/packages/nc-gui/composables/useSharedFormViewStore.ts +++ b/packages/nc-gui/composables/useSharedFormViewStore.ts @@ -16,11 +16,13 @@ import { RelationTypes, UITypes, isLinksOrLTAR, isSystemColumn, isVirtualCol } f import { isString } from '@vue/shared' import { filterNullOrUndefinedObjectProperties } from '~/helpers/parsers/parserHelpers' import { + NcErrorType, PreFilledMode, SharedViewPasswordInj, computed, createEventHook, extractSdkResponseErrorMsg, + extractSdkResponseErrorMsgv2, isNumericFieldType, isValidURL, message, @@ -176,13 +178,16 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share handlePreFillForm() } catch (e: any) { + const error = await extractSdkResponseErrorMsgv2(e) + if (e.response && e.response.status === 404) { notFound.value = true - // TODO - handle invalidSharedViewPassword - } else if (await extractSdkResponseErrorMsg(e)) { + } else if (error.error === NcErrorType.INVALID_SHARED_VIEW_PASSWORD) { passwordDlg.value = true - if (password.value && password.value !== '') passwordError.value = 'Something went wrong. Please check your credentials.' + if (password.value && password.value !== '') { + passwordError.value = error.message + } } } } diff --git a/packages/nc-gui/utils/errorUtils.ts b/packages/nc-gui/utils/errorUtils.ts index 373881783c..e01fb0a86a 100644 --- a/packages/nc-gui/utils/errorUtils.ts +++ b/packages/nc-gui/utils/errorUtils.ts @@ -1,3 +1,5 @@ +import { NcErrorType } from 'nocodb-sdk' + export async function extractSdkResponseErrorMsg(e: Error & { response: any }) { if (!e || !e.response) return e.message let msg @@ -21,3 +23,38 @@ export async function extractSdkResponseErrorMsg(e: Error & { response: any }) { return msg || 'Some error occurred' } + +export async function extractSdkResponseErrorMsgv2(e: Error & { response: any }): Promise<{ + error: NcErrorType + message: string + details?: any +}> { + const unknownError = { + error: NcErrorType.UNKNOWN_ERROR, + message: 'Something went wrong', + } + + if (!e || !e.response) { + return unknownError + } + + if (e.response.data instanceof Blob) { + try { + const parsedError = JSON.parse(await e.response.data.text()) + if (parsedError.error && parsedError.error in NcErrorType) { + return parsedError + } + return unknownError + } catch { + return unknownError + } + } else { + if (e.response.data.error && e.response.data.error in NcErrorType) { + return e.response.data + } + + return unknownError + } +} + +export { NcErrorType } diff --git a/packages/nocodb-sdk/src/lib/globals.ts b/packages/nocodb-sdk/src/lib/globals.ts index fd3ad311f3..37db920322 100644 --- a/packages/nocodb-sdk/src/lib/globals.ts +++ b/packages/nocodb-sdk/src/lib/globals.ts @@ -143,6 +143,7 @@ export enum NcErrorType { NOT_IMPLEMENTED = 'NOT_IMPLEMENTED', INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR', DATABASE_ERROR = 'DATABASE_ERROR', + UNKNOWN_ERROR = 'UNKNOWN_ERROR', } type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles; diff --git a/packages/nocodb/src/filters/global-exception/global-exception.filter.ts b/packages/nocodb/src/filters/global-exception/global-exception.filter.ts index 5f036eb1e7..a045f06c7a 100644 --- a/packages/nocodb/src/filters/global-exception/global-exception.filter.ts +++ b/packages/nocodb/src/filters/global-exception/global-exception.filter.ts @@ -1,6 +1,7 @@ import { Catch, Logger, NotFoundException, Optional } from '@nestjs/common'; import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry'; import { ThrottlerException } from '@nestjs/throttler'; +import { NcErrorType } from 'nocodb-sdk'; import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common'; import type { Request, Response } from 'express'; import { @@ -38,7 +39,13 @@ export class GlobalExceptionFilter implements ExceptionFilter { exception instanceof NotFound || exception instanceof UnprocessableEntity || exception instanceof NotFoundException || - exception instanceof ThrottlerException + exception instanceof ThrottlerException || + (exception instanceof NcBaseErrorv2 && + ![ + NcErrorType.INTERNAL_SERVER_ERROR, + NcErrorType.DATABASE_ERROR, + NcErrorType.UNKNOWN_ERROR, + ].includes(exception.error)) ) ) this.logError(exception, request); diff --git a/packages/nocodb/src/helpers/catchError.ts b/packages/nocodb/src/helpers/catchError.ts index 29bad0e38e..a682c416de 100644 --- a/packages/nocodb/src/helpers/catchError.ts +++ b/packages/nocodb/src/helpers/catchError.ts @@ -1,5 +1,4 @@ import { NcErrorType } from 'nocodb-sdk'; -import type { NextFunction, Request, Response } from 'express'; import type { ErrorObject } from 'ajv'; import { defaultLimitConfig } from '~/helpers/extractLimitAndOffset'; @@ -392,66 +391,6 @@ export function extractDBError(error): { } } -export default function ( - requestHandler: (req: Request, res: Response, next?: NextFunction) => any, -) { - return async function (req: Request, res: Response, next?: NextFunction) { - try { - return await requestHandler(req, res, next); - } catch (e) { - // skip unnecessary error logging - if ( - process.env.NC_ENABLE_ALL_API_ERROR_LOGGING === 'true' || - !( - e instanceof BadRequest || - e instanceof AjvError || - e instanceof Unauthorized || - e instanceof Forbidden || - e instanceof NotFound || - e instanceof UnprocessableEntity - ) - ) - console.log(requestHandler.name ? `${requestHandler.name} ::` : '', e); - - const dbError = extractDBError(e); - - if (dbError) { - const error = new NcBaseErrorv2(NcErrorType.DATABASE_ERROR, { - params: dbError.message, - details: dbError, - }); - return res.status(error.code).json({ - error: error.error, - message: error.message, - details: error.details, - }); - } - - if (e instanceof BadRequest) { - return res.status(400).json({ msg: e.message }); - } else if (e instanceof Unauthorized) { - return res.status(401).json({ msg: e.message }); - } else if (e instanceof Forbidden) { - return res.status(403).json({ msg: e.message }); - } else if (e instanceof NotFound) { - return res.status(404).json({ msg: e.message }); - } else if (e instanceof AjvError) { - return res.status(400).json({ msg: e.message, errors: e.errors }); - } else if (e instanceof UnprocessableEntity) { - return res.status(422).json({ msg: e.message }); - } else if (e instanceof NotAllowed) { - return res.status(405).json({ msg: e.message }); - } else if (e instanceof NcBaseErrorv2) { - return res - .status(e.code) - .json({ error: e.error, message: e.message, details: e.details }); - } - // if some other error occurs then send 500 and a generic message - res.status(500).json({ msg: 'Internal server error' }); - } - }; -} - export class NcBaseError extends Error { constructor(message: string) { super(message); @@ -485,6 +424,10 @@ const errorHelpers: { code: number; }; } = { + [NcErrorType.UNKNOWN_ERROR]: { + message: 'Something went wrong', + code: 500, + }, [NcErrorType.INTERNAL_SERVER_ERROR]: { message: (message: string) => message || `Internal server error`, code: 500, diff --git a/packages/nocodb/src/middlewares/catchError.ts b/packages/nocodb/src/middlewares/catchError.ts deleted file mode 100644 index 1f207e5ea6..0000000000 --- a/packages/nocodb/src/middlewares/catchError.ts +++ /dev/null @@ -1,4 +0,0 @@ -// todo: remove this file -export * from '~/helpers/catchError'; -import catchError from '~/helpers/catchError'; -export default catchError; diff --git a/packages/nocodb/src/middlewares/extract-ids/extract-ids.middleware.ts b/packages/nocodb/src/middlewares/extract-ids/extract-ids.middleware.ts index bbcc8a4368..2176b8c661 100644 --- a/packages/nocodb/src/middlewares/extract-ids/extract-ids.middleware.ts +++ b/packages/nocodb/src/middlewares/extract-ids/extract-ids.middleware.ts @@ -25,7 +25,7 @@ import { View, } from '~/models'; import rolePermissions from '~/utils/acl'; -import { NcError } from '~/middlewares/catchError'; +import { NcError } from '~/helpers/catchError'; export const rolesLabel = { [OrgUserRoles.SUPER_ADMIN]: 'Super Admin',