Change STOP_SENDING frame related APIs to use QuicRstStreamError type.

Change QuicControlFrameManager::WriteOrBufferStopSending(),
QuicSession::SendStopSending(), QuicStream::OnStopSending(), and
QuicStopSendingFrame constructor to take QuicRstStreamError arguments.
Add QuicStopSendingFrame::error_code member.  Remove
QuicStopSendingFrame::application_error_code and QuicApplicationErrorCode.

Previously the error code was stored in an uint16_t, now in a
QuicRstStreamError, which has a wider underlying type of int.  However, the
truncation logic for incoming STOP_SENDING frames in
QuicFramer::ProcessStopSendingFrame() is unchanged.
PiperOrigin-RevId: 329762228
Change-Id: Iab016a992e9bdde945ed96c7ba03b86276c1e968
diff --git a/quic/core/frames/quic_frames_test.cc b/quic/core/frames/quic_frames_test.cc
index 3f04c8c..2b84822 100644
--- a/quic/core/frames/quic_frames_test.cc
+++ b/quic/core/frames/quic_frames_test.cc
@@ -95,12 +95,11 @@
   SetControlFrameId(1, &frame);
   EXPECT_EQ(1u, GetControlFrameId(frame));
   stop_sending.stream_id = 321;
-  stop_sending.application_error_code = QUIC_STREAM_CANCELLED;
+  stop_sending.error_code = QUIC_STREAM_CANCELLED;
   std::ostringstream stream;
   stream << stop_sending;
-  EXPECT_EQ(
-      "{ control_frame_id: 1, stream_id: 321, application_error_code: 6 }\n",
-      stream.str());
+  EXPECT_EQ("{ control_frame_id: 1, stream_id: 321, error_code: 6 }\n",
+            stream.str());
   EXPECT_TRUE(IsControlFrame(frame.type));
 }
 
diff --git a/quic/core/frames/quic_stop_sending_frame.cc b/quic/core/frames/quic_stop_sending_frame.cc
index c4d732e..d3c1cdd 100644
--- a/quic/core/frames/quic_stop_sending_frame.cc
+++ b/quic/core/frames/quic_stop_sending_frame.cc
@@ -6,18 +6,17 @@
 
 namespace quic {
 
-QuicStopSendingFrame::QuicStopSendingFrame(
-    QuicControlFrameId control_frame_id,
-    QuicStreamId stream_id,
-    QuicApplicationErrorCode application_error_code)
+QuicStopSendingFrame::QuicStopSendingFrame(QuicControlFrameId control_frame_id,
+                                           QuicStreamId stream_id,
+                                           QuicRstStreamErrorCode error_code)
     : control_frame_id(control_frame_id),
       stream_id(stream_id),
-      application_error_code(application_error_code) {}
+      error_code(error_code) {}
 
 std::ostream& operator<<(std::ostream& os, const QuicStopSendingFrame& frame) {
   os << "{ control_frame_id: " << frame.control_frame_id
      << ", stream_id: " << frame.stream_id
-     << ", application_error_code: " << frame.application_error_code << " }\n";
+     << ", error_code: " << frame.error_code << " }\n";
   return os;
 }
 
diff --git a/quic/core/frames/quic_stop_sending_frame.h b/quic/core/frames/quic_stop_sending_frame.h
index 8222067..f54ae0e 100644
--- a/quic/core/frames/quic_stop_sending_frame.h
+++ b/quic/core/frames/quic_stop_sending_frame.h
@@ -17,7 +17,7 @@
   QuicStopSendingFrame() = default;
   QuicStopSendingFrame(QuicControlFrameId control_frame_id,
                        QuicStreamId stream_id,
-                       QuicApplicationErrorCode application_error_code);
+                       QuicRstStreamErrorCode error_code);
 
   friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
       std::ostream& os,
@@ -27,7 +27,9 @@
   // and non-zero when sent.
   QuicControlFrameId control_frame_id = kInvalidControlFrameId;
   QuicStreamId stream_id = 0;
-  QuicApplicationErrorCode application_error_code = 0;
+
+  // QuicRstStreamErrorCode associated with the frame.
+  QuicRstStreamErrorCode error_code = QUIC_STREAM_NO_ERROR;
 };
 
 }  // namespace quic
diff --git a/quic/core/http/quic_send_control_stream.cc b/quic/core/http/quic_send_control_stream.cc
index 7be8ee4..a7a5349 100644
--- a/quic/core/http/quic_send_control_stream.cc
+++ b/quic/core/http/quic_send_control_stream.cc
@@ -30,7 +30,7 @@
   QUIC_BUG << "OnStreamReset() called for write unidirectional stream.";
 }
 
-bool QuicSendControlStream::OnStopSending(uint16_t /* code */) {
+bool QuicSendControlStream::OnStopSending(QuicRstStreamErrorCode /* code */) {
   stream_delegate()->OnStreamError(
       QUIC_HTTP_CLOSED_CRITICAL_STREAM,
       "STOP_SENDING received for send control stream");
diff --git a/quic/core/http/quic_send_control_stream.h b/quic/core/http/quic_send_control_stream.h
index 6240310..25ea5b7 100644
--- a/quic/core/http/quic_send_control_stream.h
+++ b/quic/core/http/quic_send_control_stream.h
@@ -31,7 +31,7 @@
   // Overriding QuicStream::OnStopSending() to make sure control stream is never
   // closed before connection.
   void OnStreamReset(const QuicRstStreamFrame& frame) override;
-  bool OnStopSending(uint16_t code) override;
+  bool OnStopSending(QuicRstStreamErrorCode code) override;
 
   // Send SETTINGS frame if it hasn't been sent yet. Settings frame must be the
   // first frame sent on this stream.
diff --git a/quic/core/http/quic_server_session_base_test.cc b/quic/core/http/quic_server_session_base_test.cc
index 12fc297..ff3c62e 100644
--- a/quic/core/http/quic_server_session_base_test.cc
+++ b/quic/core/http/quic_server_session_base_test.cc
@@ -182,20 +182,19 @@
   // would cause a close in the opposite direction. This allows tests to do the
   // extra work to get a two-way (full) close where desired. Also sets up
   // expects needed to ensure that the STOP_SENDING worked as expected.
-  void InjectStopSendingFrame(QuicStreamId stream_id,
-                              QuicRstStreamErrorCode rst_stream_code) {
+  void InjectStopSendingFrame(QuicStreamId stream_id) {
     if (!VersionHasIetfQuicFrames(transport_version())) {
       // Only needed for version 99/IETF QUIC. Noop otherwise.
       return;
     }
-    QuicStopSendingFrame stop_sending(
-        kInvalidControlFrameId, stream_id,
-        static_cast<QuicApplicationErrorCode>(rst_stream_code));
+    QuicStopSendingFrame stop_sending(kInvalidControlFrameId, stream_id,
+                                      QUIC_ERROR_PROCESSING_STREAM);
     EXPECT_CALL(owner_, OnStopSendingReceived(_)).Times(1);
     // Expect the RESET_STREAM that is generated in response to receiving a
     // STOP_SENDING.
     EXPECT_CALL(*connection_, SendControlFrame(_));
-    EXPECT_CALL(*connection_, OnStreamReset(stream_id, rst_stream_code));
+    EXPECT_CALL(*connection_,
+                OnStreamReset(stream_id, QUIC_ERROR_PROCESSING_STREAM));
     session_->OnStopSendingFrame(stop_sending);
   }
 
@@ -258,8 +257,7 @@
 
   // For version-99 will create and receive a stop-sending, completing
   // the full-close expected by this test.
-  InjectStopSendingFrame(GetNthClientInitiatedBidirectionalId(0),
-                         QUIC_ERROR_PROCESSING_STREAM);
+  InjectStopSendingFrame(GetNthClientInitiatedBidirectionalId(0));
 
   EXPECT_EQ(0u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get()));
   // Send the same two bytes of payload in a new packet.
@@ -288,8 +286,7 @@
 
   // For version-99 will create and receive a stop-sending, completing
   // the full-close expected by this test.
-  InjectStopSendingFrame(GetNthClientInitiatedBidirectionalId(0),
-                         QUIC_ERROR_PROCESSING_STREAM);
+  InjectStopSendingFrame(GetNthClientInitiatedBidirectionalId(0));
 
   EXPECT_EQ(0u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get()));
   // Send two bytes of payload.
@@ -329,8 +326,7 @@
 
   // For version-99 will create and receive a stop-sending, completing
   // the full-close expected by this test.
-  InjectStopSendingFrame(GetNthClientInitiatedBidirectionalId(0),
-                         QUIC_ERROR_PROCESSING_STREAM);
+  InjectStopSendingFrame(GetNthClientInitiatedBidirectionalId(0));
 
   // If we were tracking, we'd probably want to reject this because it's data
   // past the reset point of stream 3.  As it's a closed stream we just drop the
diff --git a/quic/core/http/quic_spdy_server_stream_base_test.cc b/quic/core/http/quic_spdy_server_stream_base_test.cc
index a32de61..1d08da3 100644
--- a/quic/core/http/quic_spdy_server_stream_base_test.cc
+++ b/quic/core/http/quic_spdy_server_stream_base_test.cc
@@ -75,9 +75,8 @@
   if (VersionHasIetfQuicFrames(session_.transport_version())) {
     // Create and inject a STOP SENDING frame to complete the close
     // of the stream. This is only needed for version 99/IETF QUIC.
-    QuicStopSendingFrame stop_sending(
-        kInvalidControlFrameId, stream_->id(),
-        static_cast<QuicApplicationErrorCode>(QUIC_STREAM_CANCELLED));
+    QuicStopSendingFrame stop_sending(kInvalidControlFrameId, stream_->id(),
+                                      QUIC_STREAM_CANCELLED);
     session_.OnStopSendingFrame(stop_sending);
   }
 
diff --git a/quic/core/http/quic_spdy_session.cc b/quic/core/http/quic_spdy_session.cc
index dded232..ee846b0 100644
--- a/quic/core/http/quic_spdy_session.cc
+++ b/quic/core/http/quic_spdy_session.cc
@@ -1268,7 +1268,7 @@
       return true;
     }
     default:
