From 3aace02837931f0d966676b2ed3137d57be59660 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 8 Nov 2021 16:12:38 +0800 Subject: [PATCH 1/2] fix: Fix the problem that null exception may be triggered when passing wrong information --- .../net/cloudopt/next/web/NextServerVerticle.kt | 15 +++++++-------- .../main/kotlin/net/cloudopt/next/web/Resource.kt | 7 ++++--- .../cloudopt/next/web/render/FreemarkerRender.kt | 2 +- .../net/cloudopt/next/web/render/HbsRender.kt | 2 +- .../net/cloudopt/next/web/render/HtmlRender.kt | 2 +- .../net/cloudopt/next/web/render/JsonRender.kt | 2 +- .../next/web/test/controller/RestController.kt | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/NextServerVerticle.kt b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/NextServerVerticle.kt index 591ac56c..ecf0cbab 100644 --- a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/NextServerVerticle.kt +++ b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/NextServerVerticle.kt @@ -31,7 +31,6 @@ import net.cloudopt.next.validator.ValidatorTool import net.cloudopt.next.waf.Wafer import net.cloudopt.next.web.annotation.* import net.cloudopt.next.web.handler.ErrorHandler -import java.lang.RuntimeException import java.sql.Timestamp import java.text.DateFormat import java.time.LocalDate @@ -204,7 +203,7 @@ class NextServerVerticle : CoroutineVerticle() { } } catch (e: Exception) { e.printStackTrace() - Resource().init(context).fail(500) + Resource().init(context).fail(500, e) } } } @@ -238,7 +237,7 @@ class NextServerVerticle : CoroutineVerticle() { context.next() } } catch (e: Exception) { - resource.fail(500) + resource.fail(500, e) } } } @@ -273,7 +272,7 @@ class NextServerVerticle : CoroutineVerticle() { } } catch (e: Exception) { e.printStackTrace() - resource.fail(500) + resource.fail(500, e) } } } @@ -433,7 +432,7 @@ class NextServerVerticle : CoroutineVerticle() { try { arr[para] = getParaByType(para.findAnnotation()?.value ?: "", para, jsonObject) } catch (e: IllegalArgumentException) { - resource.fail(400) + resource.fail(400, e) e.printStackTrace() return } @@ -443,11 +442,11 @@ class NextServerVerticle : CoroutineVerticle() { try { arr[para] = resource.getBodyJson(para.type.jvmErasure) } catch (e: NullPointerException) { - resource.fail(400) + resource.fail(400, e) e.printStackTrace() return } catch (e: IllegalArgumentException) { - resource.fail(400) + resource.fail(400, e) e.printStackTrace() return } @@ -473,7 +472,7 @@ class NextServerVerticle : CoroutineVerticle() { logger.error( e.message ?: "${resourceTable.url} has error occurred, but the error message could not be obtained " ) - resource.fail(500) + resource.fail(500, e) } } diff --git a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/Resource.kt b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/Resource.kt index ec14adea..24be19c1 100644 --- a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/Resource.kt +++ b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/Resource.kt @@ -33,7 +33,7 @@ import net.cloudopt.next.json.Jsoner.toJsonString import net.cloudopt.next.waf.Wafer import net.cloudopt.next.web.render.RenderFactory import net.cloudopt.next.web.render.Template -import java.lang.RuntimeException +import kotlin.RuntimeException import kotlin.reflect.KClass open class Resource { @@ -366,7 +366,7 @@ open class Resource { * Ends the response. If no data has been written to the response body, * the actual response won't get written until this method gets called. *

- * Once the response has ended, it cannot be used any more. + * Once the response has ended, it cannot be used anymore. */ fun end() { response.end() @@ -382,7 +382,8 @@ open class Resource { * @param statusCode Int the HTTP status code of the response * @param throwable Throwable the throwable used when signalling failure */ - fun fail(statusCode: Int, throwable: Throwable? = null) { + fun fail(statusCode: Int, throwable: Throwable = RuntimeException("Something is wrong, " + + "but no exception messages are caught.")) { context.fail(statusCode, throwable) } diff --git a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/FreemarkerRender.kt b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/FreemarkerRender.kt index b2b37da0..42bf1941 100644 --- a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/FreemarkerRender.kt +++ b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/FreemarkerRender.kt @@ -93,7 +93,7 @@ class FreemarkerRender : Render { promise.complete(writer.toString()) } catch (e: Exception) { promise.fail(e) - resource.fail(500) + resource.fail(500, e) return@await } } diff --git a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HbsRender.kt b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HbsRender.kt index 90e59f3e..d20fa175 100644 --- a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HbsRender.kt +++ b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HbsRender.kt @@ -50,7 +50,7 @@ class HbsRender : Render { return@await } catch (e: Exception) { promise.fail(e) - resource.fail(500) + resource.fail(500, e) return@await } } diff --git a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HtmlRender.kt b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HtmlRender.kt index d0398072..46031c6e 100644 --- a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HtmlRender.kt +++ b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/HtmlRender.kt @@ -53,7 +53,7 @@ class HtmlRender : Render { return@await } catch (e: Exception) { promise.fail(e) - resource.fail(500) + resource.fail(500, e) return@await } val bufferedReader = BufferedReader(InputStreamReader(inputStream)) diff --git a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/JsonRender.kt b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/JsonRender.kt index 4c88d286..13cbabb7 100644 --- a/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/JsonRender.kt +++ b/cloudopt-next-web/src/main/kotlin/net/cloudopt/next/web/render/JsonRender.kt @@ -31,7 +31,7 @@ class JsonRender : Render { } catch (e: Exception) { promise.fail(e) e.printStackTrace() - resource.fail(500) + resource.fail(500, e) return@await } } diff --git a/cloudopt-next-web/src/test/kotlin/net/cloudopt/next/web/test/controller/RestController.kt b/cloudopt-next-web/src/test/kotlin/net/cloudopt/next/web/test/controller/RestController.kt index 9ac1a3e4..55f467a8 100644 --- a/cloudopt-next-web/src/test/kotlin/net/cloudopt/next/web/test/controller/RestController.kt +++ b/cloudopt-next-web/src/test/kotlin/net/cloudopt/next/web/test/controller/RestController.kt @@ -42,13 +42,13 @@ class RestController : Resource() { @GET("/defaultError") fun defaultError() { - fail(402) + fail(500) } @GET("/customError") fun customError() { - fail(401, RuntimeException("401")) + fail(500, RuntimeException("Test Error")) } } \ No newline at end of file From 948ec389e54a544b1f71331cc8c195475ba4dac2 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 8 Nov 2021 16:14:12 +0800 Subject: [PATCH 2/2] chore: Update version to 3.0.3.0-RELEASE --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 48cf98c8..37670c5a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -project_version=3.0.2.0 +project_version=3.0.3.0-RELEASE java_version=11 kotlin_version=1.5.0 kotlinx_version=1.5.0