Skip to content

Commit

Permalink
Merge pull request #7 from nextbss/v1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
AlexJuca committed Sep 8, 2020
2 parents 98c0893 + 1b8db53 commit be2819d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
66 changes: 62 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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<Any>)` 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<Any>): 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
---------------

Expand Down Expand Up @@ -121,14 +179,14 @@ maven
<dependency>
<groupId>com.github.nextbss</groupId>
<artifactId>requirekt</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</dependency>
```

gradle
```xml
dependencies {
implementation 'com.github.nextbss:requirekt:1.0.0'
implementation 'com.github.nextbss:requirekt:1.1.0'
}
```

Expand Down
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,28 @@

<groupId>ao.co.nextbss.requirekt</groupId>
<artifactId>requirekt</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>ao.co.nextbss.requirekt requirekt</name>

<developers>
<developer>
<name>Alexandre Antonio Juca</name>
<roles>
<role>Software Engineer</role>
</roles>
<email>alexandre.juca@nextbss.co.ao</email>
</developer>
<developer>
<name>Sergio Maziano</name>
<roles>
<role>Senior Software Engineer</role>
</roles>
<email>sergio@nextbss.co.ao</email>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.4.0</kotlin.version>
Expand All @@ -18,6 +35,7 @@
<maven.test.skip>false</maven.test.skip>
</properties>


<dependencyManagement>
<dependencies>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ao/co/nextbss/requirekt/Preconditions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any>,) {
inline fun require(value: Boolean, vararg args: ArrayList<Any>) {
val logger: Logger = Logger.getLogger("Preconditions")
if (!value) {
val customErrorBeanFinder = SpringContext.getBean(CustomErrorBeanFinder::class.java)
Expand Down

0 comments on commit be2819d

Please sign in to comment.