-      SendStopSending(static_cast<QuicApplicationErrorCode>(
+      SendStopSending(static_cast<QuicRstStreamErrorCode>(
                           QuicHttp3ErrorCode::STREAM_CREATION_ERROR),
                       pending->id());
       pending->StopReading();
diff --git a/quic/core/http/quic_spdy_session_test.cc b/quic/core/http/quic_spdy_session_test.cc
index 32fc3f1..3f2387a 100644
--- a/quic/core/http/quic_spdy_session_test.cc
+++ b/quic/core/http/quic_spdy_session_test.cc
@@ -1205,9 +1205,9 @@
   // one-way close.
   if (VersionHasIetfQuicFrames(transport_version())) {
     // Only needed for version 99/IETF QUIC.
-    QuicStopSendingFrame stop_sending(
-        kInvalidControlFrameId, GetNthClientInitiatedBidirectionalId(0),
-        static_cast<QuicApplicationErrorCode>(QUIC_ERROR_PROCESSING_STREAM));
+    QuicStopSendingFrame stop_sending(kInvalidControlFrameId,
+                                      GetNthClientInitiatedBidirectionalId(0),
+                                      QUIC_ERROR_PROCESSING_STREAM);
     // Expect the RESET_STREAM that is generated in response to receiving a
     // STOP_SENDING.
     EXPECT_CALL(*connection_,
@@ -1487,9 +1487,8 @@
   // one-way close.
   if (VersionHasIetfQuicFrames(transport_version())) {
     // Only needed for version 99/IETF QUIC.
-    QuicStopSendingFrame stop_sending(
-        kInvalidControlFrameId, stream->id(),
-        static_cast<QuicApplicationErrorCode>(QUIC_STREAM_CANCELLED));
+    QuicStopSendingFrame stop_sending(kInvalidControlFrameId, stream->id(),
+                                      QUIC_STREAM_CANCELLED);
     // Expect the RESET_STREAM that is generated in response to receiving a
     // STOP_SENDING.
     EXPECT_CALL(*connection_,
@@ -2201,8 +2200,7 @@
           QuicStopSendingFrame* stop_sending = frame.stop_sending_frame;
           EXPECT_EQ(stream_id, stop_sending->stream_id);
           EXPECT_EQ(QuicHttp3ErrorCode::STREAM_CREATION_ERROR,
-                    static_cast<QuicHttp3ErrorCode>(
-                        stop_sending->application_error_code));
+                    static_cast<QuicHttp3ErrorCode>(stop_sending->error_code));
 
           return ClearControlFrame(frame);
         }));
@@ -2907,8 +2905,7 @@
   ASSERT_TRUE(control_stream);
 
   QuicStopSendingFrame stop_sending_control_stream(
-      kInvalidControlFrameId, control_stream->id(),
-      static_cast<QuicApplicationErrorCode>(QUIC_STREAM_CANCELLED));
+      kInvalidControlFrameId, control_stream->id(), QUIC_STREAM_CANCELLED);
   EXPECT_CALL(
       *connection_,
       CloseConnection(QUIC_HTTP_CLOSED_CRITICAL_STREAM,
@@ -2920,8 +2917,7 @@
   ASSERT_TRUE(decoder_stream);
 
   QuicStopSendingFrame stop_sending_decoder_stream(
-      kInvalidControlFrameId, decoder_stream->id(),
-      static_cast<QuicApplicationErrorCode>(QUIC_STREAM_CANCELLED));
+      kInvalidControlFrameId, decoder_stream->id(), QUIC_STREAM_CANCELLED);
   EXPECT_CALL(
       *connection_,
       CloseConnection(QUIC_HTTP_CLOSED_CRITICAL_STREAM,
@@ -2933,8 +2929,7 @@
   ASSERT_TRUE(encoder_stream);
 
   QuicStopSendingFrame stop_sending_encoder_stream(
-      kInvalidControlFrameId, encoder_stream->id(),
-      static_cast<QuicApplicationErrorCode>(QUIC_STREAM_CANCELLED));
+      kInvalidControlFrameId, encoder_stream->id(), QUIC_STREAM_CANCELLED);
   EXPECT_CALL(
       *connection_,
       CloseConnection(QUIC_HTTP_CLOSED_CRITICAL_STREAM,
diff --git a/quic/core/qpack/qpack_send_stream.cc b/quic/core/qpack/qpack_send_stream.cc
index 15525b5..176c431 100644
--- a/quic/core/qpack/qpack_send_stream.cc
+++ b/quic/core/qpack/qpack_send_stream.cc
@@ -20,7 +20,7 @@
   QUIC_BUG << "OnStreamReset() called for write unidirectional stream.";
 }
 
-bool QpackSendStream::OnStopSending(uint16_t /* code */) {
+bool QpackSendStream::OnStopSending(QuicRstStreamErrorCode /* code */) {
   stream_delegate()->OnStreamError(
       QUIC_HTTP_CLOSED_CRITICAL_STREAM,
       "STOP_SENDING received for QPACK send stream");
diff --git a/quic/core/qpack/qpack_send_stream.h b/quic/core/qpack/qpack_send_stream.h
index 50d2808..e8a4ea7 100644
--- a/quic/core/qpack/qpack_send_stream.h
+++ b/quic/core/qpack/qpack_send_stream.h
@@ -33,7 +33,7 @@
   // Overriding QuicStream::OnStopSending() to make sure QPACK stream is never
   // closed before connection.
   void OnStreamReset(const QuicRstStreamFrame& frame) override;
-  bool OnStopSending(uint16_t code) override;
+  bool OnStopSending(QuicRstStreamErrorCode code) override;
 
   // The send QPACK stream is write unidirectional, so this method
   // should never be called.
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index 0a7430b..4069501 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -1476,8 +1476,7 @@
   }
 
   QUIC_DLOG(INFO) << ENDPOINT << "STOP_SENDING frame received for stream: "
-                  << frame.stream_id
-                  << " with error: " << frame.application_error_code;
+                  << frame.stream_id << " with error: " << frame.error_code;
 
   visitor_->OnStopSendingFrame(frame);
   return connected_;
diff --git a/quic/core/quic_control_frame_manager.cc b/quic/core/quic_control_frame_manager.cc
index ba00015..c2fdc38 100644
--- a/quic/core/quic_control_frame_manager.cc
+++ b/quic/core/quic_control_frame_manager.cc
@@ -103,8 +103,9 @@
       QuicMaxStreamsFrame(++last_control_frame_id_, count, unidirectional)));
 }
 
-void QuicControlFrameManager::WriteOrBufferStopSending(uint16_t code,
-                                                       QuicStreamId stream_id) {
+void QuicControlFrameManager::WriteOrBufferStopSending(
+    QuicRstStreamErrorCode code,
+    QuicStreamId stream_id) {
   QUIC_DVLOG(1) << "Writing STOP_SENDING_FRAME";
   WriteOrBufferQuicFrame(QuicFrame(
       new QuicStopSendingFrame(++last_control_frame_id_, stream_id, code)));
diff --git a/quic/core/quic_control_frame_manager.h b/quic/core/quic_control_frame_manager.h
index 7615754..d57d120 100644
--- a/quic/core/quic_control_frame_manager.h
+++ b/quic/core/quic_control_frame_manager.h
@@ -77,7 +77,8 @@
 
   // Tries to send an IETF-QUIC STOP_SENDING frame. The frame is buffered if it
   // can not be sent immediately.
-  void WriteOrBufferStopSending(uint16_t code, QuicStreamId stream_id);
+  void WriteOrBufferStopSending(QuicRstStreamErrorCode code,
+                                QuicStreamId stream_id);
 
   // Tries to send an HANDSHAKE_DONE frame. The frame is buffered if it can not
   // be sent immediately.
diff --git a/quic/core/quic_control_frame_manager_test.cc b/quic/core/quic_control_frame_manager_test.cc
index f8e9932..6219038 100644
--- a/quic/core/quic_control_frame_manager_test.cc
+++ b/quic/core/quic_control_frame_manager_test.cc
@@ -31,7 +31,8 @@
 namespace {
 
 const QuicStreamId kTestStreamId = 5;
-const QuicStreamId kTestStopSendingCode = 321;
+const QuicRstStreamErrorCode kTestStopSendingCode =
+    QUIC_STREAM_ENCODER_STREAM_ERROR;
 
 class QuicControlFrameManagerTest : public QuicTest {
  public:
diff --git a/quic/core/quic_framer.cc b/quic/core/quic_framer.cc
index c9eabcb..5bfccb2 100644
--- a/quic/core/quic_framer.cc
+++ b/quic/core/quic_framer.cc
@@ -621,7 +621,7 @@
 // static
 size_t QuicFramer::GetStopSendingFrameSize(const QuicStopSendingFrame& frame) {
   return kQuicFrameTypeSize + QuicDataWriter::GetVarInt62Len(frame.stream_id) +
-         QuicDataWriter::GetVarInt62Len(frame.application_error_code);
+         QuicDataWriter::GetVarInt62Len(frame.error_code);
 }
 
 // static
@@ -5914,12 +5914,13 @@
   }
   // TODO(fkastenholz): when error codes go to uint64_t, remove this.
   if (error_code > 0xffff) {
-    stop_sending_frame->application_error_code = 0xffff;
+    stop_sending_frame->error_code =
+        static_cast<QuicRstStreamErrorCode>(0xffff);
     QUIC_DLOG(ERROR) << "Stop sending error code (" << error_code
                      << ") > 0xffff";
   } else {
-    stop_sending_frame->application_error_code =
-        static_cast<uint16_t>(error_code);
+    stop_sending_frame->error_code =
+        static_cast<QuicRstStreamErrorCode>(error_code);
   }
   return true;
 }
@@ -5932,7 +5933,7 @@
     return false;
   }
   if (!writer->WriteVarInt62(
-          static_cast<uint64_t>(stop_sending_frame.application_error_code))) {
+          static_cast<uint64_t>(stop_sending_frame.error_code))) {
     set_detailed_error("Can not write application error code");
     return false;
   }
diff --git a/quic/core/quic_framer_test.cc b/quic/core/quic_framer_test.cc
index c610997..b67eaf3 100644
--- a/quic/core/quic_framer_test.cc
+++ b/quic/core/quic_framer_test.cc
@@ -10977,7 +10977,7 @@
       PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
 
   EXPECT_EQ(kStreamId, visitor_.stop_sending_frame_.stream_id);
-  EXPECT_EQ(0x7654, visitor_.stop_sending_frame_.application_error_code);
+  EXPECT_EQ(0x7654, visitor_.stop_sending_frame_.error_code);
 
   CheckFramingBoundaries(packet99, QUIC_INVALID_STOP_SENDING_FRAME_DATA);
 }
@@ -10996,7 +10996,7 @@
 
   QuicStopSendingFrame frame;
   frame.stream_id = kStreamId;
-  frame.application_error_code = 0xffff;
+  frame.error_code = static_cast<QuicRstStreamErrorCode>(0xffff);
   QuicFrames frames = {QuicFrame(&frame)};
 
   // clang-format off
@@ -11260,7 +11260,7 @@
             QuicFramer::GetRetransmittableControlFrameSize(
                 framer_.transport_version(), QuicFrame(&path_challenge_frame)));
 
