Handle errors when calling listen and provide some log feedback
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@trigger.dev/sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Handle errors when calling listen and provide some log feedback
|
||||||
@@ -13,10 +13,20 @@ export type LogLevel = (typeof logLevels)[number];
|
|||||||
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
#name: string;
|
#name: string;
|
||||||
|
#tags: string[];
|
||||||
readonly #level: number;
|
readonly #level: number;
|
||||||
|
|
||||||
constructor(name: string, level: LogLevel = "disabled") {
|
constructor(name: string | string[], level: LogLevel = "log") {
|
||||||
this.#name = name;
|
if (typeof name === "string") {
|
||||||
|
this.#name = name;
|
||||||
|
this.#tags = [];
|
||||||
|
} else {
|
||||||
|
const [n, ...tags] = name;
|
||||||
|
|
||||||
|
this.#name = n;
|
||||||
|
this.#tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
this.#level = logLevels.indexOf(
|
this.#level = logLevels.indexOf(
|
||||||
(process.env.TRIGGER_LOG_LEVEL ?? level) as LogLevel
|
(process.env.TRIGGER_LOG_LEVEL ?? level) as LogLevel
|
||||||
);
|
);
|
||||||
@@ -25,31 +35,55 @@ export class Logger {
|
|||||||
log(...args: any[]) {
|
log(...args: any[]) {
|
||||||
if (this.#level < 1) return;
|
if (this.#level < 1) return;
|
||||||
|
|
||||||
console.log(`[${this.#name}] `, ...args);
|
console.log(`${this.#formatName()} `, ...[...args, ...this.#formatTags()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
error(...args: any[]) {
|
error(...args: any[]) {
|
||||||
if (this.#level < 2) return;
|
if (this.#level < 2) return;
|
||||||
|
|
||||||
console.error(`[${formattedDateTime()}] [${this.#name}] `, ...args);
|
console.error(
|
||||||
|
`[${formattedDateTime()}] ${this.#formatName()} `,
|
||||||
|
...[...args, ...this.#formatTags()]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
warn(...args: any[]) {
|
warn(...args: any[]) {
|
||||||
if (this.#level < 3) return;
|
if (this.#level < 3) return;
|
||||||
|
|
||||||
console.warn(`[${formattedDateTime()}] [${this.#name}] `, ...args);
|
console.warn(
|
||||||
|
`[${formattedDateTime()}] ${this.#formatName()} `,
|
||||||
|
...[...args, ...this.#formatTags()]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
info(...args: any[]) {
|
info(...args: any[]) {
|
||||||
if (this.#level < 4) return;
|
if (this.#level < 4) return;
|
||||||
|
|
||||||
console.info(`[${formattedDateTime()}] [${this.#name}] `, ...args);
|
console.info(
|
||||||
|
`[${formattedDateTime()}] ${this.#formatName()} `,
|
||||||
|
...[...args, ...this.#formatTags()]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(...args: any[]) {
|
debug(...args: any[]) {
|
||||||
if (this.#level < 5) return;
|
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}]`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,13 +85,24 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
|
|||||||
|
|
||||||
this.#apiKey = apiKey;
|
this.#apiKey = apiKey;
|
||||||
this.#endpoint = this.#options.endpoint ?? "wss://wss.trigger.dev/ws";
|
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) {
|
async listen(instanceId?: string) {
|
||||||
await this.#initializeConnection(instanceId);
|
try {
|
||||||
this.#initializeRPC();
|
await this.#initializeConnection(instanceId);
|
||||||
this.#initializeHost();
|
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() {
|
close() {
|
||||||
@@ -580,11 +591,7 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
|
|||||||
return this.#trigger.options
|
return this.#trigger.options
|
||||||
.run(eventData, ctx)
|
.run(eventData, ctx)
|
||||||
.then((output) => {
|
.then((output) => {
|
||||||
this.#logger.log(
|
this.#logger.log(`Run ${data.id} complete 🏃`);
|
||||||
`Completed workflow '${this.#options.name}', run ${
|
|
||||||
data.id
|
|
||||||
} 🏃`
|
|
||||||
);
|
|
||||||
|
|
||||||
return serverRPC.send("COMPLETE_WORKFLOW_RUN", {
|
return serverRPC.send("COMPLETE_WORKFLOW_RUN", {
|
||||||
runId: data.id,
|
runId: data.id,
|
||||||
|
|||||||
Generated
+10
-1037
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -124,6 +124,7 @@
|
|||||||
"DEBUG",
|
"DEBUG",
|
||||||
"TRIGGER_LOG_LEVEL",
|
"TRIGGER_LOG_LEVEL",
|
||||||
"TRIGGER_API_KEY",
|
"TRIGGER_API_KEY",
|
||||||
"TRIGGER_API_URL"
|
"TRIGGER_API_URL",
|
||||||
|
"APP_ENV"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user