Skip to content

Commit f58cd23

Browse files
authored
fix(logging): use object cause in TRPCErrors so event name is logged correctly (#749)
1 parent 5382f6b commit f58cd23

7 files changed

Lines changed: 20 additions & 13 deletions

File tree

app/backend/src/acceptVisualChanges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ export const acceptVisualChanges = async (
3232
throw new TRPCError({
3333
code: 'FORBIDDEN',
3434
message: reasonToPreventUpdate,
35-
cause: 'VISUAL_CHANGE_ACCEPTANCE_BLOCKED'
35+
cause: { event: 'VISUAL_CHANGE_ACCEPTANCE_BLOCKED' }
3636
});
3737
}
3838
const hash = commitHash ?? diffId;
3939
if (!hash) {
4040
throw new TRPCError({
4141
code: 'BAD_REQUEST',
4242
message: 'Please provide either a commitHash or a diffId.',
43-
cause: 'MISSING_IDENTIFIER'
43+
cause: { event: 'MISSING_IDENTIFIER' }
4444
});
4545
}
4646

app/backend/src/fetchCurrentPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const fetchCurrentPage = async ({
1515
throw new TRPCError({
1616
code: 'NOT_FOUND',
1717
message: `Page ${page} does not exist. Only ${paginatedKeys.length} pages were found.`,
18-
cause: 'PAGE_NOT_FOUND'
18+
cause: { event: 'PAGE_NOT_FOUND' }
1919
});
2020
}
2121
const { keys, title } = currentPage;
@@ -26,7 +26,7 @@ export const fetchCurrentPage = async ({
2626
throw new TRPCError({
2727
code: 'BAD_REQUEST',
2828
message: `Invalid file name: ${key}. ${result.error.message}`,
29-
cause: 'INVALID_FILE_NAME'
29+
cause: { event: 'INVALID_FILE_NAME' }
3030
});
3131
}
3232

app/backend/src/getGroupedKeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const getGroupedKeys = async (hash: string, bucket: string) => {
1111
code: 'NOT_FOUND',
1212
message:
1313
'The commit hash was not associated with any visual regression test failures.',
14-
cause: 'COMMIT_HASH_NOT_FOUND'
14+
cause: { event: 'COMMIT_HASH_NOT_FOUND' }
1515
});
1616
}
1717

@@ -36,7 +36,7 @@ export const getGroupedKeys = async (hash: string, bucket: string) => {
3636
code: 'NOT_FOUND',
3737
message:
3838
'There was no new or diff images associated with the commit hash.\nThis might be because the tests failed before a screenshot could be taken and it could be compared to the base.',
39-
cause: 'NO_NEW_OR_DIFF_IMAGES'
39+
cause: { event: 'NO_NEW_OR_DIFF_IMAGES' }
4040
});
4141
}
4242

app/backend/src/getOctokit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const getOctokit = (owner: string, repo: string) => {
99
throw new TRPCError({
1010
code: 'UNAUTHORIZED',
1111
message: 'Missing secrets.json file',
12-
cause: 'MISSING_SECRETS_FILE'
12+
cause: { event: 'MISSING_SECRETS_FILE' }
1313
});
1414
}
1515
const parsedJson = JSON.parse(readFileSync(secretsFilePath).toString());
@@ -18,7 +18,7 @@ export const getOctokit = (owner: string, repo: string) => {
1818
throw new TRPCError({
1919
code: 'INTERNAL_SERVER_ERROR',
2020
message: `Failed to parse secrets.json: ${result.error}`,
21-
cause: 'SECRETS_PARSE_ERROR'
21+
cause: { event: 'SECRETS_PARSE_ERROR' }
2222
});
2323
}
2424
const repoSecrets = result.data[`${owner}/${repo}`];
@@ -27,7 +27,7 @@ export const getOctokit = (owner: string, repo: string) => {
2727
throw new TRPCError({
2828
code: 'UNAUTHORIZED',
2929
message: `Missing githubToken for repo ${owner}/${repo}`,
30-
cause: 'MISSING_GITHUB_TOKEN'
30+
cause: { event: 'MISSING_GITHUB_TOKEN' }
3131
});
3232
}
3333
return new Octokit({

app/backend/src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const logEvent = (
22
level: 'INFO' | 'WARN' | 'ERROR',
3-
payload: Record<string, unknown>
3+
payload: { event: string } & Record<string, unknown>
44
) => {
55
// eslint-disable-next-line no-console
66
console.log(

app/backend/src/updateCommitStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const updateCommitStatus = async ({
2222
throw new TRPCError({
2323
code: 'INTERNAL_SERVER_ERROR',
2424
message: `Failed to update GitHub commit status: ${error}`,
25-
cause: 'COMMIT_STATUS_UPDATE_FAILED'
25+
cause: { event: 'COMMIT_STATUS_UPDATE_FAILED' }
2626
});
2727
});
2828
};

app/server.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ const trpcHandler = createBunHttpHandler({
1414
endpoint: '/trpc',
1515
createContext,
1616
onError: ({ error: { code, message, cause }, path, ctx }) => {
17-
logEvent('ERROR', { path, code, message, cause, ...ctx?.urlParams });
17+
const causeInfo =
18+
cause && 'event' in cause && typeof cause.event === 'string'
19+
? { event: cause.event }
20+
: { event: 'UNKNOWN', cause: cause?.message };
21+
logEvent('ERROR', { path, code, message, ...causeInfo, ...ctx?.urlParams });
1822
}
1923
});
2024

@@ -39,4 +43,7 @@ const server = serve({
3943
}
4044
});
4145

42-
logEvent('INFO', { message: `Server running at ${server.url}` });
46+
logEvent('INFO', {
47+
event: 'STARTUP',
48+
message: `Server running at ${server.url}`
49+
});

0 commit comments

Comments
 (0)