"Reply was already" sent error even though the onSend hook isn't returning a reply
AnzeKop opened this issue · comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.25.2
Plugin version
No response
Node.js version
20
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
Latest
Description
"type": "FastifyError", "message": "Reply was already sent, did you forget to \"return reply\"
This error started happening when I introduced this "onSend" hook to track analytics at the end of the request
`
export async function incrementUsageLimit(
req: FastifyRequest,
reply: FastifyReply,
payload: any
): Promise<any | void> {
try {
const body = req.body as any;
const queryParams = req.query as any;
const portalId =
(req.method === "POST" && body.origin ? body.origin.portalId : queryParams.portalId) || "";
const isExecutionRun = req.method === "POST" && body.callbackId;
if (req.method === "POST" && !body.callbackId) {
console.log("No callbackId found in request body");
return;
}
const portal = await db.query.portals.findFirst({
where: eq(portals.portalId, parseInt(portalId, 10)),
});
const appPrefix = req.url.replace(/^\/|\/$/g, "").split("/");
const credentials = await getCredentialsForApp(appPrefix[0]);
if (!credentials || !portal) throw new Error("Credentials not found");
const junction = await fetchPortalAppJunction(portal.id, credentials.id);
const updateField = isExecutionRun ? "executionRuns" : "extensionRuns";
await db
.update(portalAppJunction)
.set({ [updateField]: sql`${sql.identifier(updateField)} + 1` })
.where(eq(portalAppJunction.id, junction.id));
if (isExecutionRun) {
const error = payload.outputFields?.errorCode ? payload.outputFields.error : null;
await db.insert(executionRunsHistory).values({
objectType: body.object.objectType,
objectId: body.object.objectId,
name: body.origin.actionDefinitionId,
outcome: error ? "FAIL" : "SUCCESS",
error: error,
portalAppJunctionId: junction.id,
timestamp: new Date(),
});
} else {
await db.insert(extensionRunsHistory).values({
objectType: queryParams.associatedObjectType,
objectId: queryParams.associatedObjectId,
madeBy: queryParams.userEmail,
madeById: queryParams.userId,
name: "crmCard",
outcome: "SUCCESS", // Assuming extension runs are always successful in this context
portalAppJunctionId: junction.id,
timestamp: new Date(),
});
}
return;
} catch (error: any) {
console.error(error);
return;
}
}
`
Tried everything, returning different stuff, rewriting... Nothing seems to fix this error, rewriting it just tells me it needs to be of type Buffer of string not object but I need it to be a object return, don't want to rewrite it just need to access the payload
Steps to Reproduce
Code block above
Expected Behavior
No response