Fix capture exception for metadata and core (#3335)
This commit is contained in:
@ -1,14 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GraphQLModule } from '@nestjs/graphql';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { APP_FILTER } from '@nestjs/core';
|
||||
|
||||
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
|
||||
|
||||
import { GraphQLConfigService } from 'src/graphql-config.service';
|
||||
import { GlobalExceptionFilter } from 'src/filters/global-exception.filter';
|
||||
|
||||
import { AppService } from './app.service';
|
||||
|
||||
import { CoreModule } from './core/core.module';
|
||||
import { IntegrationsModule } from './integrations/integrations.module';
|
||||
@ -30,12 +26,5 @@ import { WorkspaceModule } from './workspace/workspace.module';
|
||||
CoreModule,
|
||||
WorkspaceModule,
|
||||
],
|
||||
providers: [
|
||||
AppService,
|
||||
{
|
||||
provide: APP_FILTER,
|
||||
useValue: GlobalExceptionFilter,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
import { Catch, Injectable } from '@nestjs/common';
|
||||
import { GqlExceptionFilter } from '@nestjs/graphql';
|
||||
|
||||
import { globalExceptionHandler } from 'src/filters/utils/global-exception-handler.util';
|
||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||
|
||||
@Catch()
|
||||
@Injectable()
|
||||
export class GlobalExceptionFilter implements GqlExceptionFilter {
|
||||
constructor(
|
||||
private readonly exceptionHandlerService: ExceptionHandlerService,
|
||||
) {}
|
||||
|
||||
catch(exception: unknown) {
|
||||
return globalExceptionHandler(exception, this.exceptionHandlerService);
|
||||
}
|
||||
}
|
||||
@ -6,42 +6,50 @@ import {
|
||||
AuthenticationError,
|
||||
BaseGraphQLError,
|
||||
ForbiddenError,
|
||||
ValidationError,
|
||||
} from 'src/filters/utils/graphql-errors.util';
|
||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||
|
||||
const graphQLPredefinedExceptions = {
|
||||
400: ValidationError,
|
||||
401: AuthenticationError,
|
||||
403: ForbiddenError,
|
||||
};
|
||||
|
||||
export const globalExceptionHandler = (
|
||||
exception: unknown,
|
||||
export const handleExceptionAndConvertToGraphQLError = (
|
||||
exception: Error,
|
||||
exceptionHandlerService: ExceptionHandlerService,
|
||||
) => {
|
||||
if (exception instanceof HttpException) {
|
||||
return httpExceptionHandler(exception, exceptionHandlerService);
|
||||
}
|
||||
): BaseGraphQLError => {
|
||||
handleException(exception, exceptionHandlerService);
|
||||
|
||||
if (exception instanceof TypeORMError) {
|
||||
return typeOrmExceptionHandler(exception, exceptionHandlerService);
|
||||
}
|
||||
|
||||
exceptionHandlerService.captureException(exception);
|
||||
|
||||
return exception;
|
||||
return convertExceptionToGraphQLError(exception);
|
||||
};
|
||||
|
||||
export const httpExceptionHandler = (
|
||||
exception: HttpException,
|
||||
export const handleException = (
|
||||
exception: Error,
|
||||
exceptionHandlerService: ExceptionHandlerService,
|
||||
) => {
|
||||
const status = exception.getStatus();
|
||||
let error: BaseGraphQLError;
|
||||
|
||||
// Capture all 5xx errors and send them to exception handler
|
||||
if (status >= 500) {
|
||||
): void => {
|
||||
if (
|
||||
exception instanceof TypeORMError ||
|
||||
(exception instanceof HttpException && exception.getStatus() >= 500)
|
||||
) {
|
||||
exceptionHandlerService.captureException(exception);
|
||||
}
|
||||
};
|
||||
|
||||
export const convertExceptionToGraphQLError = (
|
||||
exception: Error,
|
||||
): BaseGraphQLError => {
|
||||
if (exception instanceof HttpException) {
|
||||
return convertHttpExceptionToGraphql(exception);
|
||||
}
|
||||
|
||||
return convertExceptionToGraphql(exception);
|
||||
};
|
||||
|
||||
export const convertHttpExceptionToGraphql = (exception: HttpException) => {
|
||||
const status = exception.getStatus();
|
||||
let error: BaseGraphQLError;
|
||||
|
||||
if (status in graphQLPredefinedExceptions) {
|
||||
error = new graphQLPredefinedExceptions[exception.getStatus()](
|
||||
@ -60,12 +68,7 @@ export const httpExceptionHandler = (
|
||||
return error;
|
||||
};
|
||||
|
||||
export const typeOrmExceptionHandler = (
|
||||
exception: TypeORMError,
|
||||
exceptionHandlerService: ExceptionHandlerService,
|
||||
) => {
|
||||
exceptionHandlerService.captureException(exception);
|
||||
|
||||
export const convertExceptionToGraphql = (exception: Error) => {
|
||||
const error = new BaseGraphQLError(exception.name, 'INTERNAL_SERVER_ERROR');
|
||||
|
||||
error.stack = exception.stack;
|
||||
|
||||
@ -9,14 +9,18 @@ import {
|
||||
import { GraphQLSchema, GraphQLError } from 'graphql';
|
||||
import GraphQLJSON from 'graphql-type-json';
|
||||
import { JsonWebTokenError, TokenExpiredError } from 'jsonwebtoken';
|
||||
import { GraphQLSchemaWithContext, YogaInitialContext } from 'graphql-yoga';
|
||||
import {
|
||||
GraphQLSchemaWithContext,
|
||||
YogaInitialContext,
|
||||
maskError,
|
||||
} from 'graphql-yoga';
|
||||
|
||||
import { TokenService } from 'src/core/auth/services/token.service';
|
||||
import { CoreModule } from 'src/core/core.module';
|
||||
import { Workspace } from 'src/core/workspace/workspace.entity';
|
||||
import { WorkspaceFactory } from 'src/workspace/workspace.factory';
|
||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||
import { globalExceptionHandler } from 'src/filters/utils/global-exception-handler.util';
|
||||
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
||||
|
||||
@Injectable()
|
||||
export class GraphQLConfigService
|
||||
@ -29,10 +33,24 @@ export class GraphQLConfigService
|
||||
) {}
|
||||
|
||||
createGqlOptions(): YogaDriverConfig {
|
||||
const exceptionHandlerService = this.exceptionHandlerService;
|
||||
|
||||
return {
|
||||
context: ({ req }) => ({ req }),
|
||||
autoSchemaFile: true,
|
||||
include: [CoreModule],
|
||||
maskedErrors: {
|
||||
maskError(error: GraphQLError, message, isDev) {
|
||||
if (error.originalError) {
|
||||
return handleExceptionAndConvertToGraphQLError(
|
||||
error.originalError,
|
||||
exceptionHandlerService,
|
||||
);
|
||||
}
|
||||
|
||||
return maskError(error, message, isDev);
|
||||
},
|
||||
},
|
||||
conditionalSchema: async (context) => {
|
||||
try {
|
||||
let workspace: Workspace;
|
||||
@ -63,7 +81,10 @@ export class GraphQLConfigService
|
||||
});
|
||||
}
|
||||
|
||||
throw globalExceptionHandler(error, this.exceptionHandlerService);
|
||||
throw handleExceptionAndConvertToGraphQLError(
|
||||
error,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
}
|
||||
},
|
||||
resolvers: { JSON: GraphQLJSON },
|
||||
|
||||
@ -19,10 +19,14 @@ export class FieldMetadataResolver {
|
||||
@Args('input') input: CreateOneFieldMetadataInput,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
) {
|
||||
return this.fieldMetadataService.createOne({
|
||||
...input.field,
|
||||
workspaceId,
|
||||
});
|
||||
try {
|
||||
return this.fieldMetadataService.createOne({
|
||||
...input.field,
|
||||
workspaceId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Mutation(() => FieldMetadataDTO)
|
||||
|
||||
@ -56,7 +56,7 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
||||
);
|
||||
|
||||
if (!objectMetadata) {
|
||||
throw new NotFoundException('Object does not exist');
|
||||
throw new Error('Object does not exist');
|
||||
}
|
||||
|
||||
const fieldAlreadyExists = await this.fieldMetadataRepository.findOne({
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
|
||||
import { GraphQLError } from 'graphql';
|
||||
import GraphQLJSON from 'graphql-type-json';
|
||||
import { maskError } from 'graphql-yoga';
|
||||
|
||||
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||
import { MetadataModule } from 'src/metadata/metadata.module';
|
||||
|
||||
export const metadataModuleFactory = async (
|
||||
exceptionHandlerService: ExceptionHandlerService,
|
||||
): Promise<YogaDriverConfig> => ({
|
||||
context: ({ req }) => ({ req }),
|
||||
driver: YogaDriver,
|
||||
autoSchemaFile: true,
|
||||
include: [MetadataModule],
|
||||
resolvers: { JSON: GraphQLJSON },
|
||||
plugins: [],
|
||||
path: '/metadata',
|
||||
maskedErrors: {
|
||||
maskError(error: GraphQLError, message, isDev) {
|
||||
if (error.originalError) {
|
||||
return handleExceptionAndConvertToGraphQLError(
|
||||
error.originalError,
|
||||
exceptionHandlerService,
|
||||
);
|
||||
} else {
|
||||
return maskError(error, message, isDev);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -2,10 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
import { GraphQLModule } from '@nestjs/graphql';
|
||||
|
||||
import { YogaDriverConfig, YogaDriver } from '@graphql-yoga/nestjs';
|
||||
import GraphQLJSON from 'graphql-type-json';
|
||||
|
||||
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 { DataSourceModule } from './data-source/data-source.module';
|
||||
import { FieldMetadataModule } from './field-metadata/field-metadata.module';
|
||||
@ -13,14 +13,10 @@ import { ObjectMetadataModule } from './object-metadata/object-metadata.module';
|
||||
import { RelationMetadataModule } from './relation-metadata/relation-metadata.module';
|
||||
@Module({
|
||||
imports: [
|
||||
GraphQLModule.forRoot<YogaDriverConfig>({
|
||||
context: ({ req }) => ({ req }),
|
||||
GraphQLModule.forRootAsync<YogaDriverConfig>({
|
||||
driver: YogaDriver,
|
||||
autoSchemaFile: true,
|
||||
include: [MetadataModule],
|
||||
resolvers: { JSON: GraphQLJSON },
|
||||
plugins: [],
|
||||
path: '/metadata',
|
||||
imports: [],
|
||||
useFactory: metadataModuleFactory,
|
||||
}),
|
||||
DataSourceModule,
|
||||
FieldMetadataModule,
|
||||
|
||||
@ -34,7 +34,7 @@ import {
|
||||
} from 'src/workspace/workspace-query-runner/jobs/call-webhook-jobs.job';
|
||||
import { parseResult } from 'src/workspace/workspace-query-runner/utils/parse-result.util';
|
||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||
import { globalExceptionHandler } from 'src/filters/utils/global-exception-handler.util';
|
||||
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
||||
|
||||
import { WorkspaceQueryRunnerOptions } from './interfaces/query-runner-optionts.interface';
|
||||
import {
|
||||
@ -72,7 +72,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return this.parseResult<IConnection<Record>>(result, targetTableName, '');
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
@ -106,7 +106,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return parsedResult?.edges?.[0]?.node;
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
@ -141,7 +141,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return parsedResults;
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
@ -185,7 +185,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return parsedResults?.[0];
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
@ -220,7 +220,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return parsedResults?.[0];
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
@ -255,7 +255,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return parsedResults;
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
@ -293,7 +293,7 @@ export class WorkspaceQueryRunnerService {
|
||||
|
||||
return parsedResults;
|
||||
} catch (exception) {
|
||||
const error = globalExceptionHandler(
|
||||
const error = handleExceptionAndConvertToGraphQLError(
|
||||
exception,
|
||||
this.exceptionHandlerService,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user