Git Product home page Git Product logo

problem-details's Introduction

Problem Detail badge badge

Abstract

Map standard and custom exceptions to a http response body containing problem details as specified in RFC-9457 (formerly RFC-7807).

Most things work out of the box: the type and title fields are derived from the exception class name; the detail field is the message of the exception; the instance field is a random UUID URN that is also logged together with the complete stack trace.

These defaults and the status code can be overridden with annotations.

I wrote a blog about this.

Motivation

Getting a consistent error response format for a REST API is a common problem and allows clients to handle specific business errors. Getting some generic HTML response is not helpful. There is a standard for this type of details about an error: RFC-9457. And this library provides a simple way to map exceptions to this format.

Spec & API

This has been proposed to and rejected by several existing specs:

The API in the api module looks quite stable. Some first ideas for a full spec follow below. It’s yet far from complete, but it’s a start:

  • MUST application/problem+json, application/problem+xml; SHOULD any, e.g. +yaml

  • SHOULD render text/html

  • map also @Valid REST params

  • logging: 4xx = DEBUG, 5xx = ERROR; configurable?

  • order of extensions is alphabetic (which is better for tests than random)

  • multiple extensions with the same name: undefined behavior

  • JAXB can’t unmarshal a subclass with the same type and namespace

  • Security considerations: nothing dangerous in problem details (i.e. exception message); stack-trace in logs

  • TODO scan client classpath for @Type annotated exceptions (and document this in the spec and the annotation)

  • TODO inherited annotations

  • TODO cause annotations

  • TODO type factory, e.g. URL to OpenAPI

  • TODO instance factory, e.g. URL to the logging system filtering on an UUID

Dummy-Impl [ri]

It’s called ri, but it’s actually only a POC, and it’s incomplete. See the README for details.

Test

The test module runs integration tests by using JEE Testcontainers, i.e. it can be configured to start different Docker containers with various JEE application servers. By default, it starts a Wildfly.

testcontainer-running

As the containers don’t yet implement the API by themselves, the dummy implementation ri is hard-wired in the tests for now.

Wildfly

Default mvn or explicitly mvn -Djee-testcontainer=wildfly

Open Liberty

mvn -Djee-testcontainer=open-liberty:19.0.0.9-javaee8-java11 -Pwith-slf4j

needs tag for jdk11 support needs dependencies on slf4j-api and slf4j-jdk14

TomEE

mvn -Djee-testcontainer=tomee

3 tests fail, because this version of TomEE (9.0.20 / 8.0.0-M3) doesn’t write the problem detail response entity in some cases for some reason: StandardExceptionMappingIT.shouldMapWebApplicationExceptionWithoutEntityButMessage StandardExceptionMappingIT.shouldMapWebApplicationExceptionWithoutEntityOrMessage ValidationFailedExceptionMappingIT.shouldMapValidationFailedException

Payara

mvn -Djee-testcontainer=payara -Pwith-slf4j

fails due to lack of jdk11 support of the payara image. needs dependencies on slf4j-api and slf4j-jdk14

Spring

We build for JDK 11 and the Jakarta EE 10 APIs. The current versions of Spring Boot don’t support this combination. But you can still use the older com.github.t1:problem-details-api:1.0.10, which was based on Jakarta EE 8.

problem-details's People

Contributors

dependabot[bot] avatar renovate[bot] avatar t1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

problem-details's Issues

type factory, e.g. URL to OpenAPI

Add a mechanism to build type URIs from code. E.g. to link types to some API documentation, without having to copy the full URIs to all annotations.

cause annotations

Clarify (in doc, test and RIs), if and how the annotations behave on caused exceptions.

fix compatibility with jdk 17

We use Jakarta JSON Binding to deserialize an exception (with their extensions). This worked well with JDK 11, but in JDK 17, it fails with a jakarta.json.bind.JsonbException: Internal error: Unable to make field private java.lang.Throwable java.lang.Throwable.cause accessible: module java.base does not "opens java.lang" to unnamed module.

inherited annotations

Clarify (in doc, test and RIs), if and how the annotations behave on super classes.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/maven.yml
  • actions/checkout v4
  • actions/setup-java v4
maven
api/pom.xml
  • jakarta.ws.rs:jakarta.ws.rs-api 4.0.0
  • jakarta.xml.bind:jakarta.xml.bind-api 4.0.2
pom.xml
  • org.apache.maven.plugins:maven-compiler-plugin 3.13.0
  • org.apache.maven.plugins:maven-surefire-plugin 3.3.1
  • org.apache.maven.plugins:maven-failsafe-plugin 3.3.1
  • org.apache.maven.plugins:maven-source-plugin 3.3.1
  • org.apache.maven.plugins:maven-javadoc-plugin 3.8.0
  • org.apache.maven.plugins:maven-release-plugin 3.1.1
  • org.sonatype.plugins:nexus-staging-maven-plugin 1.7.0
  • org.apache.maven.plugins:maven-gpg-plugin 3.2.4
  • org.projectlombok:lombok 1.18.34
  • org.slf4j:slf4j-api 2.0.16
  • org.junit.jupiter:junit-jupiter 5.10.3
  • org.assertj:assertj-core 3.26.3
  • org.mockito:mockito-junit-jupiter 5.12.0
  • org.hibernate.validator:hibernate-validator 8.0.1.Final
  • org.projectlombok:lombok 1.18.34
  • org.slf4j:slf4j-api 2.0.16
  • org.junit.jupiter:junit-jupiter 5.10.3
  • org.assertj:assertj-core 3.26.3
  • org.mockito:mockito-junit-jupiter 5.12.0
  • org.jboss.resteasy:resteasy-core 6.2.9.Final
  • ch.qos.logback:logback-classic 1.5.6
ri-lib/pom.xml
  • com.github.t1:problem-details-api 3.0.2-SNAPSHOT
  • jakarta.ws.rs:jakarta.ws.rs-api 4.0.0
  • jakarta.json.bind:jakarta.json.bind-api 3.0.1
  • jakarta.json:jakarta.json-api 2.1.3
  • jakarta.validation:jakarta.validation-api 3.1.0
  • org.eclipse:yasson 3.0.4
ri/pom.xml
  • jakarta.ws.rs:jakarta.ws.rs-api 4.0.0
  • jakarta.json.bind:jakarta.json.bind-api 3.0.1
  • jakarta.json:jakarta.json-api 2.1.3
  • jakarta.xml.bind:jakarta.xml.bind-api 4.0.2
  • jakarta.validation:jakarta.validation-api 3.1.0
  • com.github.t1:problem-details-api 3.0.2-SNAPSHOT
  • com.github.t1:problem-details-ri-lib 3.0.2-SNAPSHOT
  • org.glassfish.jaxb:jaxb-runtime 4.0.5
  • org.eclipse:yasson 3.0.4
  • jakarta.ejb:jakarta.ejb-api 4.0.1

  • Check this box to trigger a request for Renovate to run again on this repository

Current Status of Project?

Hi,

I'd like to build the project for jaxrs but did not manage to do so. I came across the issue, that the master branch relies on a class of the sandbox api, which is not there. The class ResponseStatus does not exist. So how could this project be build? I started to migrate to javax.ws.rs.core.Response.Status and have all files in my own project.

So I wanted to ask, what the current status of this project is? Is it supposed to be that way or am I doing something wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.