Skip to content

Commit

Permalink
Merge pull request #2823 from Infisical/daniel/consolidate-request-ids
Browse files Browse the repository at this point in the history
fix: consolidate reqId and requestId fields
  • Loading branch information
akhilmhdh authored Dec 6, 2024
2 parents f80023f + 623a99b commit d3d30eb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion backend/src/@types/fastify-request-context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import "@fastify/request-context";

declare module "@fastify/request-context" {
interface RequestContextData {
requestId: string;
reqId: string;
}
}
12 changes: 6 additions & 6 deletions backend/src/lib/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const redactedKeys = [

const UNKNOWN_REQUEST_ID = "UNKNOWN_REQUEST_ID";

const extractRequestId = () => {
const extractReqId = () => {
try {
return requestContext.get("requestId") || UNKNOWN_REQUEST_ID;
return requestContext.get("reqId") || UNKNOWN_REQUEST_ID;
} catch (err) {
console.log("failed to get request context", err);
return UNKNOWN_REQUEST_ID;
Expand Down Expand Up @@ -133,22 +133,22 @@ export const initLogger = async () => {
const wrapLogger = (originalLogger: Logger): CustomLogger => {
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-explicit-any
originalLogger.info = (obj: unknown, msg?: string, ...args: any[]) => {
return originalLogger.child({ requestId: extractRequestId() }).info(obj, msg, ...args);
return originalLogger.child({ reqId: extractReqId() }).info(obj, msg, ...args);
};

// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-explicit-any
originalLogger.error = (obj: unknown, msg?: string, ...args: any[]) => {
return originalLogger.child({ requestId: extractRequestId() }).error(obj, msg, ...args);
return originalLogger.child({ reqId: extractReqId() }).error(obj, msg, ...args);
};

// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-explicit-any
originalLogger.warn = (obj: unknown, msg?: string, ...args: any[]) => {
return originalLogger.child({ requestId: extractRequestId() }).warn(obj, msg, ...args);
return originalLogger.child({ reqId: extractReqId() }).warn(obj, msg, ...args);
};

// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-explicit-any
originalLogger.debug = (obj: unknown, msg?: string, ...args: any[]) => {
return originalLogger.child({ requestId: extractRequestId() }).debug(obj, msg, ...args);
return originalLogger.child({ reqId: extractReqId() }).debug(obj, msg, ...args);
};

return originalLogger;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ export const main = async ({ db, hsmModule, auditLogDb, smtp, logger, queue, key
await server.register(maintenanceMode);

await server.register(fastifyRequestContext, {
defaultStoreValues: (request) => ({
requestId: request.id,
log: request.log.child({ requestId: request.id })
defaultStoreValues: (req) => ({
reqId: req.id,
log: req.log.child({ reqId: req.id })
})
});

Expand Down
26 changes: 13 additions & 13 deletions backend/src/server/plugins/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,42 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
if (error instanceof BadRequestError) {
void res
.status(HttpStatusCodes.BadRequest)
.send({ requestId: req.id, statusCode: HttpStatusCodes.BadRequest, message: error.message, error: error.name });
.send({ reqId: req.id, statusCode: HttpStatusCodes.BadRequest, message: error.message, error: error.name });
} else if (error instanceof NotFoundError) {
void res
.status(HttpStatusCodes.NotFound)
.send({ requestId: req.id, statusCode: HttpStatusCodes.NotFound, message: error.message, error: error.name });
.send({ reqId: req.id, statusCode: HttpStatusCodes.NotFound, message: error.message, error: error.name });
} else if (error instanceof UnauthorizedError) {
void res.status(HttpStatusCodes.Unauthorized).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.Unauthorized,
message: error.message,
error: error.name
});
} else if (error instanceof DatabaseError || error instanceof InternalServerError) {
void res.status(HttpStatusCodes.InternalServerError).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.InternalServerError,
message: "Something went wrong",
error: error.name
});
} else if (error instanceof GatewayTimeoutError) {
void res.status(HttpStatusCodes.GatewayTimeout).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.GatewayTimeout,
message: error.message,
error: error.name
});
} else if (error instanceof ZodError) {
void res.status(HttpStatusCodes.UnprocessableContent).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.UnprocessableContent,
error: "ValidationFailure",
message: error.issues
});
} else if (error instanceof ForbiddenError) {
void res.status(HttpStatusCodes.Forbidden).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.Forbidden,
error: "PermissionDenied",
message: `You are not allowed to ${error.action} on ${error.subjectType}`,
Expand All @@ -88,28 +88,28 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
});
} else if (error instanceof ForbiddenRequestError) {
void res.status(HttpStatusCodes.Forbidden).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.Forbidden,
message: error.message,
error: error.name
});
} else if (error instanceof RateLimitError) {
void res.status(HttpStatusCodes.TooManyRequests).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.TooManyRequests,
message: error.message,
error: error.name
});
} else if (error instanceof ScimRequestError) {
void res.status(error.status).send({
requestId: req.id,
reqId: req.id,
schemas: error.schemas,
status: error.status,
detail: error.detail
});
} else if (error instanceof OidcAuthError) {
void res.status(HttpStatusCodes.InternalServerError).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.InternalServerError,
message: error.message,
error: error.name
Expand All @@ -128,14 +128,14 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
}

