GraphQL: Improve session token error messages (#5753)
* GraphQL: Improve session token error message Fixes the session token related error messages during GraphQL operations. If any authentication error were thrown, it was not correctly handled by the GraphQL express middleware, and ended responding the request with a JSON parsing error. * Refactor handleError usage * Use handleParseErrors middleware to handle invalid session token error * fix: Status code 400 when session token is invalid * fix: Undo handleParseErrors middleware change
This commit is contained in:
committed by
Antonio Davi Macedo Coelho de Castro
parent
1c62ab6f49
commit
f91034ab8c
@@ -1,6 +1,5 @@
|
||||
import Parse from 'parse/node';
|
||||
import { GraphQLSchema, GraphQLObjectType } from 'graphql';
|
||||
import { ApolloError } from 'apollo-server-core';
|
||||
import requiredParameter from '../requiredParameter';
|
||||
import * as defaultGraphQLTypes from './loaders/defaultGraphQLTypes';
|
||||
import * as parseClassTypes from './loaders/parseClassTypes';
|
||||
@@ -8,6 +7,7 @@ import * as parseClassQueries from './loaders/parseClassQueries';
|
||||
import * as parseClassMutations from './loaders/parseClassMutations';
|
||||
import * as defaultGraphQLQueries from './loaders/defaultGraphQLQueries';
|
||||
import * as defaultGraphQLMutations from './loaders/defaultGraphQLMutations';
|
||||
import { toGraphQLError } from './parseGraphQLUtils';
|
||||
|
||||
class ParseGraphQLSchema {
|
||||
constructor(databaseController, log) {
|
||||
@@ -100,17 +100,12 @@ class ParseGraphQLSchema {
|
||||
}
|
||||
|
||||
handleError(error) {
|
||||
let code, message;
|
||||
if (error instanceof Parse.Error) {
|
||||
this.log.error('Parse error: ', error);
|
||||
code = error.code;
|
||||
message = error.message;
|
||||
} else {
|
||||
this.log.error('Uncaught internal server error.', error, error.stack);
|
||||
code = Parse.Error.INTERNAL_SERVER_ERROR;
|
||||
message = 'Internal server error.';
|
||||
}
|
||||
throw new ApolloError(message, code);
|
||||
throw toGraphQLError(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { graphqlExpress } from 'apollo-server-express/dist/expressApollo';
|
||||
import { renderPlaygroundPage } from '@apollographql/graphql-playground-html';
|
||||
import { execute, subscribe } from 'graphql';
|
||||
import { SubscriptionServer } from 'subscriptions-transport-ws';
|
||||
import { handleParseHeaders } from '../middlewares';
|
||||
import { handleParseErrors, handleParseHeaders } from '../middlewares';
|
||||
import requiredParameter from '../requiredParameter';
|
||||
import defaultLogger from '../logger';
|
||||
import { ParseGraphQLSchema } from './ParseGraphQLSchema';
|
||||
@@ -55,6 +55,7 @@ class ParseGraphQLServer {
|
||||
app.use(this.config.graphQLPath, corsMiddleware());
|
||||
app.use(this.config.graphQLPath, bodyParser.json());
|
||||
app.use(this.config.graphQLPath, handleParseHeaders);
|
||||
app.use(this.config.graphQLPath, handleParseErrors);
|
||||
app.use(
|
||||
this.config.graphQLPath,
|
||||
graphqlExpress(async req => await this._getGraphQLOptions(req))
|
||||
|
||||
14
src/GraphQL/parseGraphQLUtils.js
Normal file
14
src/GraphQL/parseGraphQLUtils.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import Parse from 'parse/node';
|
||||
import { ApolloError } from 'apollo-server-core';
|
||||
|
||||
export function toGraphQLError(error) {
|
||||
let code, message;
|
||||
if (error instanceof Parse.Error) {
|
||||
code = error.code;
|
||||
message = error.message;
|
||||
} else {
|
||||
code = Parse.Error.INTERNAL_SERVER_ERROR;
|
||||
message = 'Internal server error';
|
||||
}
|
||||
return new ApolloError(message, code);
|
||||
}
|
||||
Reference in New Issue
Block a user