-  QuicStopSendingFrame stop_sending_frame(10, 3, 20);
+  QuicStopSendingFrame stop_sending_frame(10, 3, QUIC_STREAM_CANCELLED);
   EXPECT_EQ(QuicFramer::GetStopSendingFrameSize(stop_sending_frame),
             QuicFramer::GetRetransmittableControlFrameSize(
                 framer_.transport_version(), QuicFrame(&stop_sending_frame)));
diff --git a/quic/core/quic_session.cc b/quic/core/quic_session.cc
index cce8fa5..4a2c49b 100644
--- a/quic/core/quic_session.cc
+++ b/quic/core/quic_session.cc
@@ -262,7 +262,7 @@
     return;
   }
 
-  stream->OnStopSending(frame.application_error_code);
+  stream->OnStopSending(frame.error_code);
 }
 
 void QuicSession::OnPacketDecrypted(EncryptionLevel level) {
@@ -2431,7 +2431,8 @@
   return connection_->GetGuaranteedLargestMessagePayload();
 }
 
-void QuicSession::SendStopSending(uint16_t code, QuicStreamId stream_id) {
+void QuicSession::SendStopSending(QuicRstStreamErrorCode code,
+                                  QuicStreamId stream_id) {
   control_frame_manager_.WriteOrBufferStopSending(code, stream_id);
 }
 
diff --git a/quic/core/quic_session.h b/quic/core/quic_session.h
index bb5107a..e8f3c46 100644
--- a/quic/core/quic_session.h
+++ b/quic/core/quic_session.h
@@ -231,7 +231,8 @@
   virtual void SendWindowUpdate(QuicStreamId id, QuicStreamOffset byte_offset);
 
   // Create and transmit a STOP_SENDING frame
-  virtual void SendStopSending(uint16_t code, QuicStreamId stream_id);
+  virtual void SendStopSending(QuicRstStreamErrorCode code,
+                               QuicStreamId stream_id);
 
   // Called by stream |stream_id| when it gets closed.
   virtual void OnStreamClosed(QuicStreamId stream_id);
diff --git a/quic/core/quic_session_test.cc b/quic/core/quic_session_test.cc
index 38457d6..909a9f1 100644
--- a/quic/core/quic_session_test.cc
+++ b/quic/core/quic_session_test.cc
@@ -2821,7 +2821,7 @@
     return;
   }
   // Check that "invalid" stream ids are rejected.