void res.status(HttpStatusCodes.Forbidden).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.Forbidden,
error: "TokenError",
message: errorMessage
});
} else {
void res.status(HttpStatusCodes.InternalServerError).send({
requestId: req.id,
reqId: req.id,
statusCode: HttpStatusCodes.InternalServerError,
error: "InternalServerError",
message: "Something went wrong"
Expand Down
12 changes: 6 additions & 6 deletions backend/src/server/routes/sanitizedSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@ export const integrationAuthPubSchema = IntegrationAuthsSchema.pick({

export const DefaultResponseErrorsSchema = {
400: z.object({
requestId: z.string(),
reqId: z.string(),
statusCode: z.literal(400),
message: z.string(),
error: z.string()
}),
404: z.object({
requestId: z.string(),
reqId: z.string(),
statusCode: z.literal(404),
message: z.string(),
error: z.string()
}),
401: z.object({
requestId: z.string(),
reqId: z.string(),
statusCode: z.literal(401),
message: z.string(),
error: z.string()
}),
403: z.object({
requestId: z.string(),
reqId: z.string(),
statusCode: z.literal(403),
message: z.string(),
details: z.any().optional(),
error: z.string()
}),
// Zod errors return a message of varying shapes and sizes, so z.any() is used here
422: z.object({
requestId: z.string(),
reqId: z.string(),
statusCode: z.literal(422),
message: z.any(),
error: z.string()
}),
500: z.object({
requestId: z.string(),
reqId: z.string(),
statusCode: z.literal(500),
message: z.string(),
error: z.string()
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/hooks/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,26 @@ export enum ApiErrorTypes {

export type TApiErrors =
| {
requestId: string;
reqId: string;
error: ApiErrorTypes.ValidationError;
message: ZodIssue[];
statusCode: 422;
}
| {
requestId: string;
reqId: string;
error: ApiErrorTypes.UnauthorizedError;
message: string;
statusCode: 401;
}
| {
requestId: string;
reqId: string;
error: ApiErrorTypes.ForbiddenError;
message: string;
details: PureAbility["rules"];
statusCode: 403;
}
| {
requestId: string;
reqId: string;
statusCode: 400;
message: string;
error: ApiErrorTypes.BadRequestError;
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/reactQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export const queryClient = new QueryClient({
),
copyActions: [
{
value: serverResponse.requestId,
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.requestId}`
label: `Request ID: ${serverResponse.reqId}`
}
]
},
Expand Down Expand Up @@ -169,9 +169,9 @@ export const queryClient = new QueryClient({
) : undefined,
copyActions: [
{
value: serverResponse.requestId,
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.requestId}`
label: `Request ID: ${serverResponse.reqId}`
}
]
},
Expand All @@ -185,9 +185,9 @@ export const queryClient = new QueryClient({
text: `${serverResponse.message}.`,
copyActions: [
{
value: serverResponse.requestId,
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.requestId}`
label: `Request ID: ${serverResponse.reqId}`
}
]
});
Expand Down

0 comments on commit d3d30eb

Please sign in to comment.