修正异常处理逻辑流程

This commit is contained in:
tursom 2019-11-30 00:15:15 +08:00
parent f073510b4f
commit 702d8a44f3
5 changed files with 20 additions and 14 deletions

View File

@ -13,6 +13,7 @@ class NettyExceptionContent(
val ctx: ChannelHandlerContext,
override val cause: Throwable
) : ExceptionContent, NettyResponseHeaderAdapter() {
override var finished: Boolean = false
val responseBodyBuf: CompositeByteBuf = ctx.alloc().compositeBuffer()!!
var responseStatus: HttpResponseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR
override var responseCode: Int
@ -47,6 +48,7 @@ class NettyExceptionContent(
}
fun finish(response: FullHttpResponse) {
finished = true
val heads = response.headers()
addHeaders(
heads,

View File

@ -5,6 +5,7 @@ import io.netty.channel.ChannelHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.handler.codec.http.FullHttpRequest
import io.netty.handler.codec.http.HttpResponseStatus
@ChannelHandler.Sharable
class NettyHttpHandler(
@ -13,11 +14,7 @@ class NettyHttpHandler(
override fun channelRead0(ctx: ChannelHandlerContext, msg: FullHttpRequest) {
val handlerContext = NettyHttpContent(ctx, msg)
try {
handler.handle(handlerContext)
} catch (e: Throwable) {
handlerContext.finish("${e.javaClass}: ${e.message}")
}
handler.handle(handlerContext)
}
override fun channelReadComplete(ctx: ChannelHandlerContext) {
@ -25,8 +22,13 @@ class NettyHttpHandler(
ctx.flush()
}
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable?) {
if (cause != null) handler.exception(NettyExceptionContent(ctx, cause))
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
val content = NettyExceptionContent(ctx, cause)
handler.exception(content)
if (!content.finished) {
content.responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR
content.finish()
}
ctx.close()
}
}

View File

@ -4,6 +4,7 @@ import cn.tursom.core.buffer.ByteBuffer
interface ExceptionContent : ResponseHeaderAdapter {
val finished: Boolean
val cause: Throwable
var responseCode: Int
fun write(message: String)

View File

@ -5,6 +5,7 @@ interface HttpHandler<in T : HttpContent, in E : ExceptionContent> {
fun exception(e: E) {
e.cause.printStackTrace()
e.responseCode = 500
e.finish()
}

View File

@ -29,12 +29,9 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
addRouter(target ?: this)
}
override fun handle(content: T) {
val router = getRouter(content.method)
var handler = router[content.uri].first
if (handler == null) {
handler = this.router[content.uri].first
}
override fun handle(content: T) = handle(content, getHandler(content.method, content.uri))
open fun handle(content: T, handler: ((T) -> Unit)?) {
if (handler != null) {
handler(content)
} else {
@ -62,6 +59,9 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
getRouter(method)[safeRoute(route)] = handler
}
fun getHandler(method: String, route: String): ((T) -> Unit)? =
getRouter(method)[route].first ?: this.router[route].first
fun deleteRouter(route: String, method: String) {
getRouter(method).delRoute(safeRoute(route))
}
@ -136,7 +136,7 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
companion object {
private val log = try {
LoggerFactory.getLogger(RoutedHttpHandler::class.java)
} catch (e: Exception) {
} catch (e: Throwable) {
null
}