From 18273caade9de547d2079988d1d0623d1b80c9c8 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sat, 17 Jun 2023 14:24:05 +0530 Subject: [PATCH] refactor: error code corrections Signed-off-by: Pranav C --- packages/nocodb/src/db/BaseModelSqlv2.ts | 32 +++++++++++++++---- .../nocodb/src/services/data-table.service.ts | 10 +++--- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 071cb9b77d..08d1514a30 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -17,8 +17,8 @@ import Validator from 'validator'; import { customAlphabet } from 'nanoid'; import DOMPurify from 'isomorphic-dompurify'; import { v4 as uuidv4 } from 'uuid'; -import { extractLimitAndOffset } from '../helpers'; import { Knex } from 'knex'; +import { extractLimitAndOffset } from '../helpers'; import { NcError } from '../helpers/catchError'; import getAst from '../helpers/getAst'; import { @@ -3628,7 +3628,7 @@ class BaseModelSqlv2 { const column = columns.find((c) => c.id === colId); if (!column || column.uidt !== UITypes.LinkToAnotherRecord) - NcError.notFound('Column not found'); + NcError.notFound(`Link column with id ${colId} not found`); const row = await this.dbDriver(this.tnPath) .where(await this._wherePk(rowId)) @@ -3636,7 +3636,7 @@ class BaseModelSqlv2 { // validate rowId if (!row) { - NcError.notFound('Row not found'); + NcError.notFound(`Row with id ${rowId} not found`); } const colOptions = await column.getColOptions(); @@ -3809,7 +3809,9 @@ class BaseModelSqlv2 { const childRow = await childRowsQb; if (!childRow) { - NcError.unprocessableEntity(`Child record with id ${childIds[0]} not found`); + NcError.unprocessableEntity( + `Child record with id ${childIds[0]} not found`, + ); } } @@ -3849,7 +3851,7 @@ class BaseModelSqlv2 { const column = columns.find((c) => c.id === colId); if (!column || column.uidt !== UITypes.LinkToAnotherRecord) - NcError.notFound('Column not found'); + NcError.notFound(`Link column with id ${colId} not found`); const row = await this.dbDriver(this.tnPath) .where(await this._wherePk(rowId)) @@ -3857,7 +3859,7 @@ class BaseModelSqlv2 { // validate rowId if (!row) { - NcError.notFound('Row not found'); + NcError.notFound(`Row with id ${rowId} not found`); } const colOptions = await column.getColOptions(); @@ -3961,6 +3963,11 @@ class BaseModelSqlv2 { { // validate Ids { + if (childIds.length > 1) + NcError.unprocessableEntity( + 'Request must contain only one parent id', + ); + const childRowsQb = this.dbDriver(parentTn) .select(parentTable.primaryKey.column_name) .where(_wherePk(parentTable.primaryKeys, childIds[0])) @@ -3969,7 +3976,9 @@ class BaseModelSqlv2 { const childRow = await childRowsQb; if (!childRow) { - NcError.unprocessableEntity(`Child record with id ${childIds[0]} not found`); + NcError.unprocessableEntity( + `Child record with id ${childIds[0]} not found`, + ); } } @@ -4004,6 +4013,15 @@ class BaseModelSqlv2 { (c) => c.id === colId, ); + const row = await this.dbDriver(this.tnPath) + .where(await this._wherePk(id)) + .first(); + + // validate rowId + if (!row) { + NcError.notFound(`Row with id ${id} not found`); + } + const parentCol = await ( (await relColumn.getColOptions()) as LinkToAnotherRecordColumn ).getParentColumn(); diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts index 31f3920441..1060c4310f 100644 --- a/packages/nocodb/src/services/data-table.service.ts +++ b/packages/nocodb/src/services/data-table.service.ts @@ -6,7 +6,6 @@ import getAst from '../helpers/getAst'; import { PagedResponseImpl } from '../helpers/PagedResponse'; import { Base, Column, Model, View } from '../models'; import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2'; -import { referencedRowIdParam } from './api-docs/swagger/templates/params'; import { DatasService } from './datas.service'; import type { LinkToAnotherRecordColumn } from '../models'; @@ -169,7 +168,7 @@ export class DataTableService { const model = await Model.get(param.modelId); if (!model) { - NcError.notFound(`Table '${param.modelId}' not found`); + NcError.notFound(`Table with id '${param.modelId}' not found`); } if (param.projectId && model.project_id !== param.projectId) { @@ -181,7 +180,7 @@ export class DataTableService { if (param.viewId) { view = await View.get(param.viewId); if (!view || (view.fk_model_id && view.fk_model_id !== param.modelId)) { - NcError.unprocessableEntity(`View '${param.viewId}' not found`); + NcError.unprocessableEntity(`View with id '${param.viewId}' not found`); } } @@ -322,7 +321,7 @@ export class DataTableService { private async getColumn(param: { modelId: string; columnId: string }) { const column = await Column.get({ colId: param.columnId }); - if (!column) NcError.badRequest('Column not found'); + if (!column) NcError.badRequest(`Column with id '${param.columnId}' not found`); if (column.fk_model_id !== param.modelId) NcError.badRequest('Column not belong to model'); @@ -344,7 +343,6 @@ export class DataTableService { this.validateIds(param.refRowIds); const { model, view } = await this.getModelAndView(param); - if (!model) NcError.notFound('Table not found'); const base = await Base.get(model.base_id); @@ -380,7 +378,7 @@ export class DataTableService { this.validateIds(param.refRowIds); const { model, view } = await this.getModelAndView(param); - if (!model) NcError.notFound('Table not found'); + if (!model) NcError.notFound('Table with id ' + param.modelId + ' not found'); const base = await Base.get(model.base_id);