-  QuicStopSendingFrame frame(1, -1, 123);
+  QuicStopSendingFrame frame(1, -1, QUIC_STREAM_CANCELLED);
   EXPECT_CALL(
       *connection_,
       CloseConnection(QUIC_INVALID_STREAM_ID,
@@ -2834,7 +2834,8 @@
     return;
   }
   // It's illegal to send STOP_SENDING with a stream ID that is read-only.
-  QuicStopSendingFrame frame(1, GetNthClientInitiatedUnidirectionalId(1), 123);
+  QuicStopSendingFrame frame(1, GetNthClientInitiatedUnidirectionalId(1),
+                             QUIC_STREAM_CANCELLED);
   EXPECT_CALL(
       *connection_,
       CloseConnection(QUIC_INVALID_STREAM_ID,
@@ -2852,7 +2853,7 @@
       stream_id, &session_, /*is_static*/ true, BIDIRECTIONAL);
   QuicSessionPeer::ActivateStream(&session_, std::move(fake_static_stream));
   // Check that a stream id in the static stream map is ignored.
-  QuicStopSendingFrame frame(1, stream_id, 123);
+  QuicStopSendingFrame frame(1, stream_id, QUIC_STREAM_CANCELLED);
   EXPECT_CALL(*connection_,
               CloseConnection(QUIC_INVALID_STREAM_ID,
                               "Received STOP_SENDING for a static stream", _));
@@ -2870,7 +2871,7 @@
   QuicStreamPeer::SetFinSent(stream);
   stream->CloseWriteSide();
   EXPECT_TRUE(stream->write_side_closed());
-  QuicStopSendingFrame frame(1, stream_id, 123);
+  QuicStopSendingFrame frame(1, stream_id, QUIC_STREAM_CANCELLED);
   EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
   session_.OnStopSendingFrame(frame);
 }
@@ -2884,7 +2885,7 @@
   TestStream* stream = session_.CreateOutgoingBidirectionalStream();
   QuicStreamId stream_id = stream->id();
   CloseStream(stream_id);
-  QuicStopSendingFrame frame(1, stream_id, 123);
+  QuicStopSendingFrame frame(1, stream_id, QUIC_STREAM_CANCELLED);
   EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
   session_.OnStopSendingFrame(frame);
 }
