Skip to content

Commit 0fcc4c9

Browse files
authored
Include the service name in the serviceFinishedUnexpectedly error (#217)
## Motivation To aid debugging and troubleshooting, it is helpful to understand which Service led to the error. ## Modifications Add the service name to the `serviceFinishedUnexpectedly` error. ## Result Easier to debug failing services.
1 parent 7bef63b commit 0fcc4c9

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

Sources/ServiceLifecycle/Docs.docc/curation/ServiceGroupError.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
### Service Group Errors
1111

12-
- ``serviceFinishedUnexpectedly(file:line:)``
12+
- ``serviceFinishedUnexpectedly(file:line:service:)``
1313
- ``alreadyRunning(file:line:)``
1414
- ``alreadyFinished(file:line:)``

Sources/ServiceLifecycle/ServiceGroup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public actor ServiceGroup: Sendable, Service {
407407
group: &group,
408408
cancellationTimeoutTask: &cancellationTimeoutTask
409409
)
410-
return .failure(ServiceGroupError.serviceFinishedUnexpectedly())
410+
return .failure(ServiceGroupError.serviceFinishedUnexpectedly(service: "\(service.service)"))
411411

412412
case .gracefullyShutdownGroup:
413413
self.logger.debug(
@@ -670,7 +670,7 @@ public actor ServiceGroup: Sendable, Service {
670670
group: &group,
671671
cancellationTimeoutTask: &cancellationTimeoutTask
672672
)
673-
throw ServiceGroupError.serviceFinishedUnexpectedly()
673+
throw ServiceGroupError.serviceFinishedUnexpectedly(service: "\(service.service)")
674674
}
675675
// The service that we signalled graceful shutdown did exit/
676676
// We can continue to the next one.

Sources/ServiceLifecycle/ServiceRunnerError.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ public struct ServiceGroupError: Error, Hashable, Sendable {
5050

5151
/// Internal class that contains the actual error code.
5252
private final class Backing: Hashable, Sendable {
53+
let message: String?
5354
let errorCode: Code
5455
let file: String
5556
let line: Int
5657

57-
init(errorCode: Code, file: String, line: Int) {
58+
init(errorCode: Code, file: String, line: Int, message: String?) {
5859
self.errorCode = errorCode
5960
self.file = file
6061
self.line = line
62+
self.message = message
6163
}
6264

6365
static func == (lhs: Backing, rhs: Backing) -> Bool {
@@ -83,35 +85,42 @@ public struct ServiceGroupError: Error, Hashable, Sendable {
8385
self.backing = backing
8486
}
8587

86-
/// An error that indicates that the service group is already running.
88+
/// Indicates that the service group is already running.
8789
public static func alreadyRunning(file: String = #fileID, line: Int = #line) -> Self {
8890
Self(
8991
.init(
9092
errorCode: .alreadyRunning,
9193
file: file,
92-
line: line
94+
line: line,
95+
message: ""
9396
)
9497
)
9598
}
9699

97-
/// An error that indicates that the service group has already finished running.
100+
/// Indicates that the service group has already finished running.
98101
public static func alreadyFinished(file: String = #fileID, line: Int = #line) -> Self {
99102
Self(
100103
.init(
101104
errorCode: .alreadyFinished,
102105
file: file,
103-
line: line
106+
line: line,
107+
message: ""
104108
)
105109
)
106110
}
107111

108-
/// An error that indicates that a service finished unexpectedly even though it indicated it is a long running service.
109-
public static func serviceFinishedUnexpectedly(file: String = #fileID, line: Int = #line) -> Self {
112+
/// Indicates that a service finished unexpectedly even though it indicated it is a long running service.
113+
public static func serviceFinishedUnexpectedly(
114+
file: String = #fileID,
115+
line: Int = #line,
116+
service: String? = nil
117+
) -> Self {
110118
Self(
111119
.init(
112120
errorCode: .serviceFinishedUnexpectedly,
113121
file: file,
114-
line: line
122+
line: line,
123+
message: service.flatMap { "Service failed(\($0))" }
115124
)
116125
)
117126
}
@@ -120,6 +129,6 @@ public struct ServiceGroupError: Error, Hashable, Sendable {
120129
extension ServiceGroupError: CustomStringConvertible {
121130
/// A string representation of the service group error.
122131
public var description: String {
123-
"ServiceGroupError: errorCode: \(self.backing.errorCode), file: \(self.backing.file), line: \(self.backing.line)"
132+
"ServiceGroupError: errorCode: \(self.backing.errorCode), file: \(self.backing.file), line: \(self.backing.line) \(self.backing.message.flatMap { ", message: \($0)" } ?? "")"
124133
}
125134
}

Tests/ServiceLifecycleTests/ServiceGroupTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ final class ServiceGroupTests: XCTestCase {
122122
await mockService.resumeRunContinuation(with: .success(()))
123123

124124
try await XCTAsyncAssertThrowsError(await group.next()) {
125-
XCTAssertEqual($0 as? ServiceGroupError, .serviceFinishedUnexpectedly())
125+
XCTAssertEqual(
126+
$0 as? ServiceGroupError,
127+
.serviceFinishedUnexpectedly(service: "Service1")
128+
)
126129
}
127130
}
128131
}
@@ -290,7 +293,10 @@ final class ServiceGroupTests: XCTestCase {
290293
await longService.resumeRunContinuation(with: .success(()))
291294

292295
try await XCTAsyncAssertThrowsError(await group.next()) {
293-
XCTAssertEqual($0 as? ServiceGroupError, .serviceFinishedUnexpectedly())
296+
XCTAssertEqual(
297+
$0 as? ServiceGroupError,
298+
.serviceFinishedUnexpectedly(service: "Service1")
299+
)
294300
}
295301
}
296302
}

0 commit comments

Comments
 (0)