feign-vnderror-spring-cloud-starter

Spring Cloud Netflix Feign vnd.error decoder

License

License

Categories

Categories

Feign Net HTTP Clients
GroupId

GroupId

com.github.jmnarloch
ArtifactId

ArtifactId

feign-vnderror-spring-cloud-starter
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

feign-vnderror-spring-cloud-starter
Spring Cloud Netflix Feign vnd.error decoder
Project URL

Project URL

https://github.com/jmnarloch/feign-vnderror-spring-cloud-starter
Source Code Management

Source Code Management

https://github.com/jmnarloch/feign-vnderror-spring-cloud-starter.git

Download feign-vnderror-spring-cloud-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.jmnarloch/feign-vnderror-spring-cloud-starter/ -->
<dependency>
    <groupId>com.github.jmnarloch</groupId>
    <artifactId>feign-vnderror-spring-cloud-starter</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.jmnarloch/feign-vnderror-spring-cloud-starter/
implementation 'com.github.jmnarloch:feign-vnderror-spring-cloud-starter:1.2.0'
// https://jarcasting.com/artifacts/com.github.jmnarloch/feign-vnderror-spring-cloud-starter/
implementation ("com.github.jmnarloch:feign-vnderror-spring-cloud-starter:1.2.0")
'com.github.jmnarloch:feign-vnderror-spring-cloud-starter:jar:1.2.0'
<dependency org="com.github.jmnarloch" name="feign-vnderror-spring-cloud-starter" rev="1.2.0">
  <artifact name="feign-vnderror-spring-cloud-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.jmnarloch', module='feign-vnderror-spring-cloud-starter', version='1.2.0')
)
libraryDependencies += "com.github.jmnarloch" % "feign-vnderror-spring-cloud-starter" % "1.2.0"
[com.github.jmnarloch/feign-vnderror-spring-cloud-starter "1.2.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.cloud : spring-cloud-starter-feign jar 1.0.3.RELEASE
commons-io : commons-io jar 2.4
org.springframework.boot : spring-boot-starter-hateoas jar 1.2.5.RELEASE

test (3)

Group / Artifact Type Version
org.mockito : mockito-all jar 1.10.19
junit : junit jar 4.12
org.springframework.boot : spring-boot-starter-test jar 1.2.5.RELEASE

Project Modules

There are no modules declared in this project.

Spring Cloud Netflix Feign vnd.error decoder

A custom error decoder for Netflix Feign that will unmarshall vnd.error.

Build Status Coverage Status

Features

A custom Feign ErrorDecoder capable of handling JSON vnd.error responses.

Setup

Add the Spring Cloud starter to your project:

<dependency>
  <groupId>com.github.jmnarloch</groupId>
  <artifactId>feign-vnderror-spring-cloud-starter</artifactId>
  <version>1.2.0</version>
</dependency>

Usage

First on server side make sure that your exception handling logic will return VndErrors (this is part of Spring Hateoas project). You can accomplish that by registering custom @ExceptionHandler in your application

@ExceptionHandler
ResponseEntity<VndErrors> someExceptionHandler(SomeException e) {

    String logref = UUID.randomUUID();
    
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.parseMediaType("application/vnd.error+json"));
    return new ResponseEntity<>(new VndErrors(logref, e.getMessage()), httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
}

Place it in your @Controller or @ControllerAdvice annotated beans. You can populate the logref with some meaningful identifier that afterwards could be used during log analysis. Generally it could be a good idea to add HandlerInterceptor to populate request scoped identifier on preHandle().

The error decoding happens base on content type matching so it is crucial to return application/vnd.error+json as response content type.

You use your Feign clients in the same manner as in any other cases, the only difference is that whenever a client call will end with vnd.error you should expect VndErrorException instead of FeignException.

Example:

try {

    feignClient.operation();    
} catch (VndErrorException exc) {
    ...
}

The VndErrorException makes very convenient to passthrough exceptions, if you call remote service from a web application you can register @ExceptionHandler and return the original vnd.error retrieved from the VndErrorException.

Properties

feign.vnderror.enabled=true # whether to enable the vnd error decoder, true by default

Migration to 1.2.x

The VndErrorException has been reworked to include extra request information like http status, http headers and response body, replacing the existing exception constructors.

Also effort has been made to include the exception type directly in Spring HATEOAS.

License

Apache 2.0

Versions

Version
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0