Skip to content

Commit c9d86a8

Browse files
committed
init common exception handling
1 parent c425d2a commit c9d86a8

10 files changed

+89
-23
lines changed

packages/twenty-server/src/engine/api/common/common-query-runners/common-find-one-query-runner.service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-qu
1111
import { FindOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
1212

1313
import { CommonBaseQueryRunnerService } from 'src/engine/api/common/common-query-runners/common-base-query-runner.service';
14+
import {
15+
CommonQueryRunnerException,
16+
CommonQueryRunnerExceptionCode,
17+
} from 'src/engine/api/common/common-query-runners/errors/common-query-runner.exception';
1418
import { CommonBaseQueryRunnerContext } from 'src/engine/api/common/types/common-base-query-runner-context.type';
1519
import { FindOneQueryArgs } from 'src/engine/api/common/types/common-query-args.type';
16-
import {
17-
GraphqlQueryRunnerException,
18-
GraphqlQueryRunnerExceptionCode,
19-
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
2020
import { GraphqlQueryParser } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query.parser';
2121
import { ObjectRecordsToGraphqlConnectionHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/object-records-to-graphql-connection.helper';
2222
import { buildColumnsToSelect } from 'src/engine/api/graphql/graphql-query-runner/utils/build-columns-to-select';
@@ -74,9 +74,9 @@ export class CommonFindOneQueryRunnerService extends CommonBaseQueryRunnerServic
7474

7575
if (!objectRecord) {
7676
//TODO : Refacto-common - Exception handler should be Common
77-
throw new GraphqlQueryRunnerException(
77+
throw new CommonQueryRunnerException(
7878
'Record not found',
79-
GraphqlQueryRunnerExceptionCode.RECORD_NOT_FOUND,
79+
CommonQueryRunnerExceptionCode.RECORD_NOT_FOUND,
8080
);
8181
}
8282

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { CustomException } from 'src/utils/custom-exception';
2+
3+
export class CommonQueryRunnerException extends CustomException<CommonQueryRunnerExceptionCode> {}
4+
5+
export enum CommonQueryRunnerExceptionCode {
6+
RECORD_NOT_FOUND = 'RECORD_NOT_FOUND',
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
CommonQueryRunnerExceptionCode,
3+
type CommonQueryRunnerException,
4+
} from 'src/engine/api/common/common-query-runners/errors/common-query-runner.exception';
5+
import { NotFoundError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
6+
7+
export const commonQueryRunnerToGraphqlApiExceptionHandler = (
8+
error: CommonQueryRunnerException,
9+
) => {
10+
switch (error.code) {
11+
case CommonQueryRunnerExceptionCode.RECORD_NOT_FOUND:
12+
throw new NotFoundError(error);
13+
default:
14+
throw error;
15+
}
16+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BadRequestException } from '@nestjs/common';
2+
3+
import {
4+
CommonQueryRunnerExceptionCode,
5+
type CommonQueryRunnerException,
6+
} from 'src/engine/api/common/common-query-runners/errors/common-query-runner.exception';
7+
8+
export const commonQueryRunnerToRestApiExceptionHandler = (
9+
error: CommonQueryRunnerException,
10+
) => {
11+
switch (error.code) {
12+
case CommonQueryRunnerExceptionCode.RECORD_NOT_FOUND:
13+
throw new BadRequestException('Record not found');
14+
default:
15+
throw error;
16+
}
17+
};

packages/twenty-server/src/engine/api/graphql/graphql-query-runner/interfaces/base-resolver-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { workspaceQueryRunnerGraphqlApiExceptionHandler } from 'src/engine/api/g
2828
import { WorkspaceQueryHookService } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.service';
2929
import { ApiKeyRoleService } from 'src/engine/core-modules/api-key/api-key-role.service';
3030
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
31+
import { WorkspaceNotFoundDefaultError } from 'src/engine/core-modules/workspace/workspace.exception';
3132
import { type PermissionFlagType } from 'src/engine/metadata-modules/permissions/constants/permission-flag-type.constants';
3233
import {
3334
PermissionsException,
@@ -39,7 +40,6 @@ import { UserRoleService } from 'src/engine/metadata-modules/user-role/user-role
3940
import { type WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
4041
import { type WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
4142
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
42-
import { WorkspaceNotFoundDefaultError } from 'src/engine/core-modules/workspace/workspace.exception';
4343

4444
export type GraphqlQueryResolverExecutionArgs<Input extends ResolverArgs> = {
4545
args: Input;

packages/twenty-server/src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-one-resolver.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//TODO : Refacto-common - To delete
12
import { Injectable } from '@nestjs/common';
23

34
import { QUERY_MAX_RECORDS } from 'twenty-shared/constants';

packages/twenty-server/src/engine/api/graphql/workspace-query-runner/utils/workspace-query-runner-graphql-api-exception-handler.util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { type QueryFailedError } from 'typeorm';
22

3+
import { CommonQueryRunnerException } from 'src/engine/api/common/common-query-runners/errors/common-query-runner.exception';
4+
import { commonQueryRunnerToGraphqlApiExceptionHandler } from 'src/engine/api/common/common-query-runners/utils/common-query-runner-to-graphql-api-exception-handler.util';
35
import { GraphqlQueryRunnerException } from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
46
import { graphqlQueryRunnerExceptionHandler } from 'src/engine/api/graphql/workspace-query-runner/utils/graphql-query-runner-exception-handler.util';
57
import { workspaceExceptionHandler } from 'src/engine/api/graphql/workspace-query-runner/utils/workspace-exception-handler.util';
@@ -34,6 +36,8 @@ export const workspaceQueryRunnerGraphqlApiExceptionHandler = (
3436
return graphqlQueryRunnerExceptionHandler(error);
3537
case error instanceof TwentyORMException:
3638
return twentyORMGraphqlApiExceptionHandler(error);
39+
case error instanceof CommonQueryRunnerException:
40+
return commonQueryRunnerToGraphqlApiExceptionHandler(error);
3741
case error instanceof AuthException:
3842
return authGraphqlApiExceptionHandler(error);
3943
case error instanceof ApiKeyException:

packages/twenty-server/src/engine/api/rest/core/handlers/rest-api-find-one.handler.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RestApiBaseHandler } from 'src/engine/api/rest/core/interfaces/rest-api
88
import { CommonFindOneQueryRunnerService } from 'src/engine/api/common/common-query-runners/common-find-one-query-runner.service';
99
import { CommonQueryNames } from 'src/engine/api/common/types/common-query-args.type';
1010
import { parseCorePath } from 'src/engine/api/rest/core/query-builder/utils/path-parsers/parse-core-path.utils';
11+
import { workspaceQueryRunnerRestApiExceptionHandler } from 'src/engine/api/rest/utils/workspace-query-runner-rest-api-exception-handler.util';
1112

1213
@Injectable()
1314
export class RestApiFindOneHandler extends RestApiBaseHandler {
@@ -18,24 +19,25 @@ export class RestApiFindOneHandler extends RestApiBaseHandler {
1819
}
1920

2021
async handle(request: Request) {
21-
const { args, options } =
22-
await this.buildCommonArgsAndOptionsFromRestRequest(request);
22+
try {
23+
const { args, options } =
24+
await this.buildCommonArgsAndOptionsFromRestRequest(request);
2325

24-
const record = await this.commonFindOneQueryRunnerService.execute(
25-
args,
26-
options,
27-
CommonQueryNames.findOne,
28-
);
26+
const record = await this.commonFindOneQueryRunnerService.execute(
27+
args,
28+
options,
29+
CommonQueryNames.findOne,
30+
);
2931

30-
if (!isDefined(record)) {
31-
throw new BadRequestException('Record not found');
32+
return this.formatResult({
33+
operation: CommonQueryNames.findOne,
34+
objectNameSingular:
35+
options.objectMetadataItemWithFieldMaps.nameSingular,
36+
data: record,
37+
});
38+
} catch (error) {
39+
workspaceQueryRunnerRestApiExceptionHandler(error);
3240
}
33-
34-
return this.formatResult({
35-
operation: CommonQueryNames.findOne,
36-
objectNameSingular: options.objectMetadataItemWithFieldMaps.nameSingular,
37-
data: record,
38-
});
3941
}
4042

4143
private async buildCommonArgsAndOptionsFromRestRequest(request: Request) {

packages/twenty-server/src/engine/api/rest/core/interfaces/rest-api-base.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export abstract class RestApiBaseHandler {
101101

102102
protected abstract handle(
103103
request: Request,
104-
): Promise<FormatResult | { data: FormatResult[] }>;
104+
): Promise<FormatResult | { data: FormatResult[] } | undefined>;
105105

106106
public async getRepositoryAndMetadataOrFail(request: Request) {
107107
const { workspace, apiKey, userWorkspaceId } = request;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { type QueryFailedError } from 'typeorm';
2+
3+
import { CommonQueryRunnerException } from 'src/engine/api/common/common-query-runners/errors/common-query-runner.exception';
4+
import { commonQueryRunnerToRestApiExceptionHandler } from 'src/engine/api/common/common-query-runners/utils/common-query-runner-to-rest-api-exception-handler.util';
5+
6+
interface QueryFailedErrorWithCode extends QueryFailedError {
7+
code: string;
8+
}
9+
10+
export const workspaceQueryRunnerRestApiExceptionHandler = (
11+
error: QueryFailedErrorWithCode,
12+
) => {
13+
switch (true) {
14+
case error instanceof CommonQueryRunnerException:
15+
return commonQueryRunnerToRestApiExceptionHandler(error);
16+
default:
17+
throw error;
18+
}
19+
};

0 commit comments

Comments
 (0)