Skip to content
Merged
Changes from all 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
24 changes: 18 additions & 6 deletions graphql/server/src/middleware/graphile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './types'; // for Request type

import crypto from 'node:crypto';
import { classify, type ErrorContext, parse } from '@constructive-io/errors';
import { classify, errors, type ErrorContext, parse } from '@constructive-io/errors';
import type { ComputeConfig } from '@constructive-io/express-context';
import type { ConstructiveOptions } from '@constructive-io/graphql-types';
import { getNodeEnv } from '@pgpmjs/env';
Expand All @@ -17,12 +17,15 @@ import { getPgEnvOptions } from 'pg-env';

import { isGraphqlObservabilityEnabled } from '../diagnostics/observability';
import { HandlerCreationError } from '../errors/api-errors';
import { respondWithGraphQLError } from '../errors/graphql-response';
import { AuthCookiePlugin } from '../plugins/auth-cookie-plugin';
import type { DatabaseSettings } from '../types';
import { observeGraphileBuild } from './observability/graphile-build-stats';

const maskErrorLog = new Logger('graphile:maskError');

const isDev = (): boolean => getNodeEnv() === 'development';

/**
* GraphQL framework protocol codes. These originate in the GraphQL/grafast
* transport layer (not in constructive-db), so they are not Constructive domain
Expand Down Expand Up @@ -324,12 +327,17 @@ export const graphile = (opts: ConstructiveOptions): RequestHandler => {
const api = req.api;
if (!api) {
log.error(`${label} Missing API info`);
return res.status(500).send('Missing API info');
respondWithGraphQLError(res, errors.INTERNAL_FAILURE({ details: 'Missing API info' }));
return;
}
const key = req.svc_key;
if (!key) {
log.error(`${label} Missing service cache key`);
return res.status(500).send('Missing service cache key');
respondWithGraphQLError(
res,
errors.INTERNAL_FAILURE({ details: 'Missing service cache key' })
);
return;
}
const { dbname, anonRole, roleName, schema } = api;
const schemaLabel = schema?.join(',') || 'unknown';
Expand Down Expand Up @@ -431,9 +439,13 @@ export const graphile = (opts: ConstructiveOptions): RequestHandler => {
} catch (e: any) {
log.error(`${label} PostGraphile middleware error`, e);
if (!res.headersSent) {
return res.status(500).json({
error: { code: 'INTERNAL_ERROR', message: 'An unexpected error occurred' }
});
respondWithGraphQLError(
res,
errors.INTERNAL_FAILURE({
details: isDev() ? e?.message ?? String(e) : 'An unexpected error occurred'
})
);
return;
}
next(e);
}
Expand Down
Loading