Skip to content

Commit

Permalink
Merge branch 'main' into expose-isVisible-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jtourkos committed Dec 4, 2024
2 parents 7952a8f + 5e70547 commit ee1efce
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,53 @@ export interface Context {
const app = express();
const httpServer = http.createServer(app);

const server = new ApolloServer<Context>({
formatError: (formattedError, error: any) => {
if (error instanceof BaseError) {
console.error(formattedError.message);
const formatError = (formattedError: any, error: any) => {
// Handle database errors (Sequelize BaseError).
if (error instanceof BaseError) {
console.error({
type: 'DatabaseError',
source: 'Sequelize',
message: formattedError.message,
internalMessage: error.message,
stack: error.stack,
path: formattedError.path,
extensions: formattedError.extensions,
});

return { message: 'Internal server error' };
}
return { message: 'Internal server error' };
}

return formattedError;
},
// Handle unexpected internal server errors.
if (
!formattedError.extensions?.code ||
formattedError.extensions.code === 'INTERNAL_SERVER_ERROR'
) {
console.error({
type: 'InternalServerError',
source: 'Generic',
message: formattedError.message,
internalMessage: error.originalError?.message,
stack: error.originalError?.stack,
path: formattedError.path,
extensions: formattedError.extensions,
});

return { message: 'Internal server error' };
}

// Log and return other GraphQL errors as-is.
console.error({
type: 'GraphQLError',
message: formattedError.message,
path: formattedError.path,
extensions: formattedError.extensions,
});

return formattedError;
};

const server = new ApolloServer<Context>({
formatError,
introspection: true,
validationRules: [depthLimit(appSettings.maxQueryDepth)],
typeDefs,
Expand Down

0 comments on commit ee1efce

Please sign in to comment.