Skip to content

Commit a9d8bda

Browse files
committed
improvements
1 parent b7e6a6f commit a9d8bda

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

PatchPanda.Web/Services/Interfaces/INotificationService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ List<AppVersion> newerVersions
2424
/// Sends a notification message via all initialized services.
2525
/// </summary>
2626
/// <param name="message">Message to send</param>
27-
/// <param name="propagateExceptions">Whether any failures should throw</param>
27+
/// <param name="propagateExceptions">Whether any failures should throw. If false, it will log failures instead of throwing</param>
28+
/// <param name="noSuccessIsError">Whether it should throw error when sending notification was not successful</param>
2829
/// <returns>Boolean determining if any notifications were successful</returns>
29-
Task<bool> TrySendNotification(string message, bool propagateExceptions = false);
30+
Task<bool> TrySendNotification(
31+
string message,
32+
bool propagateExceptions = false,
33+
bool noSuccessIsError = true
34+
);
3035

3136
/// <summary>
3237
/// Sends a notification message via all initialized services, but throws if none succeed and if any of them fail.

PatchPanda.Web/Services/NotificationService.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task SendAutoUpdateResult(
4444
var result = await TrySendNotification(message);
4545

4646
if (!result)
47-
logger.LogError(
47+
logger.LogWarning(
4848
"Failed to send auto-update notification for container {ContainerName}",
4949
container.Name
5050
);
@@ -61,7 +61,11 @@ List<AppVersion> newerVersions
6161
return await TrySendNotification(message);
6262
}
6363

64-
public async Task<bool> TrySendNotification(string message, bool propagateExceptions = false)
64+
public async Task<bool> TrySendNotification(
65+
string message,
66+
bool propagateExceptions = false,
67+
bool noSuccessIsError = false
68+
)
6569
{
6670
var success = 0;
6771
if (discordService.IsInitialized)
@@ -97,21 +101,18 @@ public async Task<bool> TrySendNotification(string message, bool propagateExcept
97101
if (success != 0)
98102
return true;
99103

100-
if (!AnyInitialized)
101-
logger.LogWarning("No notification services are initialized. Message was not sent.");
102-
else
103-
logger.LogError("The notification could not be sent successfully.");
104+
var errorMessage = !AnyInitialized
105+
? "No notification services are initialized. Message was not sent."
106+
: "The notification could not be sent successfully.";
104107

105-
return false;
106-
}
108+
if (noSuccessIsError)
109+
throw new(errorMessage);
107110

108-
public async Task SendNotification(string message)
109-
{
110-
var result = await TrySendNotification(message, true);
111+
logger.LogWarning(errorMessage);
111112

112-
if (!result)
113-
throw new Exception(
114-
"No notification services have been initialized. Check logs for details."
115-
);
113+
return false;
116114
}
115+
116+
public async Task SendNotification(string message) =>
117+
await TrySendNotification(message, true, true);
117118
}

0 commit comments

Comments
 (0)