Wrap chunk handler errors in InternalError.

The OutOfRange error is used to signal that buffering is needed internally, so we must ensure that possible OutOfRange errors thrown by the user-provided handler are treated as Internal errors.

PiperOrigin-RevId: 852777351
diff --git a/quiche/oblivious_http/oblivious_http_client.cc b/quiche/oblivious_http/oblivious_http_client.cc
index 49a8ade..197c0d7 100644
--- a/quiche/oblivious_http/oblivious_http_client.cc
+++ b/quiche/oblivious_http/oblivious_http_client.cc
@@ -224,6 +224,9 @@
 
 absl::Status ChunkedObliviousHttpClient::DecryptResponseCheckpoint(
     absl::string_view& response_checkpoint, bool end_stream) {
+  if (chunk_handler_ == nullptr) {
+    return absl::InternalError("Chunk handler is null.");
+  }
   QuicheDataReader reader(response_checkpoint);
   switch (response_current_section_) {
     case ResponseMessageSection::kEnd:
@@ -293,13 +296,12 @@
                   response_chunk_counter.GetChunkNonce(), is_final_chunk));
 
           response_chunk_counter.Increment();
-          if (chunk_handler_ == nullptr) {
-            return absl::InternalError("Chunk handler is null.");
-          }
           absl::Status handler_status =
               chunk_handler_->OnDecryptedChunk(decrypted_chunk);
           if (!handler_status.ok()) {
-            return handler_status;
+            return absl::InternalError(absl::StrCat(
+                "Chunk handler failed to process decrypted chunk: ",
+                handler_status.message()));
           }
         }
 
@@ -331,17 +333,18 @@
               response_chunk_counter.GetChunkNonce(),
               /*is_final_chunk=*/true));
 
-      if (chunk_handler_ == nullptr) {
-        return absl::InternalError("Chunk handler is null.");
-      }
       absl::Status handler_status =
           chunk_handler_->OnDecryptedChunk(decrypted_chunk);
       if (!handler_status.ok()) {
-        return handler_status;
+        return absl::InternalError(
+            absl::StrCat("Chunk handler failed to process decrypted chunk: ",
+                         handler_status.message()));
       }
       handler_status = chunk_handler_->OnChunksDone();
       if (!handler_status.ok()) {
-        return handler_status;
+        return absl::InternalError(
+            absl::StrCat("Chunk handler failed to process chunks done: ",
+                         handler_status.message()));
       }
       return absl::OkStatus();
     }
diff --git a/quiche/oblivious_http/oblivious_http_client_test.cc b/quiche/oblivious_http/oblivious_http_client_test.cc
index ecb6cbf..0b4fab1 100644
--- a/quiche/oblivious_http/oblivious_http_client_test.cc
+++ b/quiche/oblivious_http/oblivious_http_client_test.cc
@@ -47,7 +47,7 @@
           "OnDecryptedChunk called after OnChunksDone.");
     }
     if (fail_on_decrypted_chunk_) {
-      return absl::InternalError("Failed to decrypt chunk.");
+      return absl::OutOfRangeError("Some custom supplied error.");
     }
     decrypted_chunks_.push_back(std::string(decrypted_chunk));
     return absl::OkStatus();
@@ -59,7 +59,7 @@
           "OnChunksDone called more than once.");
     }
     if (fail_on_chunks_done_) {
-      return absl::InternalError("Failed to handle chunks done.");
+      return absl::OutOfRangeError("Some custom supplied error.");
     }
     on_chunks_done_called_ = true;
     return absl::OkStatus();
diff --git a/quiche/oblivious_http/oblivious_http_gateway.cc b/quiche/oblivious_http/oblivious_http_gateway.cc
index 1d3b335..8480a5f 100644
--- a/quiche/oblivious_http/oblivious_http_gateway.cc
+++ b/quiche/oblivious_http/oblivious_http_gateway.cc
@@ -171,8 +171,13 @@
                                   ObliviousHttpRequest::DecryptChunk(
                                       *oblivious_http_request_context_, chunk,
                                       /*is_final_chunk=*/false));
