All Projects → TietoEVRY → quarkus-resteasy-problem

TietoEVRY / quarkus-resteasy-problem

Licence: Apache-2.0 license
Unified error responses for Quarkus REST APIs via Problem Details for HTTP APIs (RFC7807). Supports Quarkus 2.0+ and 1.4+

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to quarkus-resteasy-problem

quarkus-google-cloud-services
Google Cloud Services Quarkus Extensions
Stars: ✭ 42 (+16.67%)
Mutual labels:  quarkus, quarkus-extension
python-httpproblem
Utility library to work with RFC7807 Problem Details for HTTP APIs
Stars: ✭ 18 (-50%)
Mutual labels:  problem, rfc7807
jeeshop
No description or website provided.
Stars: ✭ 39 (+8.33%)
Mutual labels:  quarkus
quarkus-amazon-lambda
A simple demo application showing quarkus usage with Amazon Lambdas
Stars: ✭ 22 (-38.89%)
Mutual labels:  quarkus
optaplanner-quickstarts
OptaPlanner quick starts for AI optimization: many use cases shown in many different technologies.
Stars: ✭ 226 (+527.78%)
Mutual labels:  quarkus
bobbycar
IoT Transportation demo using Red Hat OpenShift and Middleware technologies
Stars: ✭ 33 (-8.33%)
Mutual labels:  quarkus
Trotsky
a cloud native ✏️ blog platform 一个正在开发中的云原生笔记平台
Stars: ✭ 27 (-25%)
Mutual labels:  quarkus
18-comic-finder
禁漫天堂Github Actions下载器🧘
Stars: ✭ 264 (+633.33%)
Mutual labels:  quarkus
laravel-email-exceptions
Email Exceptions package for Laravel 5.x
Stars: ✭ 33 (-8.33%)
Mutual labels:  exception-handling
heterogeneous-microservices
Implementation of the same simple microservice on different frameworks
Stars: ✭ 43 (+19.44%)
Mutual labels:  quarkus
Certified-Rancher-Operator-Thai
มาเรียนรู้ Kuberntes แบบ On-Premise และ Architecture ของ Rancher ที่ใช้ในการจัดการ Kubernetes Cluster เพื่อนำสู่ Certified Kubernetes Administrator และ Certified Rancer Operator
Stars: ✭ 78 (+116.67%)
Mutual labels:  quarkus
keycloak-phone-provider
A Keycloak provider which phone and SMS
Stars: ✭ 83 (+130.56%)
Mutual labels:  quarkus
intellij-quarkus
IntelliJ Quarkus Tools
Stars: ✭ 83 (+130.56%)
Mutual labels:  quarkus
mezzio-problem-details
Problem Details for PSR-7 HTTP APIs addressing the RFC 7807 standard
Stars: ✭ 21 (-41.67%)
Mutual labels:  rfc7807
Coderr.Client
Core client library for Coderr
Stars: ✭ 23 (-36.11%)
Mutual labels:  exception-handling
quarkus-jberet
Quarkus Extension for Batch Applications.
Stars: ✭ 26 (-27.78%)
Mutual labels:  quarkus-extension
bugsnag-java
Bugsnag error reporting for Java.
Stars: ✭ 51 (+41.67%)
Mutual labels:  exception-handling
ignition-stackoverflow
An Ignition tab that fetches StackOverflow questions and provides a searchbar.
Stars: ✭ 74 (+105.56%)
Mutual labels:  exception-handling
stack-trace-art
Turning programming exceptions into art
Stars: ✭ 39 (+8.33%)
Mutual labels:  exception-handling
openshift-quickstart
Developer Workshops related to the Java development on OpenShift
Stars: ✭ 19 (-47.22%)
Mutual labels:  quarkus

Problem Details for HTTP APIs (RFC-7807) implementation for Quarkus / RESTeasy.

Release Quarkus Quarkus

Release Quarkus Quarkus

License

Build status Build status Build status Compatibility with latest stable Quarkus

RFC7807 Problem extension for Quarkus RESTeasy/JaxRS applications. It maps Exceptions to application/problem+json HTTP responses. Inspired by Zalando Problem library.

This extension supports:

  • Quarkus 1.X and 2.X
  • quarkus-resteasy-jackson and quarkus-resteasy-jsonb
  • quarkus-resteasy-reactive-jackson and quarkus-resteasy-reactive-jsonb
  • JVM and native mode

Why you should use this extension?

  • consistency - it unifies your REST API error messages, and gives it much needed consistency, no matter which JSON provider (Jackson vs JsonB) or paradigm (classic/blocking vs reactive) you're using.

  • predictability - no matter what kind of exception is thrown: expected (thrown by you on purpose), or unexpected (not thrown 'by design') - your API consumer gets similar, repeatable experience.

  • safety - it helps prevent leakage of some implementation details like stack-traces, DTO/resource class names etc.

  • time-saving - in most cases you will not have to implement your own JaxRS ExceptionMappers anymore, which makes your app smaller, and less error-prone.

