initial commit

Co-authored-by: Aditya Ramani <a_ramani@apple.com>
Co-authored-by: Agam Dua <agam_dua@apple.com>
Co-authored-by: Danny Canter <danny_canter@apple.com>
Co-authored-by: Dmitry Kovba <dkovba@apple.com>
Co-authored-by: Eric Ernst <eric_ernst@apple.com>
Co-authored-by: Evan Hazlett <ehazlett@apple.com>
Co-authored-by: Gilbert Song <gilbertsong@apple.com>
Co-authored-by: Hugh Bussell <hbussell@apple.com>
Co-authored-by: John Logan <john_logan@apple.com>
Co-authored-by: Kathryn Baldauf <k_baldauf@apple.com>
Co-authored-by: Madhu Venugopal <mvenugopal@apple.com>
Co-authored-by: Michael Crosby <michael_crosby@apple.com>
Co-authored-by: Sidhartha Mani <sidhartha_mani@apple.com>
Co-authored-by: Tanweer Noor <tnoor@apple.com>
Co-authored-by: Ximena Perez Diaz <xperez528@gmail.com>
Co-authored-by: Yibo Zhuang <yzhuang@apple.com>
This commit is contained in:
Kathryn Baldauf
2025-06-05 16:15:21 -07:00
co-authored by Aditya Ramani Agam Dua Danny Canter Dmitry Kovba Eric Ernst Evan Hazlett Gilbert Song Hugh Bussell John Logan Madhu Venugopal Michael Crosby Sidhartha Mani Tanweer Noor Ximena Perez Diaz Yibo Zhuang
commit 3407cc3f16
229 changed files with 40687 additions and 0 deletions
@@ -0,0 +1,156 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the containerization project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
/// The core error type for Containerization.
///
/// Most API surfaces for the core container/process/agent types will
/// return a ContainerizationError.
public struct ContainerizationError: Swift.Error, Sendable {
public var code: Code
public var message: String
public var cause: (any Error)?
/// Creates a new error.
///
/// - Parameters:
/// - code: The error code.
/// - message: A description of the error.
/// - cause: The original error which led to this error being thrown.
public init(_ code: Code, message: String, cause: (any Error)? = nil) {
self.code = code
self.message = message
self.cause = cause
}
/// Creates a new error.
///
/// - Parameters:
/// - rawCode: The error code value as a String.
/// - message: A description of the error.
/// - cause: The original error which led to this error being thrown.
public init(_ rawCode: String, message: String, cause: (any Error)? = nil) {
self.code = Code(rawValue: rawCode)
self.message = message
self.cause = cause
}
public func hash(into hasher: inout Hasher) {
hasher.combine(self.code)
hasher.combine(self.message)
}
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.code == rhs.code && lhs.message == rhs.message
}
public func isCode(_ code: Code) -> Bool {
self.code == code
}
}
extension ContainerizationError: CustomStringConvertible {
public var description: String {
guard let cause = self.cause else {
return "\(self.code): \"\(self.message)\""
}
return "\(self.code): \"\(self.message)\" (cause: \"\(cause)\")"
}
}
extension ContainerizationError {
public struct Code: Sendable, Hashable {
private enum Value: Hashable, Sendable, CaseIterable {
case unknown
case invalidArgument
case internalError
case exists
case notFound
case cancelled
case invalidState
case empty
case timeout
case unsupported
case interrupted
}
private var value: Value
private init(_ value: Value) {
self.value = value
}
init(rawValue: String) {
let values = Value.allCases.reduce(into: [String: Value]()) {
$0[String(describing: $1)] = $1
}
let match = values[rawValue]
guard let match else {
fatalError("invalid Code Value \(rawValue)")
}
self.value = match
}
public static var unknown: Self {
Self(.unknown)
}
public static var invalidArgument: Self {
Self(.invalidArgument)
}
public static var internalError: Self {
Self(.internalError)
}
public static var exists: Self {
Self(.exists)
}
public static var notFound: Self {
Self(.notFound)
}
public static var cancelled: Self {
Self(.cancelled)
}
public static var invalidState: Self {
Self(.invalidState)
}
public static var empty: Self {
Self(.empty)
}
public static var timeout: Self {
Self(.timeout)
}
public static var unsupported: Self {
Self(.unsupported)
}
public static var interrupted: Self {
Self(.interrupted)
}
}
}
extension ContainerizationError.Code: CustomStringConvertible {
public var description: String {
String(describing: self.value)
}
}