Skip to content

Commit

Permalink
add(logging/pushnotifier): adds status code and error to logging (cro…
Browse files Browse the repository at this point in the history
…ss-seed#590)

This PR adds logging details to pushNotifier.ts for status code and
errors given when notifications are sent.

---------

Co-authored-by: nitsua <[email protected]>
  • Loading branch information
zakkarry and austinwbest authored Feb 1, 2024
1 parent 9d1eae4 commit c2b31f7
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions src/pushNotifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fetch from "node-fetch";
import { ActionResult, InjectionResult, SaveResult } from "./constants.js";
import {
ActionResult,
InjectionResult,
SaveResult,
USER_AGENT,
} from "./constants.js";
import { ResultAssessment } from "./decide.js";
import { Label, logger } from "./logger.js";
import { getRuntimeConfig } from "./runtimeConfig.js";
Expand All @@ -24,15 +29,38 @@ export class PushNotifier {
this.url = url;
}

notify({ title = "cross-seed", body, ...rest }: PushNotification): void {
async notify({
title = "cross-seed",
body,
...rest
}: PushNotification): Promise<void> {
if (this.url) {
fetch(this.url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title, body, ...rest }),
}).catch(() => {
logger.error({ message: "Failed to send push notification" });
});
try {
const response = await fetch(this.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
},
body: JSON.stringify({ title, body, ...rest }),
});

if (!response.ok) {
logger.error({
message: "Failed to send push notification",
});
logger.debug({
message: `Server response: ${response.status} ${response.statusText}`,
});
}
} catch (error) {
logger.error({
message: "Failed to send push notification",
});
logger.debug({
message: error,
});
}
}
}
}
Expand Down

0 comments on commit c2b31f7

Please sign in to comment.