Build API: Check chroot exists before executing inside

BUG=None
TEST=cq

Change-Id: I143ab74c3116679f5299ab0dfa22b60301270769
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2248704
Tested-by: Alex Klein <saklein@chromium.org>
Reviewed-by: LaMont Jones <lamontjones@chromium.org>
Reviewed-by: Michael Mortensen <mmortensen@google.com>
Commit-Queue: Alex Klein <saklein@chromium.org>
diff --git a/api/router.py b/api/router.py
index b462b6f..981eef9 100644
--- a/api/router.py
+++ b/api/router.py
@@ -48,6 +48,10 @@
   """Base error class for the module."""
 
 
+class InvalidSdkError(Error):
+  """Raised when the SDK is invalid or does not exist."""
+
+
 class CrosSdkNotRunError(Error):
   """Raised when the cros_sdk command could not be run to enter the chroot."""
 
@@ -277,6 +281,9 @@
     # Parse the chroot and clear the chroot field in the input message.
     chroot = field_handler.handle_chroot(input_msg)
 
+    if not chroot.exists():
+      raise InvalidSdkError('Chroot does not exist.')
+
     # Use a ContextManagerStack to avoid the deep nesting this many
     # context managers introduces.
     with cros_build_lib.ContextManagerStack() as stack: