From 4e5c1561e993817c97d7648f8a5524c77a05da0d Mon Sep 17 00:00:00 2001 From: alexjuca Date: Tue, 8 Sep 2020 13:02:33 +0100 Subject: [PATCH 1/2] Added developer section and updated version number --- pom.xml | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2f7a67d..ef3e3be 100644 --- a/pom.xml +++ b/pom.xml @@ -5,11 +5,28 @@ ao.co.nextbss.requirekt requirekt - 1.0.0 + 1.1.0 jar ao.co.nextbss.requirekt requirekt + + + Alexandre Antonio Juca + + Software Engineer + + alexandre.juca@nextbss.co.ao + + + Sergio Maziano + + Senior Software Engineer + + sergio@nextbss.co.ao + + + UTF-8 1.4.0 @@ -18,6 +35,7 @@ false + From 1b8db5342955f43a0d313ce9b5cfb7debb7d05c0 Mon Sep 17 00:00:00 2001 From: alexjuca Date: Tue, 8 Sep 2020 13:02:47 +0100 Subject: [PATCH 2/2] Added developer section and updated version number --- README.md | 66 +++++++++++++++++-- .../ao/co/nextbss/requirekt/Preconditions.kt | 2 +- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 02eecb0..b8f5a5d 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ require(MessageParser.parse(message) == true) { } ``` -#### Customized error response -You can custom the error response by passing in additional arguments. +#### Pass additional arguments +You can change the response by passing in additional arguments. Namely `status` which requires a parameter of type `HttpStatus` and `code` which is a `String` that represents the API error code. @@ -86,6 +86,64 @@ This will return the following JSON error: } ``` +#### Custom Error Responses +requirekt supports custom error responses in JSON using the +`require(value: Boolean, vararg args: ArrayList)` function. + +To use this function add a custom error with the `@ErrorResponse` annotation +and override the `toJSON` function from `AbstractErrorResponse`: + +Example: + +```kotlin +@ErrorResponse +class CustomErrorViewModel(val status: Int = 0, + val code: String? = null, + val message: String? = null, + val type: String? = null): AbstractErrorResponse() { + + override fun toJSON(vararg args: ArrayList): String { + status = fromArgsAsInt(0, args) + code = fromArgsAsString(1, args) + message = fromArgsAsString(2, args) + type = fromArgsAsString(3, args) + return super.toJSON() + } +} +``` + +NOTE: `fromArgsAsInt`, `fromArgsAsFloat` and `fromArgsAsString` are convenience functions + that help your custom error class get the required arguments passed to the `JSON()` function. + +Call the `require` function and pass a boolean to validate and, your arguments wrapped in +`arrayListOf` in the same order as the constructor of your custom error response class. +If they are not in the same order and of the same type, require will fail to create the custom +error response in JSON. + +```kotlin +require(value = false, + arrayListOf( + HttpStatus.FORBIDDEN.value(), + "104", + "Access forbidden. You are not allowed to administrate categories.", + "authentication" + ) +) +``` + +The output would than be the following: + +```json +{ + "errors" : [ { + "status" : 403, + "code" : "104", + "message" : "Access forbidden. You are not allowed to administrate categories.", + "type" : "authentication" + } ] +} +``` + Usage --------------- @@ -121,14 +179,14 @@ maven com.github.nextbss requirekt - 1.0.0 + 1.1.0 ``` gradle ```xml dependencies { - implementation 'com.github.nextbss:requirekt:1.0.0' + implementation 'com.github.nextbss:requirekt:1.1.0' } ``` diff --git a/src/main/kotlin/ao/co/nextbss/requirekt/Preconditions.kt b/src/main/kotlin/ao/co/nextbss/requirekt/Preconditions.kt index f7c6163..8817eb4 100644 --- a/src/main/kotlin/ao/co/nextbss/requirekt/Preconditions.kt +++ b/src/main/kotlin/ao/co/nextbss/requirekt/Preconditions.kt @@ -28,7 +28,7 @@ inline fun require(value: Boolean, lazyMessage: () -> Any) { /** * Throws an [ApiException] for a CustomError with the result of [args] if the [value] is false */ -inline fun require(value: Boolean, vararg args: ArrayList,) { +inline fun require(value: Boolean, vararg args: ArrayList) { val logger: Logger = Logger.getLogger("Preconditions") if (!value) { val customErrorBeanFinder = SpringContext.getBean(CustomErrorBeanFinder::class.java)