Handle errors when calling listen and provide some log feedback

This commit is contained in:
Eric Allam
2023-02-07 14:40:55 +00:00
parent d1d34da9b2
commit 710bcc2c47
5 changed files with 74 additions and 1054 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Handle errors when calling listen and provide some log feedback
+41 -7
View File
@@ -13,10 +13,20 @@ export type LogLevel = (typeof logLevels)[number];
export class Logger {
#name: string;
#tags: string[];
readonly #level: number;
constructor(name: string, level: LogLevel = "disabled") {
this.#name = name;
constructor(name: string | string[], level: LogLevel = "log") {
if (typeof name === "string") {
this.#name = name;
this.#tags = [];
} else {
const [n, ...tags] = name;
this.#name = n;
this.#tags = tags;
}
this.#level = logLevels.indexOf(
(process.env.TRIGGER_LOG_LEVEL ?? level) as LogLevel
);
@@ -25,31 +35,55 @@ export class Logger {
log(...args: any[]) {
if (this.#level < 1) return;
console.log(`[${this.#name}] `, ...args);
console.log(`${this.#formatName()} `, ...[...args, ...this.#formatTags()]);
}
error(...args: any[]) {
if (this.#level < 2) return;
console.error(`[${formattedDateTime()}] [${this.#name}] `, ...args);
console.error(
`[${formattedDateTime()}] ${this.#formatName()} `,
...[...args, ...this.#formatTags()]
);
}
warn(...args: any[]) {
if (this.#level < 3) return;
console.warn(`[${formattedDateTime()}] [${this.#name}] `, ...args);
console.warn(
`[${formattedDateTime()}] ${this.#formatName()} `,
...[...args, ...this.#formatTags()]
);
}
info(...args: any[]) {
if (this.#level < 4) return;
console.info(`[${formattedDateTime()}] [${this.#name}] `, ...args);
console.info(
`[${formattedDateTime()}] ${this.#formatName()} `,
...[...args, ...this.#formatTags()]
);
}
debug(...args: any[]) {
if (this.#level < 5) return;
console.debug(`[${formattedDateTime()}] [${this.#name}] `, ...args);
console.debug(
`[${formattedDateTime()}] ${this.#formatName()} `,
...[...args, ...this.#formatTags()]
);
}
#formatName() {
if (Array.isArray(this.#name)) {
return this.#name.map((name) => `[${name}]`).join("");
}
return `[${this.#name}]`;
}
#formatTags() {
return this.#tags.map((tag) => `[${tag}]`);
}
}
+16 -9
View File
@@ -85,13 +85,24 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
this.#apiKey = apiKey;
this.#endpoint = this.#options.endpoint ?? "wss://wss.trigger.dev/ws";
this.#logger = new Logger("trigger.dev", this.#options.logLevel);
this.#logger = new Logger(
["trigger.dev", this.#options.id],
this.#options.logLevel
);
}
async listen(instanceId?: string) {
await this.#initializeConnection(instanceId);
this.#initializeRPC();
this.#initializeHost();
try {
await this.#initializeConnection(instanceId);
this.#initializeRPC();
await this.#initializeHost();
this.#logger.log(`✨ Connected and listening for events`);
} catch (error) {
this.#logger.log(`🚩 Could not connect to trigger.dev`);
this.close();
}
}
close() {
@@ -580,11 +591,7 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
return this.#trigger.options
.run(eventData, ctx)
.then((output) => {
this.#logger.log(
`Completed workflow '${this.#options.name}', run ${
data.id
} 🏃`
);
this.#logger.log(`Run ${data.id} complete 🏃`);
return serverRPC.send("COMPLETE_WORKFLOW_RUN", {
runId: data.id,
+10 -1037
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -124,6 +124,7 @@
"DEBUG",
"TRIGGER_LOG_LEVEL",
"TRIGGER_API_KEY",
"TRIGGER_API_URL"
"TRIGGER_API_URL",
"APP_ENV"
]
}