https://github.com/cdimascio/japi-errors
⚡Customizable errors for RESTful and HTTP services.
https://github.com/cdimascio/japi-errors
errors http http-errors http-exceptions http-status-codes rest
Last synced: 8 months ago
JSON representation
⚡Customizable errors for RESTful and HTTP services.
- Host: GitHub
- URL: https://github.com/cdimascio/japi-errors
- Owner: cdimascio
- License: apache-2.0
- Created: 2018-10-22T16:32:11.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-11-16T11:31:19.000Z (about 3 years ago)
- Last Synced: 2025-03-30T07:51:14.552Z (9 months ago)
- Topics: errors, http, http-errors, http-exceptions, http-status-codes, rest
- Language: Java
- Homepage:
- Size: 223 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# japi-errors
 
Customizable errors for RESTful and HTTP services.
Out of the box, **japi-errors** provides [two error formats](#configure) or enables you to [provide](#customize) your own.
All 'out of the box' errors are Jackson ready and can be serialized as *JSON*, *XML*, and *YAML*. If you'd like to use GSON or something else, [create a custom error creator](#customize).
## Install
Gradle
```groovy
compile 'io.github.cdimascio:japi-errors:1.3.1'
```
Maven
```xml
io.github.cdimascio
japi-errors
1.3.1
```
## Usage
Import error methods
```java
import static io.github.cdimascio.japierrors.ApiError.badRequest;
// ...
```
Throw any HTTP error and optionally pass an exception or custom message.
```java
throw notFound();
throw badRequest("id required.");
throw internalServerError(exception);
// ...
```
Assign
```shell
ApiError error = unauthorized();
```
See [examples](#examples) with Spring MVC
## Configure
**japi-errors** supports two error formats "out of the box". They are enabled as follows:
```
ApiError.creator(ApiErrorCreators.BASIC); // The default
ApiError.creator(ApiErrorCreators.WCP);
```
### Basic (default)
```json
{
"code": 400,
"error": "id required."
}
```
### WCP
```json
{
"trace": "1f96a430-dfd8-11e8-9f32-f2801f1b9fd1",
"errors": [{
"code": "bad_request",
"message": "id required."
}]
}
```
## Customize
**japi-errors** enables developers to craft custom error objects. To do so, implement the `ApiErrorCreator` interface, then pass an instance of your creator to `ApiError.creator(myApiErrorCreator)`
To see a working example, check out this [source code](https://github.com/cdimascio/japi-errors/blob/master/src/main/java/io/github/cdimascio/apierrors/basic/ApiErrorBasic.java)
Example:
```java
// Create an api error creator
public class MyApiErrorCreator implements IApiErrorCreator {
@Override
public ApiError create(HttpStatus status, String message) {
return new MyApiError(message);
}
@Override
public ApiError create(HttpStatus status, Throwable t) {
return new MyApiError(t.getMessage());
}
}
```
```java
// Create a custom api error object
// Add Json ignore properties (see source code link above)
public class MyApiError extends ApiError {
@JsonProperty
private String message;
MyApiError() {
super();
}
MyApiError(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
```
## Examples
Check out the following examples to see **japi-errors** in use:
- with [Spring MVC](https://github.com/cdimascio/kotlin-spring-mvc-template/blob/master/src/main/kotlin/api/users/UsersController.kt#L38)
## License
[Apache 2](LICENSE)