@@ -2897,7 +2898,7 @@
   }
 
   QuicStopSendingFrame frame(1, GetNthServerInitiatedBidirectionalId(123456),
-                             123);
+                             QUIC_STREAM_CANCELLED);
   EXPECT_CALL(*connection_, CloseConnection(QUIC_HTTP_STREAM_WRONG_DIRECTION,
                                             "Data for nonexistent stream", _))
       .Times(1);
@@ -2910,7 +2911,8 @@
   if (!VersionHasIetfQuicFrames(transport_version())) {
     return;
   }
-  QuicStopSendingFrame frame(1, GetNthClientInitiatedBidirectionalId(1), 123);
+  QuicStopSendingFrame frame(1, GetNthClientInitiatedBidirectionalId(1),
+                             QUIC_STREAM_CANCELLED);
 
   // A Rst will be sent as a response for STOP_SENDING.
   EXPECT_CALL(*connection_, SendControlFrame(_)).Times(1);
@@ -2937,12 +2939,10 @@
   EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream));
 
   QuicStreamId stream_id = stream->id();
-  QuicStopSendingFrame frame(1, stream_id, 123);
+  QuicStopSendingFrame frame(1, stream_id, QUIC_STREAM_CANCELLED);
   // Expect a reset to come back out.
   EXPECT_CALL(*connection_, SendControlFrame(_));
