diff --git a/src/server.ts b/src/server.ts index 8be380c..4de11c9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -50,16 +50,53 @@ export interface Context { const app = express(); const httpServer = http.createServer(app); -const server = new ApolloServer({ - 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({ + formatError, introspection: true, validationRules: [depthLimit(appSettings.maxQueryDepth)], typeDefs,