Our audit logger is allowed to fail, and in production it fails without a sound
Our privacy policy says we "maintain a GDPR audit trail of privacy-related actions on your account". The code behind that sentence is one function, logAuditAction , and the most important thing about it is in its catch block. The shape export async function logAuditAction ( params : AuditLogParams ) : Promise < void > { try { await db . insert ( auditLogsTable ) . values ( { . . . } ) ; } catch (…
The privacy policy of CogniPrep states that they maintain a GDPR audit trail of privacy-related actions on user accounts. The code responsible for this functionality is a single function called logAuditAction. The most critical aspect of this function is its catch block, which logs errors but does not fail the main operation if the error occurs.
This decision is justified, as the audit trail is a record of the action, not part of the action itself. There are 14 call sites across the app, all of which await the logAuditAction function and continue executing even if the audit log fails to write.
In development mode, the error is printed, but in production, where the database table is crucial, a failed insert leaves no trace. There are no log lines, Sentry events, or counters. The function returns undefined in both cases, making it impossible for the caller to distinguish between a successful write and a failed one. The rest of the codebase has a similar tool called logError, which always prints errors. However, the audit logger does not use this tool.
In practice, this means that if the database table is unreachable for an hour, an hour of consent changes and deletions will have no records, and there will be no evidence of this in monitoring. If a schema change breaks the insert, every audit write will fail from that deployment onward, appearing identical to a quiet day. The only evidence would be a gap in the audit trail, which may not be discovered for months.
The privacy policy promises a trail, but a trail that can silently stop is a weaker promise than the one written. The solution is a one-line change that reports the failure and swallows it, keeping the main operation from failing due to the log. The catch block should be modified to use the logError function, reporting the failure with the action name, resource type, and error.
This maintains the property of ensuring the main operation never fails because of the log and makes failures visible in the error reporting system. An alternative option is to have the function return a boolean or throw an error for operations where the record is essential, allowing the caller to decide how to handle the failure.
Most of the 14 call sites currently use a fire-and-forget approach, but a few may require a different handling method.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to