Files
swift-service-lifecycle/Sources/ServiceLifecycle/ServiceRunnerError.swift
T
Joseph Heck 83cecce066 extending the documentation content (#212)
- cleaning up grammar to use present tense and more direct phrasing
- extending documentation coverage

Current state:
```
                | Abstract        | Curated         | Code Listing
Types           | 71% (10/14)     | 64% (9/14)      | 0.0% (0/14)
Members         | 44% (49/111)    | 77% (85/111)    | 0.0% (0/111)
Globals         | 73% (8/11)      | 91% (10/11)     | 9.1% (1/11)
```
2025-09-19 08:31:20 +00:00

126 lines
4.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftServiceLifecycle open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftServiceLifecycle project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftServiceLifecycle project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// Errors thrown by a service group.
public struct ServiceGroupError: Error, Hashable, Sendable {
/// A struct that represents the possible error codes.
public struct Code: Hashable, Sendable, CustomStringConvertible {
private enum _Code: Hashable, Sendable {
case alreadyRunning
case alreadyFinished
case serviceFinishedUnexpectedly
}
private var code: _Code
private init(code: _Code) {
self.code = code
}
/// A string representation of a service group error.
public var description: String {
switch self.code {
case .alreadyRunning:
return "The service group is already running the services."
case .alreadyFinished:
return "The service group has already finished running the services."
case .serviceFinishedUnexpectedly:
return "A service has finished unexpectedly."
}
}
/// Indicates that the service group is already running.
public static let alreadyRunning = Code(code: .alreadyRunning)
/// Indicates that the service group has already finished running.
public static let alreadyFinished = Code(code: .alreadyFinished)
/// Indicates that a service finished unexpectedly.
public static let serviceFinishedUnexpectedly = Code(code: .serviceFinishedUnexpectedly)
}
/// Internal class that contains the actual error code.
private final class Backing: Hashable, Sendable {
let errorCode: Code
let file: String
let line: Int
init(errorCode: Code, file: String, line: Int) {
self.errorCode = errorCode
self.file = file
self.line = line
}
static func == (lhs: Backing, rhs: Backing) -> Bool {
lhs.errorCode == rhs.errorCode
}
func hash(into hasher: inout Hasher) {
hasher.combine(self.errorCode)
}
}
/// The backing storage of the error.
private let backing: Backing
/// The error code.
///
/// - Note: This is the only thing used for the `Equatable` and `Hashable` comparisons for instances of `ServiceGroupError`.
public var errorCode: Code {
self.backing.errorCode
}
private init(_ backing: Backing) {
self.backing = backing
}
/// An error that indicates that the service group is already running.
public static func alreadyRunning(file: String = #fileID, line: Int = #line) -> Self {
Self(
.init(
errorCode: .alreadyRunning,
file: file,
line: line
)
)
}
/// An error that indicates that the service group has already finished running.
public static func alreadyFinished(file: String = #fileID, line: Int = #line) -> Self {
Self(
.init(
errorCode: .alreadyFinished,
file: file,
line: line
)
)
}
/// An error that indicates that a service finished unexpectedly even though it indicated it is a long running service.
public static func serviceFinishedUnexpectedly(file: String = #fileID, line: Int = #line) -> Self {
Self(
.init(
errorCode: .serviceFinishedUnexpectedly,
file: file,
line: line
)
)
}
}
extension ServiceGroupError: CustomStringConvertible {
/// A string representation of the service group error.
public var description: String {
"ServiceGroupError: errorCode: \(self.backing.errorCode), file: \(self.backing.file), line: \(self.backing.line)"
}
}