Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: shared form pw error handling #7991

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/nc-gui/composables/useSharedFormViewStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions packages/nc-gui/utils/errorUtils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }
1 change: 1 addition & 0 deletions packages/nocodb-sdk/src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
65 changes: 4 additions & 61 deletions packages/nocodb/src/helpers/catchError.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions packages/nocodb/src/middlewares/catchError.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down