public static void httpWaitForCommandSequenceNumber(
      final CommandQueue commandQueue,
      final KsqlRequest request,
      final Duration timeout
  ) {
    try {
      waitForCommandSequenceNumber(commandQueue, request, timeout);
    } catch (final InterruptedException e) {
      ApiServerUtils.setMaskedSqlIfNeeded(request);
      final long seqNum = request.getCommandSequenceNumber().orElse(-1L);
      final String errorMsg = "Interrupted while waiting for command with the supplied "
          + "sequence number to execute. sequence number: " + seqNum
          + ", request: " + request.getMaskedKsql();
      throw new KsqlRestException(
          Errors.serverErrorForStatement(e, errorMsg, new KsqlEntityList()));
    } catch (final TimeoutException e) {
      throw new KsqlRestException(Errors.commandQueueCatchUpTimeout(
          request.getCommandSequenceNumber().orElse(-1L)));
    }
  }
@Test
  public void shouldThrowKsqlRestExceptionOnTimeout() throws Exception {
    // Given:
    when(request.getCommandSequenceNumber()).thenReturn(Optional.of(SEQUENCE_NUMBER));
    doThrow(new TimeoutException("uh oh"))
        .when(commandQueue).ensureConsumedPast(SEQUENCE_NUMBER, TIMEOUT);

    // When:
    final KsqlRestException e = assertThrows(
        KsqlRestException.class,
        () -> CommandStoreUtil.httpWaitForCommandSequenceNumber(commandQueue, request, TIMEOUT)
    );

    // Then:
    assertThat(e, exceptionStatusCode(is(SERVICE_UNAVAILABLE.code())));
    assertThat(e, exceptionErrorMessage(errorMessage(
        containsString("Timed out while waiting for a previous command to execute"))));
    assertThat(e, exceptionErrorMessage(errorMessage(
        containsString("command sequence number: 2"))));
  }