mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-10 13:38:49 +08:00
refactor(server): dont convert graphql bad request into internal server error (#10203)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { ApolloServerErrorCode } from '@apollo/server/errors';
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
@@ -9,10 +10,12 @@ import { GqlContextType } from '@nestjs/graphql';
|
||||
import { ThrottlerException } from '@nestjs/throttler';
|
||||
import { BaseWsExceptionFilter } from '@nestjs/websockets';
|
||||
import { Response } from 'express';
|
||||
import { GraphQLError } from 'graphql';
|
||||
import { of } from 'rxjs';
|
||||
import { Socket } from 'socket.io';
|
||||
|
||||
import {
|
||||
GraphqlBadRequest,
|
||||
InternalServerError,
|
||||
NotFound,
|
||||
TooManyRequest,
|
||||
@@ -21,7 +24,28 @@ import {
|
||||
import { metrics } from '../metrics';
|
||||
import { getRequestIdFromHost } from '../utils';
|
||||
|
||||
function isGraphQLBadRequest(error: any): error is GraphQLError {
|
||||
// https://www.apollographql.com/docs/apollo-server/data/errors
|
||||
const code = error.extensions?.code;
|
||||
return (
|
||||
code === ApolloServerErrorCode.GRAPHQL_PARSE_FAILED ||
|
||||
code === ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED ||
|
||||
code === ApolloServerErrorCode.BAD_USER_INPUT ||
|
||||
code === ApolloServerErrorCode.BAD_REQUEST
|
||||
);
|
||||
}
|
||||
|
||||
export function mapAnyError(error: any): UserFriendlyError {
|
||||
if (error instanceof GraphQLError) {
|
||||
const err = error;
|
||||
if (isGraphQLBadRequest(error)) {
|
||||
return new GraphqlBadRequest({
|
||||
code: err.extensions.code as string,
|
||||
message: err.message,
|
||||
});
|
||||
}
|
||||
error = err.originalError ?? error;
|
||||
}
|
||||
if (error instanceof UserFriendlyError) {
|
||||
return error;
|
||||
} else if (error instanceof ThrottlerException) {
|
||||
|
||||
Reference in New Issue
Block a user