Обработка исключений в akka-stream в вызове полной функции akka-http - PullRequest
0 голосов
/ 06 февраля 2019

У меня проблемы с тем, что akka-http возвращает исключение, когда akka-поток завершается ошибкой во время работы полным методом akka-http.Работает при запуске потока прежде, чем akka-http вернет результат.Хотелось бы передать результат даже в случае сбоя потока.

Собрать веб-сервер scala akka-http с локальными akka-потоками, выполняющими его в операционной системе Linux Arch

неявный val jsonStreamingSupport = EntityStreamingSupport.json ()

  implicit def testExceptionHandler: ExceptionHandler =
    ExceptionHandler {
      case t: RuntimeException =>
        extractUri { uri =>
          log.error(s"Request to $uri could not be handled normally", t)
          complete(HttpResponse(InternalServerError, entity = s"Wups didn't work"))
        }
    }

  val innerRoute: Route = Route.seal(
    path("just-fail") {
      get {
        val marshallable = Source.single("please show me the fail").via(failingFlow).runWith(Sink.seq)
        complete(marshallable)
      }
    } ~
      path("give-me-fail") {
        get {
          complete(Source.single("please show me the fail").via(failingFlow))
        }
      }
  )

  val failingFlow: Flow[String, String, NotUsed] =
  Flow[String].map{ s =>
    throw new RuntimeException("Ka BOOOOOOOOOOOOOOOM")
  }

Хотите, чтобы он обрабатывал исключение следующим образом:

% curl -v http://localhost:8080/just-fail
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /just-fail HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.63.0
> Accept: */*
> 
< HTTP/1.1 500 Internal Server Error
< Access-Control-Allow-Origin: *
< Server: akka-http/10.1.7
< Date: Wed, 06 Feb 2019 14:43:30 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 16
< 
* Connection #0 to host localhost left intact
Wups didn't work%

Но получает 200 в порядке, с "Ошибка записи: сброс соединения по пиру":

% curl -v http://localhost:8080/give-me-fail                                                                                                                                                                                                             
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /give-me-fail HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.63.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Server: akka-http/10.1.7
< Date: Wed, 06 Feb 2019 14:46:36 GMT
< Transfer-Encoding: chunked
< Content-Type: application/json
< 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

Похоже, что против цели дырки соединения akka-http и akka-stream, материализоваться до завершения маршрута и возврата результата.

...