improve error message when client validation fails

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-02 21:05:18 +02:00
parent 6ed8d3a543
commit 28d95da56b
3 changed files with 17 additions and 12 deletions

View File

@@ -18,13 +18,15 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
) => { ) => {
try { try {
const client = await validateSdkRequest(req.headers).catch((error) => { const client = await validateSdkRequest(req.headers).catch((error) => {
if (!(error instanceof SdkAuthError)) { if (error instanceof SdkAuthError) {
logger.error(error, 'Failed to validate sdk request'); 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) { if (!client?.projectId) {
return reply.status(401).send(); return reply.status(401).send('No project found for this client');
} }
req.projectId = client.projectId; req.projectId = client.projectId;
req.client = client; req.client = client;

View File

@@ -8,13 +8,15 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook('preHandler', async (req, reply) => { fastify.addHook('preHandler', async (req, reply) => {
try { try {
const client = await validateSdkRequest(req.headers).catch((error) => { const client = await validateSdkRequest(req.headers).catch((error) => {
if (!(error instanceof SdkAuthError)) { if (error instanceof SdkAuthError) {
logger.error(error, 'Failed to validate sdk request'); 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) { if (!client?.projectId) {
return reply.status(401).send(); return reply.status(401).send('No project found for this client');
} }
req.projectId = client.projectId; req.projectId = client.projectId;
req.client = client; req.client = client;

View File

@@ -18,14 +18,15 @@ const trackRouter: FastifyPluginCallback = (fastify, opts, done) => {
) => { ) => {
try { try {
const client = await validateSdkRequest(req.headers).catch((error) => { const client = await validateSdkRequest(req.headers).catch((error) => {
if (!(error instanceof SdkAuthError)) { if (error instanceof SdkAuthError) {
logger.error(error, 'Failed to validate sdk request'); 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) { if (!client?.projectId) {
return reply.status(401).send(); return reply.status(401).send('No project found for this client');
} }
req.projectId = client.projectId; req.projectId = client.projectId;