Spring Cloud: Feign Vnd.error decoder

I’ve already spoiled this extension in one of the previous posts, but essentially I had made a custom Feign error decoder that co ops with Spring Cloud and will automatically handle any Vnd.error returned from remote service calls.

The extension is preatty much simple, you just drop it into your classpath:

Example pom.xml

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

or your build.gradle

compile 'com.github.jmnarloch:feign-vnderror-spring-cloud-starter:1.1.1'

From now on every Vnd.error recieved through Feign call will be automatically unmarshalled and used for populating VndErrorException giving you more structurized access to the error information.

The project source code is available at Github: https://github.com/jmnarloch/feign-vnderror-spring-cloud-starter

Server side setup is also really simple, all you need to do is define custom Spring @ExceptionHandler and build the VndError object and return it as your response:

@ExceptionHandler
public ResponseEntity error(Exception ex) {

    final VndError vndError = new VndError(RequestCorrelationUtils.getCurrentCorrelationId(), ex.getMessage());

    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .header(HttpHeaders.CONTENT_TYPE, "application/vnd.error+json")
            .body(vndError);
}

You may ask yourself what is the reason of using Vnd.error in the first place? The first and far most gain is that you adapt a unified approach for representing your errors within your system which becomes more important if you expose the API for public use.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s