Skip to content

Commit

Permalink
Fix metadata exception handler #2 (#3357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiko authored Jan 10, 2024
1 parent 4f9ea78 commit 22047fa
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { HttpException } from '@nestjs/common';

import { TypeORMError } from 'typeorm';

import {
AuthenticationError,
BaseGraphQLError,
Expand Down Expand Up @@ -29,12 +27,10 @@ export const handleException = (
exception: Error,
exceptionHandlerService: ExceptionHandlerService,
): void => {
if (
exception instanceof TypeORMError ||
(exception instanceof HttpException && exception.getStatus() >= 500)
) {
exceptionHandlerService.captureException(exception);
if (exception instanceof HttpException && exception.getStatus() < 500) {
return;
}
exceptionHandlerService.captureException(exception);
};

export const convertExceptionToGraphQLError = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ExceptionHandlerSentryDriver
{
constructor(options: ExceptionHandlerSentryDriverFactoryOptions['options']) {
Sentry.init({
dsn: options.dns,
dsn: options.dsn,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const exceptionHandlerModuleFactory = async (
return {
type: ExceptionHandlerDriver.Sentry,
options: {
dns: environmentService.getSentryDSN() ?? '',
dsn: environmentService.getSentryDSN() ?? '',
serverInstance: adapterHost.httpAdapter?.getInstance(),
debug: environmentService.isDebugMode(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum ExceptionHandlerDriver {
export interface ExceptionHandlerSentryDriverFactoryOptions {
type: ExceptionHandlerDriver.Sentry;
options: {
dns: string;
dsn: string;
serverInstance?: Router;
debug?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ export class FieldMetadataResolver {
@Args('input') input: CreateOneFieldMetadataInput,
@AuthWorkspace() { id: workspaceId }: Workspace,
) {
try {
return this.fieldMetadataService.createOne({
...input.field,
workspaceId,
});
} catch (error) {
console.log(error);
}
return this.fieldMetadataService.createOne({
...input.field,
workspaceId,
});
}

@Mutation(() => FieldMetadataDTO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
);

if (!objectMetadata) {
throw new Error('Object does not exist');
throw new NotFoundException('Object does not exist');
}

const fieldAlreadyExists = await this.fieldMetadataRepository.findOne({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const metadataModuleFactory = async (
error.originalError,
exceptionHandlerService,
);
} else {
return maskError(error, message, isDev);
}

return maskError(error, message, isDev);
},
},
});
3 changes: 2 additions & 1 deletion packages/twenty-server/src/metadata/metadata.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { YogaDriverConfig, YogaDriver } from '@graphql-yoga/nestjs';
import { WorkspaceMigrationRunnerModule } from 'src/workspace/workspace-migration-runner/workspace-migration-runner.module';
import { WorkspaceMigrationModule } from 'src/metadata/workspace-migration/workspace-migration.module';
import { metadataModuleFactory } from 'src/metadata/metadata.module-factory';
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';

import { DataSourceModule } from './data-source/data-source.module';
import { FieldMetadataModule } from './field-metadata/field-metadata.module';
Expand All @@ -15,7 +16,7 @@ import { RelationMetadataModule } from './relation-metadata/relation-metadata.mo
imports: [
GraphQLModule.forRootAsync<YogaDriverConfig>({
driver: YogaDriver,
imports: [],
inject: [ExceptionHandlerService],
useFactory: metadataModuleFactory,
}),
DataSourceModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { FindManyOptions, FindOneOptions, Repository } from 'typeorm';
Expand Down Expand Up @@ -59,7 +59,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
objectMetadataInput.nameSingular.toLowerCase() ===
objectMetadataInput.namePlural.toLowerCase()
) {
throw new Error(
throw new BadRequestException(
'The singular and plural name cannot be the same for an object',
);
}
Expand Down

0 comments on commit 22047fa

Please sign in to comment.