-  EXPECT_CALL(
-      *connection_,
-      OnStreamReset(stream_id, static_cast<QuicRstStreamErrorCode>(123)));
+  EXPECT_CALL(*connection_, OnStreamReset(stream_id, QUIC_STREAM_CANCELLED));
   EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
   session_.OnStopSendingFrame(frame);
 
diff --git a/quic/core/quic_stream.cc b/quic/core/quic_stream.cc
index a5be981..5d64f3b 100644
--- a/quic/core/quic_stream.cc
+++ b/quic/core/quic_stream.cc
@@ -484,7 +484,7 @@
   sequencer_.OnStreamFrame(frame);
 }
 
-bool QuicStream::OnStopSending(uint16_t code) {
+bool QuicStream::OnStopSending(QuicRstStreamErrorCode code) {
   // Do not reset the stream if all data has been sent and acknowledged.
   if (write_side_closed() && !IsWaitingForAcks()) {
     QUIC_DVLOG(1) << ENDPOINT
@@ -502,11 +502,10 @@
     return false;
   }
 
-  stream_error_ = static_cast<QuicRstStreamErrorCode>(code);
+  stream_error_ = code;
 
-  session()->SendRstStream(id(),
-                           static_cast<quic::QuicRstStreamErrorCode>(code),
-                           stream_bytes_written(), /*send_rst_only = */ true);
+  session()->SendRstStream(id(), code, stream_bytes_written(),
+                           /*send_rst_only = */ true);
   rst_sent_ = true;
   CloseWriteSide();
   return true;
diff --git a/quic/core/quic_stream.h b/quic/core/quic_stream.h
index f808d50..a5520bd 100644
--- a/quic/core/quic_stream.h
+++ b/quic/core/quic_stream.h
@@ -344,7 +344,7 @@
 
   // Handle received StopSending frame. Returns true if the processing finishes
   // gracefully.
-  virtual bool OnStopSending(uint16_t code);
+  virtual bool OnStopSending(QuicRstStreamErrorCode code);
 
   // Returns true if the stream is static.
   bool is_static() const { return is_static_; }
diff --git a/quic/core/quic_types.h b/quic/core/quic_types.h
index 4b94152..f39ebdf 100644
--- a/quic/core/quic_types.h
+++ b/quic/core/quic_types.h
@@ -46,9 +46,6 @@
 enum : size_t { kQuicPathFrameBufferSize = 8 };
 using QuicPathFrameBuffer = std::array<uint8_t, kQuicPathFrameBufferSize>;
 
-// Application error code used in the QUIC Stop Sending frame.
-using QuicApplicationErrorCode = uint16_t;
-
 // The connection id sequence number specifies the order that connection
 // ids must be used in. This is also the sequence number carried in
 // the IETF QUIC NEW_CONNECTION_ID and RETIRE_CONNECTION_ID frames.
diff --git a/quic/test_tools/quic_test_utils.h b/quic/test_tools/quic_test_utils.h
index 614e958..854fc23 100644
--- a/quic/test_tools/quic_test_utils.h
+++ b/quic/test_tools/quic_test_utils.h
@@ -813,7 +813,7 @@
   MOCK_METHOD(bool, ShouldKeepConnectionAlive, (), (const, override));
   MOCK_METHOD(void,
               SendStopSending,
-              (uint16_t code, QuicStreamId stream_id),
+              (QuicRstStreamErrorCode code, QuicStreamId stream_id),
               (override));
   MOCK_METHOD(std::vector<std::string>, GetAlpnsToOffer, (), (const, override));
   MOCK_METHOD(std::vector<quiche::QuicheStringPiece>::const_iterator,
diff --git a/quic/tools/quic_simple_server_session_test.cc b/quic/tools/quic_simple_server_session_test.cc
index f5dad7d..780ff9d 100644
--- a/quic/tools/quic_simple_server_session_test.cc
+++ b/quic/tools/quic_simple_server_session_test.cc
@@ -296,9 +296,8 @@
       return;
     }
     EXPECT_CALL(owner_, OnStopSendingReceived(_)).Times(1);
-    QuicStopSendingFrame stop_sending(
-        kInvalidControlFrameId, stream_id,
-        static_cast<QuicApplicationErrorCode>(rst_stream_code));
+    QuicStopSendingFrame stop_sending(kInvalidControlFrameId, stream_id,
+                                      rst_stream_code);
     // Expect the RESET_STREAM that is generated in response to receiving a
     // STOP_SENDING.
     EXPECT_CALL(*connection_, OnStreamReset(stream_id, rst_stream_code));