japi-errors

Errors for http apis

Лицензия

Лицензия

Группа

Группа

io.github.cdimascio
Идентификатор

Идентификатор

japi-errors
Последняя версия

Последняя версия

1.3.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

japi-errors
Errors for http apis
Ссылка на сайт

Ссылка на сайт

https://github.com/cdimascio/japi-errors
Организация-разработчик

Организация-разработчик

Carmine DiMascio OSS
Система контроля версий

Система контроля версий

https://github.com/cdimascio/jwcp-errors

Скачать japi-errors

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/io.github.cdimascio/japi-errors/ -->
<dependency>
    <groupId>io.github.cdimascio</groupId>
    <artifactId>japi-errors</artifactId>
    <version>1.3.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.cdimascio/japi-errors/
implementation 'io.github.cdimascio:japi-errors:1.3.1'
// https://jarcasting.com/artifacts/io.github.cdimascio/japi-errors/
implementation ("io.github.cdimascio:japi-errors:1.3.1")
'io.github.cdimascio:japi-errors:jar:1.3.1'
<dependency org="io.github.cdimascio" name="japi-errors" rev="1.3.1">
  <artifact name="japi-errors" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.cdimascio', module='japi-errors', version='1.3.1')
)
libraryDependencies += "io.github.cdimascio" % "japi-errors" % "1.3.1"
[io.github.cdimascio/japi-errors "1.3.1"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
com.fasterxml.jackson.core : jackson-annotations jar 2.9.7

test (5)

Идентификатор библиотеки Тип Версия
com.fasterxml.jackson.core : jackson-databind jar 2.9.7
com.mashape.unirest : unirest-java jar 1.4.9
org.junit.jupiter : junit-jupiter-api jar 5.3.1
org.junit.jupiter : junit-jupiter-params jar 5.3.1
org.junit.jupiter : junit-jupiter-engine jar 5.3.1

Модули Проекта

Данный проект не имеет модулей.

japi-errors

Customizable errors for RESTful and HTTP services.

Out of the box, japi-errors provides two error formats or enables you to provide 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.

Install

Gradle

compile 'io.github.cdimascio:japi-errors:1.3.1'

Maven

<dependency>
  <groupId>io.github.cdimascio</groupId>
  <artifactId>japi-errors</artifactId>
  <version>1.3.1</version>
</dependency>

Usage

Import error methods

import static io.github.cdimascio.japierrors.ApiError.badRequest;
// ...

Throw any HTTP error and optionally pass an exception or custom message.

throw notFound();
throw badRequest("id required.");
throw internalServerError(exception);
// ...

Assign

ApiError error = unauthorized();

See 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)

{
  "code": 400,
  "error": "id required."
}

WCP

{
  "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

Example:

// 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());
    }
}
// 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:

License

Apache 2

Версии библиотеки

Версия
1.3.1
1.3.0
1.2.0