OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
improve error message when client validation fails
Carl-Gerhard Lindesvärd28d95da56b65
diff --git a/apps/api/src/routes/event.router.ts b/apps/api/src/routes/event.router.tsindex ed23ecf90..043525c09 100644--- a/apps/api/src/routes/event.router.ts+++ b/apps/api/src/routes/event.router.ts@@ -18,13 +18,15 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {) => {try {const client = await validateSdkRequest(req.headers).catch((error) => {- if (!(error instanceof SdkAuthError)) {- logger.error(error, 'Failed to validate sdk request');+ if (error instanceof SdkAuthError) {+ return reply.status(401).send(error.message);}- return null;+ logger.error(error, 'Failed to validate sdk request');+ return reply.status(401).send('Unknown validation error');});+if (!client?.projectId) {- return reply.status(401).send();+ return reply.status(401).send('No project found for this client');}req.projectId = client.projectId;req.client = client;diff --git a/apps/api/src/routes/profile.router.ts b/apps/api/src/routes/profile.router.tsindex 891ddeaf0..b0c534c8f 100644--- a/apps/api/src/routes/profile.router.ts+++ b/apps/api/src/routes/profile.router.ts@@ -8,13 +8,15 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {fastify.addHook('preHandler', async (req, reply) => {try {const client = await validateSdkRequest(req.headers).catch((error) => {- if (!(error instanceof SdkAuthError)) {- logger.error(error, 'Failed to validate sdk request');+ if (error instanceof SdkAuthError) {+ return reply.status(401).send(error.message);}- return null;+ logger.error(error, 'Failed to validate sdk request');+ return reply.status(401).send('Unknown validation error');});+if (!client?.projectId) {- return reply.status(401).send();+ return reply.status(401).send('No project found for this client');}req.projectId = client.projectId;req.client = client;diff --git a/apps/api/src/routes/track.router.ts b/apps/api/src/routes/track.router.tsindex 3cedf61ea..8849b35e7 100644--- a/apps/api/src/routes/track.router.ts+++ b/apps/api/src/routes/track.router.ts@@ -18,14 +18,15 @@ const trackRouter: FastifyPluginCallback = (fastify, opts, done) => {) => {try {const client = await validateSdkRequest(req.headers).catch((error) => {- if (!(error instanceof SdkAuthError)) {- logger.error(error, 'Failed to validate sdk request');+ if (error instanceof SdkAuthError) {+ return reply.status(401).send(error.message);}- return null;+ logger.error(error, 'Failed to validate sdk request');+ return reply.status(401).send('Unknown validation error');});if (!client?.projectId) {- return reply.status(401).send();+ return reply.status(401).send('No project found for this client');}req.projectId = client.projectId;