Skip to content

Commit 22d508b

Browse files
committed
Ensure all .invalidArgument errors are converted into GRPCStatus
1 parent 8633fe6 commit 22d508b

File tree

1 file changed

+81
-49
lines changed

1 file changed

+81
-49
lines changed

vminitd/Sources/vminitd/Server+GRPC.swift

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,14 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
432432
"configuration": "\(request.configuration.count)",
433433
])
434434

435-
if !request.hasContainerID {
436-
throw ContainerizationError(
437-
.invalidArgument,
438-
message: "processes in the root of the vm not implemented"
439-
)
440-
}
441-
442435
do {
436+
if !request.hasContainerID {
437+
throw ContainerizationError(
438+
.invalidArgument,
439+
message: "processes in the root of the vm not implemented"
440+
)
441+
}
442+
443443
var ociSpec = try JSONDecoder().decode(
444444
ContainerizationOCI.Spec.self,
445445
from: request.configuration
@@ -536,14 +536,14 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
536536
"signal": "\(request.signal)",
537537
])
538538

539-
if !request.hasContainerID {
540-
throw ContainerizationError(
541-
.invalidArgument,
542-
message: "processes in the root of the vm not implemented"
543-
)
544-
}
545-
546539
do {
540+
if !request.hasContainerID {
541+
throw ContainerizationError(
542+
.invalidArgument,
543+
message: "processes in the root of the vm not implemented"
544+
)
545+
}
546+
547547
let ctr = try await self.state.get(container: request.containerID)
548548
try await ctr.kill(execID: request.id, request.signal)
549549

@@ -584,14 +584,14 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
584584
"containerID": "\(request.containerID)",
585585
])
586586

587-
if !request.hasContainerID {
588-
throw ContainerizationError(
589-
.invalidArgument,
590-
message: "processes in the root of the vm not implemented"
591-
)
592-
}
593-
594587
do {
588+
if !request.hasContainerID {
589+
throw ContainerizationError(
590+
.invalidArgument,
591+
message: "processes in the root of the vm not implemented"
592+
)
593+
}
594+
595595
let ctr = try await self.state.get(container: request.containerID)
596596

597597
// Are we trying to delete the container itself?
@@ -640,14 +640,14 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
640640
"containerID": "\(request.containerID)",
641641
])
642642

643-
if !request.hasContainerID {
644-
throw ContainerizationError(
645-
.invalidArgument,
646-
message: "processes in the root of the vm not implemented"
647-
)
648-
}
649-
650643
do {
644+
if !request.hasContainerID {
645+
throw ContainerizationError(
646+
.invalidArgument,
647+
message: "processes in the root of the vm not implemented"
648+
)
649+
}
650+
651651
let ctr = try await self.state.get(container: request.containerID)
652652
let pid = try await ctr.start(execID: request.id)
653653

@@ -693,20 +693,36 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
693693
"containerID": "\(request.containerID)",
694694
])
695695

696-
if !request.hasContainerID {
697-
throw ContainerizationError(
698-
.invalidArgument,
699-
message: "processes in the root of the vm not implemented"
700-
)
701-
}
702-
703696
do {
697+
if !request.hasContainerID {
698+
throw ContainerizationError(
699+
.invalidArgument,
700+
message: "processes in the root of the vm not implemented"
701+
)
702+
}
703+
704704
let ctr = try await self.state.get(container: request.containerID)
705705
let size = Terminal.Size(
706706
width: UInt16(request.columns),
707707
height: UInt16(request.rows)
708708
)
709709
try await ctr.resize(execID: request.id, size: size)
710+
} catch let err as ContainerizationError {
711+
log.error(
712+
"resizeProcess",
713+
metadata: [
714+
"id": "\(request.id)",
715+
"containerID": "\(request.containerID)",
716+
"error": "\(err)",
717+
])
718+
switch err.code {
719+
case .invalidArgument:
720+
throw GRPCStatus(code: .invalidArgument, message: "resizeProcess: failed to resize process: \(err)")
721+
case .notFound:
722+
throw GRPCStatus(code: .notFound, message: "resizeProcess: failed to resize process: \(err)")
723+
default:
724+
throw GRPCStatus(code: .internalError, message: "resizeProcess: failed to resize process: \(err)")
725+
}
710726
} catch {
711727
log.error(
712728
"resizeProcess",
@@ -734,14 +750,14 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
734750
"containerID": "\(request.containerID)",
735751
])
736752

737-
if !request.hasContainerID {
738-
throw ContainerizationError(
739-
.invalidArgument,
740-
message: "processes in the root of the vm not implemented"
741-
)
742-
}
743-
744753
do {
754+
if !request.hasContainerID {
755+
throw ContainerizationError(
756+
.invalidArgument,
757+
message: "processes in the root of the vm not implemented"
758+
)
759+
}
760+
745761
let ctr = try await self.state.get(container: request.containerID)
746762
let exitStatus = try await ctr.wait(execID: request.id)
747763

@@ -788,19 +804,35 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
788804
"containerID": "\(request.containerID)",
789805
])
790806

791-
if !request.hasContainerID {
792-
throw ContainerizationError(
793-
.invalidArgument,
794-
message: "processes in the root of the vm not implemented"
795-
)
796-
}
797-
798807
do {
808+
if !request.hasContainerID {
809+
throw ContainerizationError(
810+
.invalidArgument,
811+
message: "processes in the root of the vm not implemented"
812+
)
813+
}
814+
799815
let ctr = try await self.state.get(container: request.containerID)
800816

801817
try await ctr.closeStdin(execID: request.id)
802818

803819
return .init()
820+
} catch let err as ContainerizationError {
821+
log.error(
822+
"closeProcessStdin",
823+
metadata: [
824+
"id": "\(request.id)",
825+
"containerID": "\(request.containerID)",
826+
"error": "\(err)",
827+
])
828+
switch err.code {
829+
case .invalidArgument:
830+
throw GRPCStatus(code: .invalidArgument, message: "closeProcessStdin: failed to close process stdin: \(err)")
831+
case .notFound:
832+
throw GRPCStatus(code: .notFound, message: "closeProcessStdin: failed to close process stdin: \(err)")
833+
default:
834+
throw GRPCStatus(code: .internalError, message: "closeProcessStdin: failed to close process stdin: \(err)")
835+
}
804836
} catch {
805837
log.error(
806838
"closeProcessStdin",

0 commit comments

Comments
 (0)