Skip to content

Commit

Permalink
Add enhanced uncaught error logging, allow internal networking for AP…
Browse files Browse the repository at this point in the history
…I communication (#1385)

* log uncaught errors to console

* add optional CODEGEN_GQL_URL var, use in gql codegen

* make health endpoint check API connection

* use relative GQL path in dripsQL

* pass new arg through dockerfile

* create valid URL for GraphQLClient

* fix check
  • Loading branch information
efstajas authored Dec 9, 2024
1 parent 2f3cdb6 commit a2a258d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ETHERSCAN_API_KEY=string
# URL of the Drips GraphQL API deployment to use. Set to the defaults below to use public Sepolia deployment.
GQL_URL=string # https://drips-api-sepolia-testnet.up.railway.app/
GQL_ACCESS_TOKEN=string # afdb8b7e-8fa7-4de9-bd95-b650b839e745
CODEGEN_GQL_URL=string # optional, set a different GQL URL for the GQL code generator build step. Defaults to GQL_URL

# Redis cache for caching various response such as GitHub. If undefined, caching is disabled.
CACHE_REDIS_CONNECTION_STRING=string
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ARG ETHERSCAN_API_KEY

ARG GQL_URL
ARG GQL_ACCESS_TOKEN
ARG CODEGEN_GQL_URL

ARG CACHE_REDIS_CONNECTION_STRING

Expand Down
2 changes: 1 addition & 1 deletion codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const config: CodegenConfig = {
},
schema: [
{
[process.env.GQL_URL]: {
[process.env.CODEGEN_GQL_URL ?? process.env.GQL_URL]: {
headers: {
Authorization: `Bearer ${process.env.GQL_ACCESS_TOKEN}`,
},
Expand Down
7 changes: 5 additions & 2 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sequence } from '@sveltejs/kit/hooks';
import { handleErrorWithSentry, sentryHandle } from '@sentry/sveltekit';
import { sentryHandle } from '@sentry/sveltekit';
import * as Sentry from '@sentry/sveltekit';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import getOptionalEnvVar from '$lib/utils/get-optional-env-var/public';
Expand All @@ -26,4 +26,7 @@ PuppeteerManager.launch({
});

export const handle = sequence(sentryHandle());
export const handleError = handleErrorWithSentry();
export const handleError = Sentry.handleErrorWithSentry(function (error: unknown) {
// eslint-disable-next-line no-console
console.error('Uncaught error', error);
});
11 changes: 10 additions & 1 deletion src/lib/graphql/dripsQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { parse } from 'graphql';
import { GraphQLClient, type RequestDocument, type Variables } from 'graphql-request';
import { addTypenameToDocument } from '@apollo/client/utilities';
import { BASE_URL } from '$lib/utils/base-url';
import { browser, dev } from '$app/environment';

export default async function query<TResponse, TVariables extends Variables = Variables>(
query: RequestDocument,
variables?: TVariables,
customFetch: typeof fetch = fetch,
): Promise<TResponse> {
const client = new GraphQLClient(`${BASE_URL}/api/gql`, {
// We proxy client-side requests through an endpoint on this sveltekit app in order to
// inject the Authorization header.
// If we're currently in a browser or dev mode, we use the app's base URL to construct the endpoint.
// If we're on the server in prod, we use localhost in order for traffic to stay within the container
// and avoid network overhead.
// IMPORTANT: This assumes the app is running on port 8080 in the container, which should usually be the case.
const endpointLocation = browser || dev ? `${BASE_URL}/api/gql` : 'http://localhost:8080/api/gql';

const client = new GraphQLClient(endpointLocation, {
fetch: customFetch,
});

Expand Down
20 changes: 19 additions & 1 deletion src/routes/api/health/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import query from '$lib/graphql/dripsQL';
import { error } from '@sveltejs/kit';

export const GET = async () => {
// simple ping endpoint for downtime-less deploys. railway will hit this and wait for 200
// before directing traffic to a newly built container
// checks whether the API can be reached. This is important because private IPV6 networking
// in Railway needs a few seconds to start up after a deploy

const testQuery = `
query Test {
__typename
}
`;

return new Response('OK');
try {
await query(testQuery);
return new Response('OK');
} catch (e) {
// eslint-disable-next-line no-console
console.error('Health endpoint error', e);
return error(500);
}
};

0 comments on commit a2a258d

Please sign in to comment.