Skip to content

Commit d72edfb

Browse files
authored
Log metadata in worker errors (#943)
* export ErrorWithContext * log error metadata if present
1 parent 135a84e commit d72edfb

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

background-charm-service/src/worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type Charm, CharmManager } from "@commontools/charm";
22
import {
33
Cell,
44
idle,
5+
isErrorWithContext,
56
isStream,
67
onError,
78
setBobbyServerUrl,
@@ -144,7 +145,9 @@ async function runCharm(data: { charmId: string }) {
144145
console.log(`Worker: Successfully executed charm ${spaceId}/${charmId}`);
145146
return { success: true, charmId };
146147
} catch (error) {
147-
const errorMessage = error instanceof Error ? error.message : String(error);
148+
const errorMessage = isErrorWithContext(error)
149+
? `${error.message} @ ${error.space}:${error.charmId} running ${error.recipeId}`
150+
: String(error);
148151
console.error(
149152
`Worker error executing charm ${spaceId}/${charmId}: ${errorMessage}`,
150153
);

runner/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export { run, stop } from "./runner.ts";
22
export { addModuleByRef, raw } from "./module.ts";
33
export {
4-
type Action,
54
idle,
5+
isErrorWithContext,
66
onError,
77
run as addAction,
88
unschedule as removeAction,
@@ -11,7 +11,7 @@ export { getRecipeEnvironment, setRecipeEnvironment } from "./env.ts";
1111
export type { DocImpl } from "./doc.ts";
1212
export type { Cell, CellLink, Stream } from "./cell.ts";
1313
export type { QueryResult } from "./query-result-proxy.ts";
14-
export type { ReactivityLog } from "./scheduler.ts";
14+
export type { Action, ErrorWithContext, ReactivityLog } from "./scheduler.ts";
1515
export * as StorageInspector from "./storage/inspector.ts";
1616
export { getDoc, isDoc } from "./doc.ts";
1717
export {
@@ -59,8 +59,8 @@ export {
5959
setBlobbyServerUrl,
6060
} from "./blobby-storage.ts";
6161
export { tsToExports } from "./local-build.ts";
62-
export {
62+
export {
6363
addCommonIDfromObjectID,
6464
followAliases,
65-
maybeGetCellLink
65+
maybeGetCellLink,
6666
} from "./utils.ts";

runner/src/scheduler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ export type ErrorWithContext = Error & {
169169
recipeId: string;
170170
};
171171

172+
export function isErrorWithContext(error: unknown): error is ErrorWithContext {
173+
return error instanceof Error && "action" in error && "charmId" in error &&
174+
"space" in error && "recipeId" in error;
175+
}
176+
172177
function handleError(error: Error, action: any) {
173178
// TODO(seefeld): This is a rather hacky way to get the context, based on the
174179
// unsafe_binding pattern. Once we replace that mechanism, let's add nicer

0 commit comments

Comments
 (0)