-          QUICHE_RETURN_IF_ERROR(
-              chunk_handler_.OnDecryptedChunk(decrypted_chunk));
+          absl::Status handler_status =
+              chunk_handler_.OnDecryptedChunk(decrypted_chunk);
+          if (!handler_status.ok()) {
+            return absl::InternalError(absl::StrCat(
+                "Chunk handler failed to process decrypted chunk: ",
+                handler_status.message()));
+          }
         }
 
         SaveCheckpoint(reader);
@@ -194,8 +199,19 @@
           ObliviousHttpRequest::DecryptChunk(*oblivious_http_request_context_,
                                              reader.PeekRemainingPayload(),
                                              /*is_final_chunk=*/true));
-      QUICHE_RETURN_IF_ERROR(chunk_handler_.OnDecryptedChunk(decrypted_chunk));
-      QUICHE_RETURN_IF_ERROR(chunk_handler_.OnChunksDone());
+      absl::Status handler_status =
+          chunk_handler_.OnDecryptedChunk(decrypted_chunk);
+      if (!handler_status.ok()) {
+        return absl::InternalError(
+            absl::StrCat("Chunk handler failed to process decrypted chunk: ",
+                         handler_status.message()));
+      }
+      handler_status = chunk_handler_.OnChunksDone();
+      if (!handler_status.ok()) {
+        return absl::InternalError(
+            absl::StrCat("Chunk handler failed to process chunks done: ",
+                         handler_status.message()));
+      }
     }
   }
   return absl::OkStatus();
diff --git a/quiche/oblivious_http/oblivious_http_gateway_test.cc b/quiche/oblivious_http/oblivious_http_gateway_test.cc
index 5736011..731ccf7 100644
--- a/quiche/oblivious_http/oblivious_http_gateway_test.cc
+++ b/quiche/oblivious_http/oblivious_http_gateway_test.cc
@@ -233,14 +233,15 @@
             absl::StatusCode::kInvalidArgument);
 }
 
-TEST(ChunkedObliviousHttpGateway, ChunkHandlerOnChunkErrorPropagates) {
+TEST(ChunkedObliviousHttpGateway,
+     ChunkHandlerOnChunkErrorPropagatesAsInternalError) {
   class FailingChunkHandler : public ObliviousHttpChunkHandler {
    public:
     FailingChunkHandler() = default;
     ~FailingChunkHandler() override = default;
     absl::Status OnDecryptedChunk(
         absl::string_view /*decrypted_chunk*/) override {
-      return absl::InvalidArgumentError("Invalid data");
+      return absl::OutOfRangeError("Some custom supplied error");
     }
     absl::Status OnChunksDone() override {
       return absl::InvalidArgumentError("Invalid data");
@@ -254,10 +255,11 @@
                                      &encapsulated_request_bytes));
 
   EXPECT_EQ(instance->DecryptRequest(encapsulated_request_bytes, true).code(),
-            absl::StatusCode::kInvalidArgument);
+            absl::StatusCode::kInternal);
 }
 
-TEST(ChunkedObliviousHttpGateway, ChunkHandlerOnChunksDoneErrorPropagates) {
+TEST(ChunkedObliviousHttpGateway,
+     ChunkHandlerOnChunksDoneErrorPropagatesAsInternalError) {
   class FailingChunkHandler : public ObliviousHttpChunkHandler {
    public:
     FailingChunkHandler() = default;
@@ -267,7 +269,7 @@
       return absl::OkStatus();
     }
     absl::Status OnChunksDone() override {
-      return absl::InvalidArgumentError("Invalid data");
+      return absl::OutOfRangeError("Some custom supplied error");
     }
   };
   FailingChunkHandler chunk_handler;
@@ -278,7 +280,7 @@
                                      &encapsulated_request_bytes));
 
   EXPECT_EQ(instance->DecryptRequest(encapsulated_request_bytes, true).code(),
-            absl::StatusCode::kInvalidArgument);
+            absl::StatusCode::kInternal);
 }
 
 TEST(ObliviousHttpGateway, TestDecryptingMultipleRequestsWithSingleInstance) {