Skip to content

GraphQL: Improve session token error messages #5753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3198,8 +3198,8 @@ describe('ParseGraphQLServer', () => {
});
expect(logOut.data.users.logOut).toBeTruthy();

await expectAsync(
apolloClient.query({
try {
await apolloClient.query({
query: gql`
query GetCurrentUser {
users {
Expand All @@ -3214,8 +3214,13 @@ describe('ParseGraphQLServer', () => {
'X-Parse-Session-Token': sessionToken,
},
},
})
).toBeRejected();
});
} catch (err) {
expect(err.networkError.result).toEqual({
code: 209,
error: 'Invalid session token',
});
}
});
});

Expand Down
9 changes: 2 additions & 7 deletions src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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';
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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/GraphQL/ParseGraphQLServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions src/GraphQL/ParseGraphQLUtils.js
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 3 additions & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ export function handleParseErrors(err, req, res, next) {
case Parse.Error.OBJECT_NOT_FOUND:
httpStatus = 404;
break;
case Parse.Error.INVALID_SESSION_TOKEN:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

humm.. I'm not sure... I think it can break some apps. Let's just leave 400 as it was before.

httpStatus = 401;
break;
default:
httpStatus = 400;
}
Expand Down