-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3165 from michael72/master
vertx: end responses safely
- Loading branch information
Showing
8 changed files
with
68 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
server/vertx-server/src/main/scala/sttp/tapir/server/vertx/RichResponse.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sttp.tapir.server.vertx | ||
|
||
import io.vertx.core.Promise | ||
import io.vertx.core.http.HttpServerResponse | ||
|
||
import scala.concurrent.Await | ||
import scala.concurrent.Future | ||
|
||
object Helpers { | ||
|
||
/** Helper class that implements safer ending of a http server response | ||
*/ | ||
implicit class RichResponse(response: HttpServerResponse) { | ||
|
||
/** Ends the response if it hasn't ended yet | ||
* @return | ||
* A future that is completed when the response has been ended | ||
*/ | ||
def safeEnd(): io.vertx.core.Future[Void] = { | ||
if (!response.ended()) response.end() | ||
else io.vertx.core.Future.succeededFuture(null) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
server/vertx-server/src/main/scala/sttp/tapir/server/vertx/VertxErrorHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sttp.tapir.server.vertx | ||
|
||
import io.vertx.ext.web.{RoutingContext} | ||
|
||
import sttp.tapir.server.vertx.Helpers.RichResponse | ||
|
||
/** Common error handler implementation for all Vertx interpreter classes. | ||
* | ||
* Ends the response of the current routing context safely. | ||
* | ||
* @param rc | ||
* the routing context where the response shall be ended | ||
* @param ex | ||
* exception that occurred during the interpreter call | ||
*/ | ||
trait VertxErrorHandler { | ||
def handleError(rc: RoutingContext, ex: Throwable): io.vertx.core.Future[Void] = { | ||
val r = if (rc.response().bytesWritten() > 0) rc.response().safeEnd() else io.vertx.core.Future.succeededFuture[Void](null) | ||
rc.fail(ex) | ||
r | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters