11import './types' ; // for Request type
22
33import 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' ;
55import type { ComputeConfig } from '@constructive-io/express-context' ;
66import type { ConstructiveOptions } from '@constructive-io/graphql-types' ;
77import { getNodeEnv } from '@pgpmjs/env' ;
@@ -17,12 +17,15 @@ import { getPgEnvOptions } from 'pg-env';
1717
1818import { isGraphqlObservabilityEnabled } from '../diagnostics/observability' ;
1919import { HandlerCreationError } from '../errors/api-errors' ;
20+ import { respondWithGraphQLError } from '../errors/graphql-response' ;
2021import { AuthCookiePlugin } from '../plugins/auth-cookie-plugin' ;
2122import type { DatabaseSettings } from '../types' ;
2223import { observeGraphileBuild } from './observability/graphile-build-stats' ;
2324
2425const 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