Skip to content

Commit 99104d2

Browse files
authored
Merge pull request #1565 from constructive-io/devin/1785487887-server-error-registry-sweep
fix(server): server error-registry sweep — route graphile middleware errors through the helper
2 parents 7a37da1 + 0704c21 commit 99104d2

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

graphql/server/src/middleware/graphile.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './types'; // for Request type
22

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

1818
import { isGraphqlObservabilityEnabled } from '../diagnostics/observability';
1919
import { HandlerCreationError } from '../errors/api-errors';
20+
import { respondWithGraphQLError } from '../errors/graphql-response';
2021
import { AuthCookiePlugin } from '../plugins/auth-cookie-plugin';
2122
import type { DatabaseSettings } from '../types';
2223
import { observeGraphileBuild } from './observability/graphile-build-stats';
2324

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

27+
const isDev = (): boolean => getNodeEnv() === 'development';
28+
2629
/**
2730
* GraphQL framework protocol codes. These originate in the GraphQL/grafast
2831
* transport layer (not in constructive-db), so they are not Constructive domain
@@ -324,12 +327,17 @@ export const graphile = (opts: ConstructiveOptions): RequestHandler => {
324327
const api = req.api;
325328
if (!api) {
326329
log.error(`${label} Missing API info`);
327-
return res.status(500).send('Missing API info');
330+
respondWithGraphQLError(res, errors.INTERNAL_FAILURE({ details: 'Missing API info' }));
331+
return;
328332
}
329333
const key = req.svc_key;
330334
if (!key) {
331335
log.error(`${label} Missing service cache key`);
332-
return res.status(500).send('Missing service cache key');
336+
respondWithGraphQLError(
337+
res,
338+
errors.INTERNAL_FAILURE({ details: 'Missing service cache key' })
339+
);
340+
return;
333341
}
334342
const { dbname, anonRole, roleName, schema } = api;
335343
const schemaLabel = schema?.join(',') || 'unknown';
@@ -431,9 +439,13 @@ export const graphile = (opts: ConstructiveOptions): RequestHandler => {
431439
} catch (e: any) {
432440
log.error(`${label} PostGraphile middleware error`, e);
433441
if (!res.headersSent) {
434-
return res.status(500).json({
435-
error: { code: 'INTERNAL_ERROR', message: 'An unexpected error occurred' }
436-
});
442+
respondWithGraphQLError(
443+
res,
444+
errors.INTERNAL_FAILURE({
445+
details: isDev() ? e?.message ?? String(e) : 'An unexpected error occurred'
446+
})
447+
);
448+
return;
437449
}
438450
next(e);
439451
}

0 commit comments

Comments
 (0)