Problem JSON by Tritt

Description

This library simplifies the implementation of the application/problem+json format as described in RFC 7807 specification.

The standard format for application/problem+json includes:

type (string)

A URI reference [RFC3986] that identifies the problem type.

title (string)

A short, human-readable summary of the problem type. The library defaults to the description of the HTTP status code.

status (number)

The HTTP status code ([RFC7231], Section 6).

detail (string)

A human-readable explanation specific to this occurrence of the problem. The library returns the detail described by the ProblemCode.

instance (string)

A URI reference that identifies the specific occurrence of the problem.

In addition, the library defines three more fields:

code (number)

application level error code.

info (map)

which includes extra information about the actual problem.

trace (object)

tracing information.

Installation on Micronaut

The module tritt-problem-json-mn adds the necessary support for Micronaut based applications. It will allow using ThrowableProblem and automatically use it for validation errors.

In order to enable it, it is just necessary to include the tritt-open repository:

repositories {
    mavenCentral()
    maven {
        name 'trittOpen'
        url 'https://gitlab.com/api/v4/groups/12992043/-/packages/maven'
    }
}

and add the dependency:

dependencies {
    implementation 'no.tritt.problem:problem-json-mn:0.1.0'
}

Usage

The code will throw ThrowableProblem with the proper information.

@Controller("/pets")
public class PetController {

    Map<String, Pet> petRepository = new ConcurrentHashMap<>();

    @Get("/{name}")
    public Pet findPetByName(String name) {
        return petRepository.computeIfAbsent(
            name,
            (missing) -> {
                throw Problem.of(HttpStatus.NOT_FOUND.getCode(), PetProblemCode.PetNotFound)
                    .withInfo("name", missing)
                    .toThrowable();
            }
        );
    }

}

In the case of the Micronaut integration this will automatically be translated to a proper HttpResponse with the problem as body.

The ProblemCode interface might be implemented by for instance an enum that define application level error codes and their detail.

package no.tritt.problem.micronaut.example;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpStatus;

import no.tritt.problem.ProblemCode;

public enum PetProblemCode implements ProblemCode {

    PetNotFound(2000, HttpStatus.NOT_FOUND, "Not possible to find pet");

    private final int code;
    private final Integer status;
    private final String detail;

    PetProblemCode(int code, HttpStatus status, String detail) {
        this.code = code;
        this.status = status.getCode();
        this.detail = detail;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Nullable
    @Override
    public Integer getStatus() {
        return status;
    }

    @Override
    public String getDetail() {
        return detail;
    }
}

Examples

Refer to tritt-problem-json-mn-example to check the configuration of the library in Micronaut.