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