Skip to content

Commit

Permalink
Cleanup logs during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Dec 18, 2024
1 parent 3e1a901 commit 501269d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/backend/src/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function middleware(request: NextRequest) {
// CORS headers
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
"Access-Control-Max-Age": "86400", // 1 day (capped to lower values, eg. 10min, by some browsers)
"Access-Control-Allow-Headers": corsAllowedRequestHeaders.join(', '),
"Access-Control-Expose-Headers": corsAllowedResponseHeaders.join(', '),
},
Expand Down
17 changes: 9 additions & 8 deletions apps/backend/src/route-handlers/smart-route-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export function handleApiRequest(handler: (req: NextRequest, options: any, reque
headers: Object.fromEntries(req.headers),
});

// During development, don't trash the console with logs from E2E tests
const disableExtendedLogging = getNodeEnvironment().includes('dev') && !!req.headers.get("x-stack-development-disable-extended-logging");

let hasRequestFinished = false;
try {
// censor long query parameters because they might contain sensitive data
Expand All @@ -89,32 +92,30 @@ export function handleApiRequest(handler: (req: NextRequest, options: any, reque
}
});

console.log(`[API REQ] [${requestId}] ${req.method} ${censoredUrl}`);
if (!disableExtendedLogging) console.log(`[API REQ] [${requestId}] ${req.method} ${censoredUrl}`);
const timeStart = performance.now();
const res = await handler(req, options, requestId);
const time = (performance.now() - timeStart);
if ([301, 302].includes(res.status)) {
throw new StackAssertionError("HTTP status codes 301 and 302 should not be returned by our APIs because the behavior for non-GET methods is inconsistent across implementations. Use 303 (to rewrite method to GET) or 307/308 (to preserve the original method and data) instead.", { status: res.status, url: req.nextUrl, req, res });
}
console.log(`[ RES] [${requestId}] ${req.method} ${censoredUrl} (in ${time.toFixed(0)}ms)`);
if (!disableExtendedLogging) console.log(`[ RES] [${requestId}] ${req.method} ${censoredUrl} (in ${time.toFixed(0)}ms)`);
return res;
} catch (e) {
let statusError: StatusError;
try {
statusError = catchError(e);
} catch (e) {
console.log(`[ EXC] [${requestId}] ${req.method} ${req.url}: Non-error caught (such as a redirect), will be re-thrown. Digest: ${(e as any)?.digest}`);
if (!disableExtendedLogging) console.log(`[ EXC] [${requestId}] ${req.method} ${req.url}: Non-error caught (such as a redirect), will be re-thrown. Digest: ${(e as any)?.digest}`);
throw e;
}

console.log(`[ ERR] [${requestId}] ${req.method} ${req.url}: ${statusError.message}`);
if (!disableExtendedLogging) console.log(`[ ERR] [${requestId}] ${req.method} ${req.url}: ${statusError.message}`);

// if we're in prod, log some extra info for debugging
// probably won't need this in dev, and it only spams the console
if (getNodeEnvironment().includes('prod') && !commonErrors.some(e => statusError instanceof e)) {
if (!commonErrors.some(e => statusError instanceof e)) {
// HACK: Log a nicified version of the error instead of statusError to get around buggy Next.js pretty-printing
// https://www.reddit.com/r/nextjs/comments/1gkxdqe/comment/m19kxgn/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
console.debug(`For the error above with request ID ${requestId}, the full error is:`, errorToNiceString(statusError));
if (!disableExtendedLogging) console.debug(`For the error above with request ID ${requestId}, the full error is:`, errorToNiceString(statusError));
}

const res = await createResponse(req, requestId, {
Expand Down
1 change: 1 addition & 0 deletions apps/e2e/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export async function niceFetch(url: string | URL, options?: NiceRequestInit): P
...options,
headers: {
"x-stack-disable-artificial-development-delay": "yes",
"x-stack-development-disable-extended-logging": "yes",
...filterUndefined(options?.headers ?? {}),
},
});
Expand Down
1 change: 1 addition & 0 deletions apps/e2e/tests/snapshot-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const hideHeaders = [
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-expose-headers",
"access-control-max-age",
"cache-control",
"connection",
"content-security-policy",
Expand Down

0 comments on commit 501269d

Please sign in to comment.