See Built-in Exception Mappers Wiki for more details.

From RFC7807:

HTTP [RFC7230] status codes are sometimes not sufficient to convey
enough information about an error to be helpful.  While humans behind
Web browsers can be informed about the nature of the problem with an
HTML [W3C.REC-html5-20141028] response body, non-human consumers of
so-called "HTTP APIs" are usually not.

Usage

Quarkus 2.X / Java 11+

Make sure JDK 11 is in your PATH, the run:

mvn io.quarkus:quarkus-maven-plugin:2.12.0.Final:create \
    -DprojectGroupId=problem \
    -DprojectArtifactId=quarkus-resteasy-problem-playground \
    -DclassName="problem.HelloResource" \
    -Dpath="/hello" \
    -Dextensions="resteasy,resteasy-jackson"
cd quarkus-resteasy-problem-playground
./mvnw quarkus:add-extension -Dextensions="com.tietoevry.quarkus:quarkus-resteasy-problem:2.0.1"

Or add the following dependency to pom.xml in existing project:

<dependency>
    <groupId>com.tietoevry.quarkus</groupId>
    <artifactId>quarkus-resteasy-problem</artifactId>
    <version>2.0.1</version>
</dependency>

Quarkus 1.X / Java 1.8+

Create a new Quarkus project with the following command:

mvn io.quarkus:quarkus-maven-plugin:1.13.7.Final:create \
    -DprojectGroupId=problem \
    -DprojectArtifactId=quarkus-resteasy-problem-playground \
    -DclassName="problem.HelloResource" \
    -Dpath="/hello" \
    -Dextensions="resteasy,resteasy-jackson,com.tietoevry.quarkus:quarkus-resteasy-problem:1.0.0"
cd quarkus-resteasy-problem-playground

or

<dependency>
    <groupId>com.tietoevry.quarkus</groupId>
    <artifactId>quarkus-resteasy-problem</artifactId>
    <version>1.0.0</version>
</dependency>

Hint: you can also use resteasy-jsonb or reactive equivalents: resteasy-reactive-jackson / resteasy-reactive-jsonb instead of resteasy-jackson

Once you run Quarkus: ./mvnw compile quarkus:dev, and you will find resteasy-problem in the logs:

Installed features: [cdi, resteasy, resteasy-jackson, resteasy-problem]

Now you can throw HttpProblems (using builder or a subclass), JaxRS exceptions (e.g NotFoundException) or ThrowableProblems from Zalando library:

package problem;

import com.tietoevry.quarkus.resteasy.problem.HttpProblem;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloResource {

    @GET
    public String hello() {
        throw new HelloProblem("rfc7807-by-example");
    }

    static class HelloProblem extends HttpProblem {
        HelloProblem(String message) {
            super(builder()
                    .withTitle("Bad hello request")
                    .withStatus(Response.Status.BAD_REQUEST)
                    .withDetail(message)
                    .withHeader("X-RFC7807-Message", message)
                    .with("hello", "world"));
        }
    }
}

Open http://localhost:8080/hello in your browser, and you should see this response:

HTTP/1.1 400 Bad Request
X-RFC7807-Message: rfc7807-by-example
Content-Type: application/problem+json
        
{
    "status": 400,
    "title": "Bad hello request",
    "detail": "rfc7807-by-example",
    "instance": "/hello",
    "hello": "world"
}

This extension will also produce the following log message:

10:53:48 INFO [http-problem] (executor-thread-1) status=400, title="Bad hello request", detail="rfc7807-by-example"

Exceptions transformed into http 500s (aka server errors) will be logged as ERROR, including full stacktrace.

You may also want to check this article on RFC7807 practical usage.
More on throwing problems: zalando/problem usage

Configuration options

All configuration options are build-time properties, meaning that you cannot override them in the runtime (i.e via environment variables).

  • Include MDC properties in the API response (you have to provide those properties to MDC using MDC.put)
quarkus.resteasy.problem.include-mdc-properties=uuid,application,version

Result:

{
  "status": 500,
  "title": "Internal Server Error",
  "uuid": "d79f8cfa-ef5b-4501-a2c4-8f537c08ec0c",
  "application": "awesome-microservice",
  "version": "1.0"
}
  • Enable Smallrye (Microprofile) metrics for http error counters. Requires quarkus-smallrye-metrics in the classpath.

Please note that if you use quarkus-micrometer-registry-prometheus you don't need this feature - http error metrics will be produced regardless of this setting or presence of this extension.

quarkus.resteasy.problem.metrics.enabled=true

Result:

GET /metrics
application_http_error_total{status="401"} 3.0
application_http_error_total{status="500"} 5.0
  • Tuning logging
quarkus.log.category.http-problem.level=INFO # default: all problems are logged
quarkus.log.category.http-problem.level=ERROR # only HTTP 5XX problems are logged
quarkus.log.category.http-problem.level=OFF # disables all problems-related logging

Troubles?

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker. You may also want to have a look at troubleshooting FAQ.

Contributing

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change. For more details check the contribution guidelines.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].