fix(api): logs
This commit is contained in:
@@ -1,17 +1,23 @@
|
|||||||
import { SdkAuthError, validateSdkRequest } from '@/utils/auth';
|
import { SdkAuthError, validateSdkRequest } from '@/utils/auth';
|
||||||
|
import type { PostEventPayload, TrackHandlerPayload } from '@openpanel/sdk';
|
||||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
|
|
||||||
export async function clientHook(req: FastifyRequest, reply: FastifyReply) {
|
export async function clientHook(
|
||||||
|
req: FastifyRequest<{
|
||||||
|
Body: PostEventPayload | TrackHandlerPayload;
|
||||||
|
}>,
|
||||||
|
reply: FastifyReply,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const client = await validateSdkRequest(req);
|
const client = await validateSdkRequest(req);
|
||||||
req.client = client;
|
req.client = client;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof SdkAuthError) {
|
if (error instanceof SdkAuthError) {
|
||||||
req.log.warn(error, 'Invalid SDK request');
|
req.log.warn('Invalid SDK request', error);
|
||||||
return reply.status(401).send(error.message);
|
return reply.status(401).send(error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.log.error(error, 'Invalid SDK request');
|
req.log.error('Invalid SDK request', error);
|
||||||
return reply.status(500).send('Internal server error');
|
return reply.status(500).send('Internal server error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export class SdkAuthError extends Error {
|
|||||||
) {
|
) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'SdkAuthError';
|
this.name = 'SdkAuthError';
|
||||||
|
this.message = message;
|
||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user