Browse Source

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
pull/8002/head
Mert E 8 months ago committed by GitHub
parent
commit
0f7b88970f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      packages/nc-gui/composables/useSharedFormViewStore.ts
  2. 37
      packages/nc-gui/utils/errorUtils.ts
  3. 1
      packages/nocodb-sdk/src/lib/globals.ts
  4. 9
      packages/nocodb/src/filters/global-exception/global-exception.filter.ts
  5. 65
      packages/nocodb/src/helpers/catchError.ts
  6. 4
      packages/nocodb/src/middlewares/catchError.ts
  7. 2
      packages/nocodb/src/middlewares/extract-ids/extract-ids.middleware.ts

11
packages/nc-gui/composables/useSharedFormViewStore.ts

@ -16,11 +16,13 @@ import { RelationTypes, UITypes, isLinksOrLTAR, isSystemColumn, isVirtualCol } f
import { isString } from '@vue/shared' import { isString } from '@vue/shared'
import { filterNullOrUndefinedObjectProperties } from '~/helpers/parsers/parserHelpers' import { filterNullOrUndefinedObjectProperties } from '~/helpers/parsers/parserHelpers'
import { import {
NcErrorType,
PreFilledMode, PreFilledMode,
SharedViewPasswordInj, SharedViewPasswordInj,
computed, computed,
createEventHook, createEventHook,
extractSdkResponseErrorMsg, extractSdkResponseErrorMsg,
extractSdkResponseErrorMsgv2,
isNumericFieldType, isNumericFieldType,
isValidURL, isValidURL,
message, message,
@ -176,13 +178,16 @@ const [useProvideSharedFormStore, useSharedFormStore] = useInjectionState((share
handlePreFillForm() handlePreFillForm()
} catch (e: any) { } catch (e: any) {
const error = await extractSdkResponseErrorMsgv2(e)
if (e.response && e.response.status === 404) { if (e.response && e.response.status === 404) {
notFound.value = true notFound.value = true
// TODO - handle invalidSharedViewPassword } else if (error.error === NcErrorType.INVALID_SHARED_VIEW_PASSWORD) {
} else if (await extractSdkResponseErrorMsg(e)) {
passwordDlg.value = true 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
}
} }
} }
} }

37
packages/nc-gui/utils/errorUtils.ts

@ -1,3 +1,5 @@
import { NcErrorType } from 'nocodb-sdk'
export async function extractSdkResponseErrorMsg(e: Error & { response: any }) { export async function extractSdkResponseErrorMsg(e: Error & { response: any }) {
if (!e || !e.response) return e.message if (!e || !e.response) return e.message
let msg let msg
@ -21,3 +23,38 @@ export async function extractSdkResponseErrorMsg(e: Error & { response: any }) {
return msg || 'Some error occurred' 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 }

1
packages/nocodb-sdk/src/lib/globals.ts

@ -143,6 +143,7 @@ export enum NcErrorType {
NOT_IMPLEMENTED = 'NOT_IMPLEMENTED', NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR', INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
DATABASE_ERROR = 'DATABASE_ERROR', DATABASE_ERROR = 'DATABASE_ERROR',
UNKNOWN_ERROR = 'UNKNOWN_ERROR',
} }
type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles; type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles;

9
packages/nocodb/src/filters/global-exception/global-exception.filter.ts

@ -1,6 +1,7 @@
import { Catch, Logger, NotFoundException, Optional } from '@nestjs/common'; import { Catch, Logger, NotFoundException, Optional } from '@nestjs/common';
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry'; import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry';
import { ThrottlerException } from '@nestjs/throttler'; import { ThrottlerException } from '@nestjs/throttler';
import { NcErrorType } from 'nocodb-sdk';
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common'; import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
import type { Request, Response } from 'express'; import type { Request, Response } from 'express';
import { import {
@ -38,7 +39,13 @@ export class GlobalExceptionFilter implements ExceptionFilter {
exception instanceof NotFound || exception instanceof NotFound ||
exception instanceof UnprocessableEntity || exception instanceof UnprocessableEntity ||
exception instanceof NotFoundException || 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); this.logError(exception, request);

65
packages/nocodb/src/helpers/catchError.ts

@ -1,5 +1,4 @@
import { NcErrorType } from 'nocodb-sdk'; import { NcErrorType } from 'nocodb-sdk';
import type { NextFunction, Request, Response } from 'express';
import type { ErrorObject } from 'ajv'; import type { ErrorObject } from 'ajv';
import { defaultLimitConfig } from '~/helpers/extractLimitAndOffset'; 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 { export class NcBaseError extends Error {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
@ -485,6 +424,10 @@ const errorHelpers: {
code: number; code: number;
}; };
} = { } = {
[NcErrorType.UNKNOWN_ERROR]: {
message: 'Something went wrong',
code: 500,
},
[NcErrorType.INTERNAL_SERVER_ERROR]: { [NcErrorType.INTERNAL_SERVER_ERROR]: {
message: (message: string) => message || `Internal server error`, message: (message: string) => message || `Internal server error`,
code: 500, code: 500,

4
packages/nocodb/src/middlewares/catchError.ts

@ -1,4 +0,0 @@
// todo: remove this file
export * from '~/helpers/catchError';
import catchError from '~/helpers/catchError';
export default catchError;

2
packages/nocodb/src/middlewares/extract-ids/extract-ids.middleware.ts

@ -25,7 +25,7 @@ import {
View, View,
} from '~/models'; } from '~/models';
import rolePermissions from '~/utils/acl'; import rolePermissions from '~/utils/acl';
import { NcError } from '~/middlewares/catchError'; import { NcError } from '~/helpers/catchError';
export const rolesLabel = { export const rolesLabel = {
[OrgUserRoles.SUPER_ADMIN]: 'Super Admin', [OrgUserRoles.SUPER_ADMIN]: 'Super Admin',

Loading…
Cancel
Save